#1
  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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    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*/
    }
  4. #3
  5. 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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    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__
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    #python for loop looks like this

    for x in iterable:
    # block
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    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 Short Understanding the C/C++ Preprocessor
    Taming Python A 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
  14. #8
  15. 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.
  16. #9
  17. 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!
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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 02:31 AM.

IMN logo majestic logo threadwatch logo seochat tools logo