A question about i++ in a for loop.

C

Chad

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.
 
I

Ian Collins

Chad said:
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.

Because it comes after the break.
 
C

Chad

Chad said:
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.

Because it comes after the break.

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.
 
I

Ian Collins

Chad said:
Chad said:
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.
Because it comes after the break.

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.

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

Richard Tobin

[/QUOTE]
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
 
L

Lew Pitcher

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.

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.
------
 
K

Kaz Kylheku

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.
 
C

Chad

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.

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?

Yes.
 
S

Seebs

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
 
A

Andrew Smallshaw

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.
 

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
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top