Menu

My first question on the forum

Matt
2011-03-11
2012-09-26
  • Matt

    Matt - 2011-03-11

    Greetings!

    With regards to the simple code:

    for (i = 0; i < 10; i = i + 1)
    printf("i is %d\n", i);

    Let's assume that the expression just ran through successfully once and
    printf()'ed the value and it is about to be run though again.

    Why does the for loop not re-assert the initial expression and reset i = 0
    each time?

    I do realize how noobish this question is and I am therefore grateful to
    anyone who takes the time to offer an answer.

    Cheers!

     
  • Ahaan Rungta

    Ahaan Rungta - 2011-03-13

    Hello, one234h. I am a beginner, too, so no worries! Can you just explain me
    what you mean by

    Let's assume that the expression just ran through successfully once and
    printf()'ed the value and it is about to be run though again.

    Why does the for loop not re-assert the initial expression and reset i = 0
    each time?

     
  • Steve A

    Steve A - 2011-03-15

    Why does the for loop not re-assert the initial expression and reset i = 0
    each time?
    I do realize how noobish this question is and I am therefore grateful to
    anyone who takes the time to offer an answer.

    Because it's a "FOR" loop.
    The compile knows how to interpret the meaning of the following code
    structure:

    for (i = 0; i < 10; i++)
    {
    printf("i is %d\n", i);
    }

    It says:
    1) preset variable "i" to "0",
    2) while i "is less than 10" loop
    3) printf(........)
    4) increment i by 1
    5) loop to step #2 and repeat

     
  • Matt

    Matt - 2011-03-16

    ahaanomegas - What I thought was that the program would loop, "repeat",
    everything that came after it. Sarbayo did an excellent job of explaining in
    English what a "for loop" does. ... what exactly it loops.

    What I was asking was why, after those steps that sarbayo listed, ran through
    1) through 5), why it didn't "loop" back to 1) instead.

     
  • JaeDyWolf

    JaeDyWolf - 2011-03-16

    I believe I understand your question...

    With the line, "for(i = 0 ,i<10, i++)," it might be easy to think of it as
    saying in English, "Set i to zero, and if i is less than 10 then perform
    action and increase i." but in actuality the step "i = 0" isn't part of any
    "expression." The declaration of i happens before the loop begins, and
    shouldn't be considered as being part of the loop itself.

    ...At least I'm hoping that helped.

    ~JaeDyWolf

     
  • Steve A

    Steve A - 2011-03-16

    The declaration of i happens before the loop begins, and shouldn't be
    considered as being part of the loop itself.

    That is absolutely correct.
    (i=0;.........
    is not part of the loop.
    it is an initializer, it initializes i to 0.

    I thought that was clear when I said:

    1)        preset variable "i" to "0",
    2)      .......
    ....
    5)        loop to step #2 and repeat
    

    In BASIC, the same statement looks like this:

    FOR I = 0 TO 10  STEP 1
        PRINT "I is"; I
    NEXT I
    

    it's the exact same thing.
    once I is initialized, that's it, that part of the statement is never
    revisited.

     
  • Steve A

    Steve A - 2011-03-16

    I hate SourceForge, the worst forum there is!

    that was supposed to be:
    1) preset variable "i" to "0",
    2) .......
    ....
    5) loop to step #2 and repeat

    -AND-

    FOR I = 0 TO 10 STEP 1
    PRINT "I is"; I
    NEXT I

     
  • Ahaan Rungta

    Ahaan Rungta - 2011-03-17

    I got it, now, too!

    Thanks for your great explanations!

     
  • Matt

    Matt - 2011-03-24

    Thank you all for your additions to my question!

    That all really helped to clarify it for me and it was interesting. This was
    really a question out of curiosity more than anything else, but I hate not
    knowing why something happens.

    And now I know!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.