C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 14th, 2012, 03:36 PM
Ephexeve Ephexeve is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 Ephexeve User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old October 14th, 2012, 03:43 PM
G4143 G4143 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 G4143 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
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*/
}

Reply With Quote
  #3  
Old October 14th, 2012, 04:10 PM
Ephexeve Ephexeve is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 Ephexeve User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old October 14th, 2012, 04:34 PM
G4143 G4143 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 G4143 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
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__

Reply With Quote
  #5  
Old October 14th, 2012, 04:35 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,256 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 20 h 36 m 23 sec
Reputation Power: 1985
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.

So to answer your misunderstanding, x=1 is executed only once.

Reply With Quote
  #6  
Old October 14th, 2012, 08:32 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 33 m 33 sec
Reputation Power: 403
#python for loop looks like this

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

Reply With Quote
  #7  
Old October 15th, 2012, 01:31 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,785 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 18 h 21 m 8 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

Reply With Quote
  #8  
Old October 15th, 2012, 01:35 AM
php_developer00 php_developer00 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 4 php_developer00 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #9  
Old October 15th, 2012, 01:55 AM
Ephexeve Ephexeve is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 Ephexeve User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #10  
Old October 15th, 2012, 02:29 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,256 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 20 h 36 m 23 sec
Reputation Power: 1985
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 02:31 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > For loop variable increment

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap