在for循环中声明的变量的范围 [英] Scope of variable declared inside a for loop
本文介绍了在for循环中声明的变量的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
for(int i=0; i<10;i++){
int j=0;
}
j是块变量还是局部变量?我看到j的范围只有for循环结束
Is j a block variable or a local variable? I see that j's scope is only till the for loop ends
推荐答案
局部变量在方法,构造函数或块。
很明显,所有块变量都是局部变量。
根据阻止的定义
块是平衡括号之间的一组零个或多个语句,可以在允许单个语句的任何地方使用。
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 ??"
这篇关于在for循环中声明的变量的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文