Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Repeat loop from beginning until new numbers breaks and loop again in c

so I’m confused about how to solve this problem in C. The answer to this question is more likely solve if I can understand the logic. But it seems I cant understand it. Sorry if the question title does not matches the explanation of the problem. So:

In C, loop with using for like this, for(x=0;x<10;x++)
, this will result to loop simultaneously without a break.
For example:

for(x=0;x<10;x++)
printf("this is x = %d",x);

result example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

this is x = 0
this is x = 1
.
.
this is x = 10

So how to loop, but will break in each new number and start over then break/pause until new numbers and so on. For example:

for(x=0;x<//variables;x++)
printf("this is x = %d",x);

result:

(start)
this is x = 0
this is x = 1
(over/break)

(start)
this is x = 0
this is x = 1
this is x = 2
(over/break)

(start)
this is x = 0
this is x = 1
this is x = 2
this is x = 3
(over/break)

.
.
.

(start)
this is x = 0
this is x = 1
this is x = 2
this is x = 3
.
.
.
this is x = 10
(over/break)

So how to do this? This maybe seems simple but I can’t find the solution. I hope the explanation can make the problem clear. Thank you.

>Solution :

Just us 2 for loops instead of 1:

for (y = 0; y < 10; y++)
    for (x = 0; x < y; x++)
        printf("this is x = %d",x);

If you wan’t to change the maximum number, just change the value in the condition of the first loop (y < 10).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading