C++ noob question dont be mad

JumBie

Golden Member
May 2, 2011
1,645
1
71
Why does this code print the following:
1 1 1
1 1 2
2 1 1
2 1 2
3 1 1
3 1 2
Code:
Code:
#include <StdAfx.h>
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int x, y, z;
	
	for (x = 1;x <= 3 ; x++)
		{
			for (y = 1; y < 3; y++)
			{
				if (y == 2)
					break;
				for (z = 1; z < 3; z++)
				{
			cout << x << " " << y << " " << z << endl;
			
		}
}
	}

		
		
		

	int yes;
	cin >> yes;
    return 0;
}

To my understanding i know why the middle is always 1, because once y == 2 it breaks. However why is it that x is not 2 in the second line? isn't x suppose to add 1 to its self after the first loop runs? Also z goes back to 1 on the third line.

Can anyone explain this to me, I'm slightly confused and have probably missed the reasoning for this somewhere.Too all the experts out this i know this is the stupidest question in the world, but I've literally just started and I'm looking to understand every bit that i can, rather than to pass it over.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
If you have a "nested" (inner) loop like the loops for y, z, those loops will run completely before going back to running the outer loop.

x loop starts. Each time the x loop runs, it _fully_ runs the y loop once. Try this:

Code:
for (int x = 0; x < 5; x++) {
  cout << "x runs  " << x << endl;
  for (int y = 0; y < 3; y++)
  {
    cout << "y runs  " << y << endl;
  }
}
 

JumBie

Golden Member
May 2, 2011
1,645
1
71
If you have a "nested" (inner) loop like the loops for y, z, those loops will run completely before going back to running the outer loop.

x loop starts. Each time the x loop runs, it _fully_ runs the y loop once. Try this:

Code:
for (int x = 0; x < 5; x++) {
  cout << "x runs  " << x << endl;
  for (int y = 0; y < 3; y++)
  {
    cout << "y runs  " << y << endl;
  }
}
I'm still having trouble understanding the order of which the numbers are being printed. If anyone could elaborate more, on why these numbers to me seem out of order.
 

Cogman

Lifer
Sep 19, 2000
10,283
135
106
I'm still having trouble understanding the order of which the numbers are being printed. If anyone could elaborate more, on why these numbers to me seem out of order.

Every C++ program operates a step at a time. where the next step occurs depends on the scope that the step counter counter is pointing at.

It can be very useful to just move your finger along the code, on step at a time. When you come to a for loop, remember that you repeat the stuff inside the braces until the condition is met.

Keep track of the values in the variables on paper as you are doing this to get a better feel for what is going on.

If it helps, you could put lots of outputs everywhere in the code. For example.

Code:
for (int x = 0; x < 3; ++x)
{
   cout << "start of first for loop, x has " << x << endl;
   for (int y = 0; y < 3; ++y)
   {
      cout << "\tstart of second for loop, x has " << x << " y has " << y << endl;
      cout << "\tend of second for loop";
   }
   cout << "end of first for loop";
}
 

iCyborg

Golden Member
Aug 8, 2008
1,335
57
91
What development environment are you using? The greatest insight would probably come from running a debugger and stepping over the code watching the variable values as they change.

First x=1, then y=1, then z=1 and you print 1,1,1. Then you increment z since it's the innermost loop, so z=2, x,y didn't change, and you print 1,1,2. You can't increment x before z if you're deep inside for_z loop
 

JumBie

Golden Member
May 2, 2011
1,645
1
71
for (x = 1;x <= 3 ; x++)
{
for (y = 1; y < 3; y++)
{
if (y == 2)
break;
for (z = 1; z < 3; z++)
{
cout << x << " " << y << " " << z << endl;

Like i understand the first line:
1 1 1 (everything starts as 1)
1 1 2 (if this is the second run of the loop, why did x++ change to 2? shouldnt it be 2 1 2?)
2 1 1 ( if now this is the 3rd loop shouldn't it be 3 1 3? why has z gone back to 1?)

These are just a few questions i have, I'm still confused, i think i need like a babies explanation for it. I'm using Visual Basic 2010.
 

humanure

Senior member
Dec 28, 2005
441
0
0
Let me try to walk through it for you maybe it will help

When you start the first line is executed, you declare 3 ints x,y,z

next is the "x" for loop, x=1

now we enter the "y" loop, y=1, then the break statement terminates the y loop

now the "z" loop starts z=1

inside the z loop you have your output statement so the first line printed is 1 1 1

the z loop executes again because z<3, z=2, no changes happened to x or y since we have not gotten out of the z loop yet, so the next line output in the z loop is 1 1 2

now z increments again but the test z < 3 fails, now we increment x, x=2

enter y loop again, same result

back to z loop, z=1 again so next output is 2 1 1, increment z, z= 2 output 2 1 2

now the test z <3 fails, back to x, x increments to 3, test passes x<=3, y loop, z loop again z is assigned 1, prints 3 1 1, z increments to 2, 3 1 2

each time the z loop starts z is assigned 1, then increments to 2, test z < 3 fails, at this point the outer x loop runs again and x is incremented. That is why the second line is 1 1 2, not 2 1 2. Your code will never output 3 1 3 because your test is z < 3 and that is false if z =3.
 

JumBie

Golden Member
May 2, 2011
1,645
1
71
Let me try to walk through it for you maybe it will help

When you start the first line is executed, you declare 3 ints x,y,z

next is the "x" for loop, x=1

now we enter the "y" loop, y=1, then the break statement terminates the y loop

now the "z" loop starts z=1

inside the z loop you have your output statement so the first line printed is 1 1 1

the z loop executes again because z<3, z=2, no changes happened to x or y since we have not gotten out of the z loop yet, so the next line output in the z loop is 1 1 2

now z increments again but the test z < 3 fails, now we increment x, x=2

enter y loop again, same result

back to z loop, z=1 again so next output is 2 1 1, increment z, z= 2 output 2 1 2

now the test z <3 fails, back to x, x increments to 3, test passes x<=3, y loop, z loop again z is assigned 1, prints 3 1 1, z increments to 2, 3 1 2

each time the z loop starts z is assigned 1, then increments to 2, test z < 3 fails, at this point the outer x loop runs again and x is incremented. That is why the second line is 1 1 2, not 2 1 2. Your code will never output 3 1 3 because your test is z < 3 and that is false if z =3.
Wow i finally understand! Thank you so much for taking the time to explain, and i appreciate your patience with me! You've helped me more than you know, and thank you to everyone for your input.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Y is only allowed to be 1 prior to the output statement.

when Y becomes 2, you bust out of the loop and bypass the output statement.

then the outer loop continues on with Y restarting at 1.