[CSI] scoping of variables in loops
Mikhail Nesterenko
mikhail at cs.kent.edu
Wed Jun 29 13:11:29 EDT 2016
CSI students,
The scope of a variable in a block is this block. While-expression
lies outside the block and, hence, the variable declared inside the
block is out-of-scope in the expression. That is:
do{
int i = 0; // declared inside the block
++i;
cout << i;
}while (i < 10); // accessed outside the block
does not compile. The only exception is a loop variable of for-loop
which is declared outside the for-loop block yet whose scope is the
for-loop block. So the below statement compiles:
for(int i=0; i < 10; ++i){
cout << i;
}
Thanks,
--
Mikhail
More information about the cs13001
mailing list