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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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!
Hello, one234h. I am a beginner, too, so no worries! Can you just explain me
what you mean by
Why does the for loop not re-assert the initial expression and reset i = 0
each time?
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
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.
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
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:
In BASIC, the same statement looks like this:
it's the exact same thing.
once I is initialized, that's it, that part of the statement is never
revisited.
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
I got it, now, too!
Thanks for your great explanations!
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!