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:
1 2 3 4 5
for (int i = 1; i <= 5; ++i)
cout << i << endl;
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.
@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.
> 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.