Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A question about i++ in a for loop.

0 views
Skip to first unread message

Chad

unread,
Nov 27, 2009, 7:24:22 PM11/27/09
to
The question is about the following C code...

[cdalten@localhost oakland]$ more loopy.c
#include <stdio.h>

int main(void)
{

int i;

for (i = 0; i < 5; i++) {
if (i == 3)
break;
printf("The value of i inside the loop is is: %d\n", i);
}

printf("The value of i is: %d\n", i);
return 0;
}
[cdalten@localhost oakland]$ ./loopy
The value of i inside the loop is is: 0
The value of i inside the loop is is: 1
The value of i inside the loop is is: 2
The value of i is: 3
[cdalten@localhost oakland]$

The way I understand it, when i =2, the value will get incremented to
a value of three (via i++ in the for loop). The question is, how come
the value of i in ...

printf("The value of i inside the loop is is: %d\n", i);

doesn't get effected by the increment? Ie, how can it retain the
previous value after i has been incremented to a value of 3.

Ian Collins

unread,
Nov 27, 2009, 7:51:28 PM11/27/09
to

Because it comes after the break.

--
Ian Collins

Chad

unread,
Nov 27, 2009, 7:58:48 PM11/27/09
to

Maybe I'm being a bit daft here, how can i == 3, but still retain a
value of 2 in

printf("The value of i inside the loop is is: %d\n", i);

It just looks like that i has two different values at the same time.

Ian Collins

unread,
Nov 27, 2009, 8:00:17 PM11/27/09
to

Because when it gets to three, it breaks out of the loop.

--
Ian Collins

Richard Tobin

unread,
Nov 27, 2009, 8:20:54 PM11/27/09
to
In article <ebc90fc7-b91c-4dcd...@z3g2000prd.googlegroups.com>,
Chad <cda...@gmail.com> wrote:

>> > for (i = 0; i < 5; i++) {
>> > if (i == 3)
>> > break;
>> > printf("The value of i inside the loop is is: %d\n", i);
>> > }
>>
>> > printf("The value of i is: %d\n", i);
>> > return 0;
>> > }

>Maybe I'm being a bit daft here, how can i == 3, but still retain a


>value of 2 in
>
>printf("The value of i inside the loop is is: %d\n", i);

It doesn't. It printed 2 on the previous go around the loop.
When it gets to 3 it breaks out of the loop before printing it,
then prints 3 outside the loop.

-- Richard
--
Please remember to mention me / in tapes you leave behind.

Richard Heathfield

unread,
Nov 27, 2009, 8:27:38 PM11/27/09
to
In
<a0cf2cac-ecb4-4743...@y10g2000prg.googlegroups.com>,
Chad wrote:

<snip>

> for (i = 0; i < 5; i++) {
> if (i == 3)
> break;
> printf("The value of i inside the loop is is: %d\n", i);
> }
>
> printf("The value of i is: %d\n", i);

<snip>



> The way I understand it, when i =2, the value will get incremented
> to a value of three (via i++ in the for loop). The question is, how
> come the value of i in ...
>
> printf("The value of i inside the loop is is: %d\n", i);
>
> doesn't get effected by the increment? Ie, how can it retain the
> previous value after i has been incremented to a value of 3.


Because with a value of i, inside the loop, of 3, you can't get as far
as the printf because the break gets in the way and dumps you outside
the loop immediately.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

Lew Pitcher

unread,
Nov 27, 2009, 8:34:22 PM11/27/09
to

The value of <i> is affected by the increment. The increment happens
at the bottom of the loop, not the top, and thus happens /after/ the
printf(), not before. You can think of the increment as happening at
the closing brace of the loop's compound statement.

The first time into the loop, <i> is given the initial value of 0.
Since i < 5, the for() compound statement is executed
Since i != 3, your if-statement bypasses the break.
Now, you printf() the value of <i>, which is still 0
You hit the end of the loop, and <i> is now incremented to 1

