Good evening. Came with a question. I read a book here, in one fragment it was written that if I specify the data type of the variable in the condition of the function, then the value of the variable will have a greater scope than if I specified a variable without type. That is, without the type, the variable will act while the cycle is active, but with the data type it will also act on one more bracket.

Decided to check. Wrote with int-everything worked. I removed the int and the program began to use the value of the variable in the loop in the entire program and, according to some principle, the compiler even changed it, according to some, I do not understand.

Question: I incorrectly understood the information from the textbook? Yes, how things should really be?

#include <iostream> using namespace std; int main() { int done=5; int howmany=3; for( done=1; done<howmany; done++) { cout<<"7"<<endl; } cout<<done; return 0; } 

Conclusion:

7
7
3

  • What is a "function condition"? Do I understand correctly that you just need to read the chapter of the textbook on the scope of variables? - PinkTux
  • Well, if we interpret "I will indicate the data type of the variable in the condition of the function" as "I will declare a local variable in scope" ... - VladD
  • Give a specific quote from the book, well, the code text, not a picture. Although a knowledgeable person can easily guess the essence of your question, it is formulated rather awfully. - αλεχολυτ
  • In a for statement, you can declare and initialize a variable. This is useful when a variable is needed only during the execution of a loop. For example, consider the following statement: for (int done = 1; done <= howmany; done ++) This instruction is equivalent to two others; int done; for (done = 1; done <= howmany; done ++) A variable declared in the initial statement of a for statement is considered declared before the compound statement that defines the loop. Consequently, its scope extends beyond the last instruction of the cycle, - Men's Phisique

2 answers 2

Consider the following program

 #include <iostream> int main() { int done = 5; int howmany = 3; for ( done = 1; done < howmany; done++ ) { std::cout << done << std::endl; } std::cout << done << std::endl; return 0; } 

Its output to the console

 1 2 3 

In this program, the done variable is declared in the function code block of main . First, this variable is initialized to 5

 int done = 5; 

and then in the for clause it is assigned the value 1

 for ( done = 1; done < howmany; done++ ) ^^^^^^^^ 

After exiting the loop, this variable will have the value 3.

Now consider the following program

 #include <iostream> int main() { int done = 5; int howmany = 3; for ( int done = 1; done < howmany; done++ ) { std::cout << done << std::endl; } std::cout << done << std::endl; return 0; } 

Its output to the console

 1 2 5 

In this program, the for clause declares a variable called done , which is the same as the variable name declared in the main code block.

 for ( int done = 1; done < howmany; done++ ) ^^^^^^^^^^^^ 

This variable in the for clause hides a variable of the same name with the same name, declared in an external block of code. Its scope is the body of a for loop. After exiting the loop, this variable ceases to exist.

From C ++ Standard (6.5.3 The for statement)

3 for the statement.

Therefore, the last line of the program output

 5 

already displays to the console the value of the done variable, which was declared before the loop in the code block of the main function

In general, a name declared in the inner declaration area hides the same name declared in the outer declaration area.

From standard C ++ (3.3.10 Name hiding)

1 A name can be hidden declare region or derived class (10.2).

Note that there is an important difference in the definition of a for clause between C ++ and C.

In C ++, the second part of the for clause, where the condition is written, can also be a declaration.

View the following demo.

 #include <iostream> int main() { int done = 5; int howmany = 3; for ( ; int howmany = done; done-- ) { std::cout << done << std::endl; } std::cout << '\n' << howmany << std::endl; return 0; } 

Its output to the console

 5 4 3 2 1 3 

At each iteration of this for loop

 for ( ; int howmany = done; done-- ) ^^^^^^^^^^^^^^^^^^ 

A variable is declared howmany , which hides a variable with the same name, declared in the function main . The value assigned to this variable is converted to a boolean type. If it is 0, the condition will be false and the loop will exit.

After the loop, the value of the howmany variable declared in main is displayed.

Well, and finally, an example, when in a for clause in two parts at once variables are declared that hide the variables of the same name with the same names, declared in main . After exiting the loop, these variables cease to exist, and the variables declared in main become visible.

 #include <iostream> int main() { int done = 10; int howmany = 20; for ( int done = 5; int howmany = done; done-- ) { std::cout << done << std::endl; } std::cout << '\n' << done << std::endl; std::cout << howmany << std::endl; return 0; } 

The output of this program to the console

 5 4 3 2 1 10 20 

The for clause in this program can logically be represented as

 { int done = 5; Label_repeat: { int howmany = done; if ( howmany != 0 ) { std::cout << done << std::endl; done--; goto Label_repeat; } else { goto Label_exit; } } } Label_exit: //... 
  • With some deformity in C (at least in gcc), the variable in the loop condition can also be declared - for (; ({int h = d; h;}); --d) ... (of course, in the body of the loop h not visible) - avp
  • @avp, and the meaning then declare it, if it is not visible in the body? - Grundy
  • @Grundy, yes, in general, no. With the same success, such a block can be made anywhere an expression is allowed. I do not think that in practice it would be nice to read such a code. - avp

As I understand it, you simply specified in a for loop

 for(int done .... 

In this case, the following happened: you declared a new variable done , which has only a for loop as scope, and after exiting it, the done variable safely dies, and the name done refers again to the variable that is declared at the beginning of the main function. Since in the loop you changed the variable that is just the "namesake" of the local variable done from the main function, the latter remained unchanged.

But as soon as you remove the word int from the header of the for loop, the declaration disappears, and the variable used in the for loop is the same as declared in main . Naturally, when the cycle works, it changes.

In general, it is obvious on the question that you understand this topic incorrectly, so read a textbook on the topic of scope of variables more closely.