How important is it to know this?

for(int a = 0; a < 5; ++a)
for(int b = 3; b < 6; b=b + 2)
cout << a << " " << b << " ";
cout << endl;


The answer in compiler = 03051315232533354345

I have been trying to do pen and paper to follow the code and understand the output and have gotten to about half way and get confused. My question is how important is it for me to understand something like this for every output to become senior level programmer..

I have been only programming for 2 weeks but i want to get good and know if its important to understand this perfectly.. I showed this to my professor to try and walk me through this and he couldn't even tell me it exactly..
Seriously, you're teacher couldn't tell you what was happening?

This is nested for-loops. The first loop will run once, and the second loop will run, so long as the condition is true. In this case, the conditions are a < 5 and b < 6.

Once you go in to the second loop, you forget about the first loop! Until, the condition of the second loop is false.

So, as soon as this code runs for the first time, as it's nested for-loops, you jump right in to the second loop.
a = 0; b = 3;

Because the second loop has b = b + 2, this now makes b equal to 5. Which, is still less than 6( the condition! )

It then prints( cout ) the values again.
a = 0; b = 5;

Now, when b = b + 2, this now makes the condition false! So you jump back to the first loop, and do the increase( ++a )

And repeat until the outter loops condition is false.

Hope that's explained ok!

As for the need to understand nested loops. I've used them quite a lot within console applications. Using nested for-loops are a must for printing 2D( and more ) arrays/vetors etc.

EDIT:
Bad grammer... lol.
Last edited on
Make sure that you understand this first:
1
2
3
for(int a = 0; a < 5; ++a)
    cout << a << ' ' ;
cout << '\n' ;


Once you have got that far, try to figure this out:
1
2
3
4
for(int a = 0; a < 5; ++a)
    for(int b = 3; b < 6; b=b + 2)
        cout << a << " " << b << " ";
cout << endl;

for(int a = 0; a <5; ++a)
cout << a <<endl;
return 0;

This code above is going to display a... 5 times the code will present 01234/////
What is happening is a is less than 5 because a is 0 so it couts 0! then it goes back into the for loop and hits the ++a what that does is adds 1 to the counter of a... so now memory is a = 1... 1 is still less than 5 so cout displays 1 then it goes back into the for section of loop ++a adds one to a which makes a now 2.. 2 is still less than 5 so it makes it true.. a then couts 2 which is still less than 5 so it goes back into the for loop ++a adds 1 to a... now a is 3 a is still less than 5 so it executed and cout displays 3.. then it goes back into the for loop and ++a adds one to a and now a = 4.. 4 is still less than 5 so it executes and cout displays 4.. then it goes back into the for loop and ++a adds one to a.. a = 5 now... But 5 is not less than 5 so no action is taken.. the program now ends...


I understand all of that but just not when the nested for loop comes into play.... I think Lynx may have nailed what i was not understanding.. I did not understand that u exit the first loop after it executes and that you stay in the inner loop until it is false.. Thank you Lynx very much!
Topic archived. No new replies allowed.