### very beginner loop question

hello everyone,

i'm just trying to pick up programmingand im really not that mentally predispose to excel at it apparently, so please forgive me for my very simple questions!

so im trying to practice with loops (any link to a whole lot of loop exercises?)

anyhow can anyone please explain to me why this isn't an infinite loop?

 ``12345678910111213`` ``````#include using namespace std; int main() { int count = 1; for (; count <= 5 ; count++) { int count = 1; cout << count << "\n"; } return 0; }``````

thank you!
 ``12345`` ``````for(; count <= 5; count++) //count is never > 5. { int count = 1; //you redefine and set count to 1 every loop cout << count << "\n"; }``````
Last edited on
Well, first off, I don't think that'd even compile. If it did it still isn't good form to use the same variable name inside the loop like that. Or rather, to declare another variable of the same name. Variable names should be unique and descriptive. Good code is so because it works, is efficient and requires few comments because the intent of the code is obvious through good variable/function naming.

As for why it's not an infinite loop, every time the code in the body executes it then proceeds to the increment expression and executes that (the count++ part of the for header in this case.) After that it evaluates the test expression and if it's true it executes the body again. Since you are incrementing count on each iteration it will eventually be greater then 5 which would make the test expression false. Once that occurs, execution skips to the first statement after the loop which in this case is main()'s return statement.

Usually for a simple iteration loop you declare the count variable inside the parentheses like this:

 ``12345`` ``````int main() { for (int i = 1; i <= 5; ++i) cout << i << endl; }``````
Last edited on
I'm not totally sure why it isn't an infinite loop but here's one that is:
 ```for(int i = 1; i <= 5; i++){ i = 1; cout<< i << endl; }```

this is not infinite loop

when a loop run to the i = 2;
its run the implementation for the loop right?

so that, u initialize i = 1 again;
when the display shown, of course it's shown 1 .
so think again. for third loop again.
initialize i = 1 again. if course it's keep shown result of 1
 ``12345678910111213141516`` ``````#include using namespace std; int main() { int count = 1; int& outer_count = count ; for (; count <= 5 ; count++) { int count = 1; cout << "inner count: " << count << '\n' ; cout << "outer count: " << outer_count << '\n' ; } return 0; }`````` ```inner count: 1 outer count: 1 inner count: 1 outer count: 2 inner count: 1 outer count: 3 inner count: 1 outer count: 4 inner count: 1 outer count: 5```

Different variables with the same name, are still different variables.

The count variable defined in the body of the for loop has a different scope than the one defined before the for loop. Within the for loop, it is impossible to refer to the count variable defined outside the for loop after the inner count variable is defined, because the inner count variable hides the outer one.

Last edited on
@cire True, but using the same variable names in different scopes is confusing and obfuscates the intention of the code.

As for why the loop that you posted, Fatal, repeats infinitely Felica is right. You are are assigning the value of 1 to i every single time the loop runs. After the body of the loop finishes, it increments i by 1, then tests it. Since i + 1 < 5 it evaluates as true, then executes the body again, which again assigns the value of 1 to i. Repeat ad nauseum.
Last edited on
@Raezzor: Yes, but I suspect the what TC posted was an academic example specifically written to see if the reader could see how the scoping affecting the looping behavior.
> but using the same variable names in different scopes is confusing and obfuscates the intention of the code.
I'm going to nitpick.
Different functions are different scope, there is no problem with using the same names for the variables.
Topic archived. No new replies allowed.