if loop

R

Roman Töngi

for (int i = 1; i <= 10; i++)
cout << i << endl;

I expected the following:
1
2
3
4
5
6
7
8
9
10
11

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?
 
I

Ioannis Vranos

Roman said:
for (int i = 1; i <= 10; i++)


The above can also be written:

for(int i=1; i<11; i++)

cout << i << endl;

I expected the following:
1
2
3
4
5
6
7
8
9
10
11

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?


In both cases the loop body is executed for i values in [1, 10].
 
?

=?ISO-8859-15?Q?Stefan_N=E4we?=

Roman said:
for (int i = 1; i <= 10; i++)
cout << i << endl;

I expected the following:
1
2
3
4
5
6
7
8
9
10
11

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?
No.

Maybe it get's clearer if you re-write the for-loop as a while-loop
(which are semantically equivalent, except for the scope of the loop
variable i):

int i=1;
while(i<=10)
{
cout << i << endl;
i++;
}


HTH
Stefan
 
?

=?ISO-8859-15?Q?Stefan_N=E4we?=

Roman said:
for (int i = 1; i <= 10; i++)
cout << i << endl;

I expected the following:
1
2
3
4
5
6
7
8
9
10
11

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?
No.

Maybe it get's clearer if you re-write the for-loop as a while-loop
(which are semantically equivalent, except for the scope of the loop
variable i):

int i=1;
while(i<=10)
{
cout << i << endl;
i++;
}


HTH
Stefan
 
J

Jakob Bieling

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?

No, but the return value from incrementing is not of interest. It
used nowhere in the code. First, you increment, then you use i to check
the condition. Note: you do not use the return value, but the new value
i has.

hth
 
N

Niels Dybdahl

for (int i = 1; i <= 10; i++)
cout << i << endl;

I expected the following:
1
2
3
4
5
6
7
8
9
10
11

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?

The two expressions i<=19 and i++ are evaluated independently, so when i is
incremented from 10 to 11, then 11 is stored and used for the comparison.

Niels Dybdahl
 
R

Roman Töngi

I understand. Condition and Expression within the if head
are separate.

Thanks
 
U

ulrich

for (int i = 1; i <= 10; i++)
cout << i << endl;

I expected the following:
1
2
3
4
5
6
7
8
9
10
11

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?

i would say that the postfix increments i after the whole loop is done. so
when the loop has printed 10, i is incremented to 11, _then_ the loop
condition is evaluated and the loop isn't entered any more. so, no
printing of 11.

:)
 
H

Howard

ulrich said:
i would say that the postfix increments i after the whole loop is done. so
when the loop has printed 10, i is incremented to 11, _then_ the loop
condition is evaluated and the loop isn't entered any more. so, no
printing of 11.

:)

Just to be clear... the same would happen with a prefix operator instead of
a postfix operator. The expression portion of the for statement is *always*
executed after the loop is executed.

-Howard
 
H

Howard

Roman Tvngi said:
I understand. Condition and Expression within the if head
are separate.

That's a "for" loop, not an "if" statement. Forget your coffee this
morning? :)

-Howard
 
R

ruksan

Howard said:
Just to be clear... the same would happen with a prefix operator instead of
a postfix operator. The expression portion of the for statement is *always*
executed after the loop is executed.

-Howard



Hi,

I'll show you the execution order of a 'for loop' then you'll
understand

for ( int i = 0; i <= 10; i++)
cout << i;

Order Of Execution;

1) i = 0 ----> i initialized

2) i <= 10 condition checked ( if evaluates to true you get in to the
loop else
you exit the loop. your i will remain as 0 if you exit here

3) if in step 2 you did not exist i++ would be evaluated

4) now onwards step 2 and 3 are followed till you exit the loop.


just after you have 10 output i would be incremented to 11 and then the
condtion checked and evaluates to false and you exit the loop without
printing
10. but your i would contain the value 11

rgds

ruksan
 
H

Howard

ruksan said:
Hi,

I'll show you the execution order of a 'for loop' then you'll
understand

for ( int i = 0; i <= 10; i++)
cout << i;

Order Of Execution;

1) i = 0 ----> i initialized

2) i <= 10 condition checked ( if evaluates to true you get in to the
loop else
you exit the loop. your i will remain as 0 if you exit here

3) if in step 2 you did not exist i++ would be evaluated

That should read:

3) if in step 2 you did not exit the loop, then the loop body would be
executed. After that, the expression (i++, in this case) would be
evaluated.
4) now onwards step 2 and 3 are followed till you exit the loop.


just after you have 10 output i would be incremented to 11 and then the
condtion checked and evaluates to false and you exit the loop without
printing
10. but your i would contain the value 11

The variable i would indeed contain 11 when the check is made, but once the
loop is exited, i is no longer in scope and thus cannot be said to have any
value at all.

-Howard
 

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

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,650
Latest member
IanTylor5

Latest Threads

Top