for(x = 0; x < 9; x++) when is x incremented?

J

John Smith

Why in the printout of the following code s, t, and u only go up
to 9 but w and x 10? I would think they would at least go up to
the same number, be it 9 or 10.

The last line of the printout is:

column 9 row 9 u 9 v 0 w 10 x 10 y 1 z 48652
-----------------------------------------
for(s = 0; s < 9; s++)
for(t = 0; t < 9; t++)
for(u = 0; u < 9; u++)
{
if(v = Cell[t].GetElement(u) != 0)
for(w = 0; w < 9; w++)
{
if(w != t)
{
for(x = 0; x < 9; x++)
{
if(y = Cell[w].GetElement(x) != 0)
if(v >= y)
if( v == y)
{
z++;
}
}
}
}
cout << "column " << s+1 << " row " << t+1 << \\
" u " << u+1 << " v " << v << " w " << w+1 << " x " << x+1 \\
<< " y " << y << " z " << z << '\n';
}
 
J

John Carson

John Smith said:
Why in the printout of the following code s, t, and u only go up
to 9 but w and x 10? I would think they would at least go up to
the same number, be it 9 or 10.

Incrementation takes place after the body of the for() loop is executed. In
your case, the printout occurs after the body of the w and x for loops but
inside the body of the s, t and u loops. That explains the difference.
 
O

Otac0n

The basic "unrolled" (missused, i know) form of a for-loop is:

for( statement; conditional; operation )
{
body;
}

becomes

statement;
while( conditional )
{
body;
operation;
}

and can, in fact, replace all while-loops (but for clarity, while-loops
are still viable)
 
G

guyarad

and you should see any 10s if the condition is "x<9".
The reason is simple - when outputting he sometime print out x+1 and
sometie just 'v'...

None of the variables reaches 10....
 
P

paulius-maruska

Actualy, none of the variables reaches 9, because conditions in all
cases are "x<9", when variable reaches 9 - loop body isn't executed
anymore.
(e-mail address removed) raše:
 
G

guyarad

you are wrong.
see that when the condition of the loop is evaluated (x<9) and the body
than skipped, x is already 9! otherwise the body of the loop will be
executed.

when you have something like this:
for (int i=0; i<9; i++) {
/// bla.... some statements
}
cout << i;
the output will always (if the loop was not ended abruptly) be '9'.
 
G

guyarad

I'm sorry, I think I misunderstood you.
I guess you meant '9' is never PRINTED - but you wrote "none of the
variables reaches 9".
But also this is not accurate, because w does reaches 9 in before the
cout command is reached.
 
V

Vimal Aravindashan

you are wrong.
see that when the condition of the loop is evaluated (x<9) and the body
than skipped, x is already 9! otherwise the body of the loop will be
executed.

when you have something like this:
for (int i=0; i<9; i++) {
/// bla.... some statements
}
cout << i;
the output will always (if the loop was not ended abruptly) be '9'.

Please quote the messages in your replies. Use the 'Reply' in the 'More
options' link if you are using the google-groups interface.

And, in
for(int i.....) { }
the scope of 'i' is restricted to within the for loop, it is not visible
outside. :)

Cheers,
Vimal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top