The 2nd time into the loop, <i> has the value of 1, from the end of
the previous loop
Since i < 5, the for() compound statement is executed
Since i != 3, your if-statement bypasses the break
Now, you printf() the value of <i>, which is still 1
You hit the end of the loop, and <i> is now incremented to 2

The 3nd time into the loop, <i> has the value of 2, from the end of
the previous loop
Since i < 5, the for() compound statement is executed
Since i != 3, your if-statement bypasses the break
Now, you printf() the value of <i>, which is still 2
You hit the end of the loop, and <i> is now incremented to 3

The 4th time into the loop, <i> has the value of 3, from the end of
the previous loop
Since i < 5, the for() compound statement is executed
Since i == 3, your if-statement executes the break, which terminates
the loop

You now printf() the value of <i>, which is still 3


Clear?
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing.
------

Kaz Kylheku

unread,
Nov 27, 2009, 8:34:52 PM11/27/09
to
On 2009-11-28, Chad <cda...@gmail.com> wrote:
> The question is about the following C code...
>
> [cdalten@localhost oakland]$ more loopy.c
> #include <stdio.h>
>
> int main(void)
> {
>
> int i;
>
> for (i = 0; i < 5; i++) {
> if (i == 3)
> break;
> printf("The value of i inside the loop is is: %d\n", i);
> }

> The way I understand it, when i =2, the value will get incremented to


> a value of three (via i++ in the for loop). The question is, how come
> the value of i in ...
>
> printf("The value of i inside the loop is is: %d\n", i);

The for statement does not evaluate its constituents in the order in which they
are written. Given this template:

for (E1; E2; E3)
S

The for operator will first evalute the E1 material, then the E2 controlling
expression. If E2 yields true, then the body statement S is executed.
If that body does not prematurely terminate the loop by a jump such
as break or goto, then E3 is evaluated. Then back to evaluating E2, and
the loop body again if E2 yields true, and so on.

See? The written order is E1 E2 E3 S, but the evaluation order is E1 E2 S E3

So your i++ increment is performed after the print.

> doesn't get effected by the increment? Ie, how can it retain the
> previous value after i has been incremented to a value of 3.

Your loop body observes the same value of i as the guard expression i < 5.
The increment is done after the print. The early break test in your
loop ensures that when the loop body is entered with i == 3, the loop
terminates and thus the print never happens for that value.

Chad

unread,
Nov 27, 2009, 9:24:36 PM11/27/09
to

Yes.

Seebs

unread,
Nov 28, 2009, 2:02:46 AM11/28/09
to
On 2009-11-28, Chad <cda...@gmail.com> wrote:
> Maybe I'm being a bit daft here, how can i == 3, but still retain a
> value of 2 in

> printf("The value of i inside the loop is is: %d\n", i);

It doesn't.

> It just looks like that i has two different values at the same time.

No. The 2 was printed the previous time through the loop. After that
executed, execution jumped to the ++i, then to the i < 5, then to the
if (i == 3) break; so it exited the loop, with i == 3.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Andrew Smallshaw

unread,
Nov 28, 2009, 10:01:28 AM11/28/09
to
On 2009-11-28, Kaz Kylheku <kkyl...@gmail.com> wrote:
> On 2009-11-28, Chad <cda...@gmail.com> wrote:
>> The question is about the following C code...
>>
>> for (i = 0; i < 5; i++) {
>> if (i == 3)
>> break;
>> printf("The value of i inside the loop is is: %d\n", i);
>> }
>
> The for statement does not evaluate its constituents in the order in which they
> are written. Given this template:
>
> for (E1; E2; E3)
> S

> See? The written order is E1 E2 E3 S, but the evaluation order is E1 E2 S E3


>
> So your i++ increment is performed after the print.

I think the while loop equivalence often makes things clearer.

for (initialisation; condition; increment)
body;

can usually be re-expressed as:

initialisation;
while (condition) {
body;
increment;
}

The only exception is if the body executes a continue statement.
In that case the for loop abandons execution of the body whereas
the while loop version abandons execution of both the body and the
increment for that iteration of the loop.

--
Andrew Smallshaw
and...@sdf.lonestar.org

0 new messages