且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在for循环中声明的变量的范围

更新时间:2022-04-13 15:46:57

局部变量在方法,构造函数或块。

很明显,所有块变量都是局部变量。

根据阻止的定义


块是平衡括号之间的一组零个或多个语句,可以在允许单个语句的任何地方使用。

A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.

所以

{   //block started

}    //block ended

什么变量声明在块内,范围仅限于该块。

What ever the variables declared inside the block ,the scope restricted to that block.

for(int i=0; i<10;i++){
 int j=0;
}

所以 J 范围被限制在该区块内。这是for循环。

So J scope is restricted to inside that block. That is for loop.

for(int i=0; i<10;i++){
 int j=0;
 //do some thing with j ---> compiler says "yes boss"
}
//do some thing with j ---> compiler says "Sorry boss, what is j ??"