### Thread: For loop variable increment

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
5
Rep Power
0

#### For loop variable increment

Code:
```int x;
for (x = 1; x <= 10; ++x) {
printf("%d", x);
}```
By reading this code, you can see X assigned with the int 1 inside the for loop itself. But looping this works fine, but wouldn't this assign X to 1 every time the loop starts? since the for loop loops the x <= 10; ++x wouldn't it loop the x = 1 also? and assign it every time and end up on a infinite loop?
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
71
Rep Power
3
Originally Posted by Ephexeve
Code:
```int x;
for (x = 1; x <= 10; ++x) {
printf("%d", x);
}```
By reading this code, you can see X assigned with the int 1 inside the for loop itself. But looping this works fine, but wouldn't this assign X to 1 every time the loop starts? since the for loop loops the x <= 10; ++x wouldn't it loop the x = 1 also? and assign it every time and end up on a infinite loop?
You said it, x is set to 1 when the for loop starts. With each complete iteration of the for loop x is incremented by one until its greater than 10. You could rewrite the for loop like below.

Code:
```int x = 1;

while ( x <= 10 )
{
++x;
/*do something here*/
}```
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
5
Rep Power
0
Yes, what I mean here is since x = 1 is set into the for loop, shouldn't it reset back to 1 every time the loop goes to the next step? This is a bit different, I come from a Perl and Python background.
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
71
Rep Power
3
Originally Posted by Ephexeve
Yes, what I mean here is since x = 1 is set into the for loop, shouldn't it reset back to 1 every time the loop goes to the next step? This is a bit different, I come from a Perl and Python background.
Well if you have a Perl background then you understand what an iterator is. In this Perl example the function each doesn't reset with every loop because it remembers its place with an iterator. If you can concieve that, then the C/C++ for loop should be a snap.

perl.pl
Code:
```#!/usr/bin/perl

use warnings;
use strict;

my %hash =      (
one=>123,
two=>234,
three=>345,
four=>456
);

while ( my @ans = each %hash )
{
print "@ans \n";
}

__END__```
5. Perl and, I'm sure, Python do it the same way as C does. Refer to basic for-loop syntax and theory (disclaimer: the names I am about to use may not be standard):

for (x = 1; x <= 10; ++x)
x=1 is the initialization expression. It is executed only once which is at the start of the loop.
x <= 10 is the test expression. As long as it evaluates to true (non-zero), the loop will run. It is first called after the initialization expression and then thereafter it is called after the iteration expression. It is possible that the test expression evaluates to false (zero) when it is first called, in which case the loop body is never executed.
After the test expression is evaluated to true, the loop body is executed.
++x is the iteration expression. It is run every time the end of the loop is reached. After the iteration expression is run, the test expression is evaluated.

6. #python for loop looks like this

for x in iterable:
# block
7. The important thing to take away here is that in the C/C++ style for() loop, there are three sections to the loop control: the initializer, the test, and the increment. In BNF, it looks like:

Code:
`for-loop ::= 'for' '(' <initializer> ';' <test> ';' <increment> ')' <loop-body>`
The initializer - the part before the first semicolon - is only run once, before the loop itself begins, the test is run at the start of each iteration, and the increment is run at the end of each iteration.
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
4
Rep Power
0
Loops have as purpose to repeat a statement a certain number of times or while a condition is fulfilled.
9. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
5
Rep Power
0
I see. It just seems strange to me, looking at a simple C for loop (this one for example), seems pretty powerful.

In Python for example, you cannot assign in variable in the for loop and do the job there like C, well, you can assign, but it will overwrite every time the loop starts looping.

Code:
```for i in range(10):
y = 0 # it will assign 0 to the loop every time and we will end up with 1 instead of 10.
y += 1```
Anyway guys, now I see the x = 1; only get ran once.

Cheers!
10. Originally Posted by Ephexeve
Code:
```for i in range(10):
y = 0 # it will assign 0 to the loop every time and we will end up with 1 instead of 10.
y += 1```
That is actually a more advanced construct. It's like C#'S for each construct. C is much more basic and down-to-earth than that. You tell the computer just exactly what to do. The higher-level stuff is more general than that. Scripting languages such as Python add a lot of hidden support of high-level stuff, while C is much more "lean and mean" and dealing more directly with the low-level operations you are driving.

Originally Posted by Ephexeve
Anyway guys, now I see the x = 1; only get ran once.
Good.
Last edited by dwise1_aol; October 15th, 2012 at 03:31 AM.