Re-declaring Guess inside the loop

I noticed that Ben defines Guess inside the loop, so it gets re-defined every iteration. This seemed to me to be bad practice, but I also heard that it can be helpful to do it that way. Can anyone comment?

the code was along the lines of:
for (int i=1; i<=5; i++)
string Guess = getGuess();

When I coded this, I put the declaration before the loop, so that Guess wouldn’t keep getting re-declared.

I personally avoided this by declaring and initializing before the loop, then passing guess into the function by reference. Code looks cleaner that way in both playGame() and getGuess()

Privacy & Terms