4

I just started programming in C++, and while coding I started noticing that I had a lot of for loops in my code, then I started thinking.

In for loops, when you create variables like: for (int **i**=1;i<10;i++), what happens to the i after the loop is done?
Is it okay to create multiple for loops with the same variable name?
What about creating variables inside while loops?

4
  • 1
    in this case i is scoped to the for loop - it doesn't exist anymore after the loop is finished Commented Oct 23, 2017 at 7:56
  • Read about block scope here: en.cppreference.com/w/cpp/language/scope Commented Oct 23, 2017 at 7:58
  • 1
    i, j, k are conventionally understood to be indexes in loop, but if it makes it more clear, do use other names Commented Oct 23, 2017 at 7:59
  • Variables are local to loop statement scope and can't be used outside the loop scope. Commented Oct 23, 2017 at 8:02

5 Answers 5

7

... what happens to the i after the loop is done?

It goes out of scope.

Is it okay to create multiple for loops with the same variable name?

Yes, it's OK and widespread practice to do so.

What about creating variables inside while loops?

Variables created inside of while loops are local to the while loops body. They are different from running variables created in the first part of the for loop header, which is executed only before the loop is entered first time.

Sign up to request clarification or add additional context in comments.

Comments

1

Variables created in a loop are variables that will not preserve that same value after the loop. It is only a temporal value. For example, this code will print i=10, as you are using the first "i" variable.

 #include <iostream> using namespace std; int main() { int i = 0; for (i = 1; i<10; i++); cout << i; return 0; } 

We are in c++, so you can perfectly create another "i" variable in a loop, which will have a temporal value:

#include <iostream> using namespace std; int main() { int i = 0; for (int i = 1; i<10; i++); cout << i; return 0; } 

And it will print the "i" not temporal value, which is 0.

Comments

1

That depends, if you define the iteration value i inside the for() it is a local variable and is only declared inside for(), therefore you shouldn't use it for another loop inside the for() (besides you have a good reason for).

The following code for example will throw an error because the variable i is not declared outside the for() loop, that measn it is not accessable inside of the general main(){}:

#include<cstdlib> #include<iostream> int main() { for (int i = 5; i > 0; i--) { std::cout << "Iteration: " << i << std::endl; } std::cout << "Iteration outside of 'for': " << i << std::endl; // Error return 0; } 

Comments

1

what happens to the 'i' after the loop is done?

'i' will get the last value assigned in the condition inputted.

Is it okay to create multiple for loops with the same variable name?

It will be depending on the syntax or statement used to run this code.

Good practice will be assigned different variable name for different declaration.

This will also fall in the first question. Also, you need to consider where this code will be use is it in web, mobile application or a program.

What about creating variables inside while loops? Thanks in advance.

Best practice is creating variables before doing any condition or statement.

Comments

1

Generic names such as i j k are indeed commonly used, but it is actually a common bad practice. Variable name should properly reflect the purpose of the stored value. For example if it is used to index vector of items then it should be named item_index instead of just i. This may seem unnecessary verbose but such approach helps avoiding common bugs when variables with generic names are messed up, for example compare this frobs[i] = some_frob; bars[j] = some_bar; with frobs[bar_index] = some_frob; bars[frob_index] = some_bar;. Actually in such case not only indexing variable name should be non-generic, but variable type should not be not generic as well so conversion of one indexing type to another would require explicit cast. I think the situation with presence of several generically named variables is similar to Three Star Programmer in the sense that it should never happen in properly written code.

Variables declared inside of loop scope are not visible outside of loop scope, so there is no problem. However construct for(int i=1;i<10;i++) is actually a syntax pitfall, as it ignores normal scoping rules and makes variable i declared outside of loop scope visible inside of scope. This is actually different from C, where declaring variable inside of for construct was prohibited. Basically

for(int i = 1; i < 10; ++i) { // loop body } // i is not visible here i = 42; // error 

in C++ is a more compact equivalent to

{ int i; for(i = 1; i < 10; ++i) { /* loop body */ } } /* i is not visible here */ i = 42; /* error */ 

in C.

Some compilers however may demonstrate non-standard behavior and allow variable declared inside of for construct be visible even after for loop has exited. For example Visual C++ with /Zc:forScope-

for(int i = 1; i < 10; ++i) { // loop body } // i is still visible here i = 42; // no error 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.