Jump to content

Help solving an example program (C - Nested Loops)

Go to solution Solved by Stardar1,

OK, I'm going to trace the code for you. 

 

running through the first loop, I is 0, which satisfies the condition and goes into the loop. 

 

now we enter the second loop, as 0<3 for J

here, we print the first "Two"

now we loop through the second loop again, J is now 1, and 1<3

here we print the second "Two"

now, J is 1 and I is 0, which satisfies that if statement, so we break from the second loop.

we then print "One"

we go back to the first loop, i is now 1, j is 0 again, we print another "Two"

the if statement is true again, so we break from the loop and print the "One" again. 

I no longer satisfies the condition, so we dont loop again

I've just been reading about loops in C programing, and I'm trying to work out  the output of some example programs, however I really can't figure out this one (Image below) I understand up to the first Two output. But after that I can't see how Two is ouputted again. It's been confusing me for days.

 

Could anyone explain this one, then maybe I can understand the rest of the examples simular to this. Any help would be appreciated.

 

 

post-225550-0-91331700-1449244476_thumb.

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

OK, I'm going to trace the code for you. 

 

running through the first loop, I is 0, which satisfies the condition and goes into the loop. 

 

now we enter the second loop, as 0<3 for J

here, we print the first "Two"

now we loop through the second loop again, J is now 1, and 1<3

here we print the second "Two"

now, J is 1 and I is 0, which satisfies that if statement, so we break from the second loop.

we then print "One"

we go back to the first loop, i is now 1, j is 0 again, we print another "Two"

the if statement is true again, so we break from the loop and print the "One" again. 

I no longer satisfies the condition, so we dont loop again

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to comment
Share on other sites

Link to post
Share on other sites

It can be really helpful to print stuff out so you can see how the variables change. For example, adding these two print statements for i and j will allow you to see how the nested loop works.

for (i = 0; i < 2; i++){    printf("i = %d", i);    for (j = 0; j < 3; j++)    {        printf("j = %d", j);        // ...
Link to comment
Share on other sites

Link to post
Share on other sites

OK, I'm going to trace the code for you. 

 

running through the first loop, I is 0, which satisfies the condition and goes into the loop. 

 

now we enter the second loop, as 0<3 for J

here, we print the first "Two"

now we loop through the second loop again, J is now 1, and 1<3

here we print the second "Two"

now, J is 1 and I is 0, which satisfies that if statement, so we break from the second loop.

we then print "One"

we go back to the first loop, i is now 1, j is 0 again, we print another "Two"

the if statement is true again, so we break from the loop and print the "One" again. 

I no longer satisfies the condition, so we dont loop again

Nice thanks a lot, so from that i take it nested loops run first as long as the outer loop condition is true? So the nested loop runs until the condition of that nested loops is false, then the outer loop runs and if true goes back ino the nested loop? And the cycle repeats until the outer loop condition is false? If that makes sense? I was getting confused with the order of nested loops before asking this asking you see. But if my thinking is correct on your answer i think i get it now.

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×