Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 14th, 2012, 04:36 PM
 Ephexeve
Registered User

Join Date: Oct 2012
Posts: 5
Time spent in forums: 51 m 10 sec
Reputation 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
October 14th, 2012, 04:43 PM
 G4143
Contributing User

Join Date: Oct 2012
Posts: 71
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 2
Quote:
 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
October 14th, 2012, 05:10 PM
 Ephexeve
Registered User

Join Date: Oct 2012
Posts: 5
Time spent in forums: 51 m 10 sec
Reputation 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
October 14th, 2012, 05:34 PM
 G4143
Contributing User

Join Date: Oct 2012
Posts: 71
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 2
Quote:
 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
October 14th, 2012, 05:35 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,862
Time spent in forums: 3 Months 1 Day 6 h 58 m 54 sec
Reputation Power: 2199
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
October 14th, 2012, 09:32 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,163
Time spent in forums: 1 Month 3 Weeks 2 Days 9 h 25 m 16 sec
Reputation Power: 455
#python for loop looks like this

for x in iterable:
# block
__________________
[code]Code tags[/code] are essential for python code!

#7
October 15th, 2012, 02:31 AM
 Schol-R-LEA
Commie Mutant Traitor

Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,798
Time spent in forums: 1 Month 2 Weeks 3 Days 1 h 39 m 19 sec
Reputation Power: 1569
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.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. \$4500 obo. tverres@et.ins.gov

#8
October 15th, 2012, 02:35 AM
 php_developer00
Registered User

Join Date: Sep 2012
Posts: 4
Time spent in forums: 1 h 22 m 3 sec
Reputation Power: 0
Loops have as purpose to repeat a statement a certain number of times or while a condition is fulfilled.

#9
October 15th, 2012, 02:55 AM
 Ephexeve
Registered User

Join Date: Oct 2012
Posts: 5
Time spent in forums: 51 m 10 sec
Reputation 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
October 15th, 2012, 03:29 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,862
Time spent in forums: 3 Months 1 Day 6 h 58 m 54 sec
Reputation Power: 2199
Quote:
 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.

Quote:
 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.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > For loop variable increment