[英]When are the values of the ForInit if a StatementExpressionList is used in a for statement discared?
A simple for statement would be: 一个简单的陈述是:
for(ForInit/*Optional*/; EXPRESSION/*Optional*/; ForUpdate/*Optional*/) {
}
ForInit:
StatmentExpressionList
LocalVariableDecleration
StatmentExpressionList
e.g
int i = 0; a = 10, z = 2;
Each expression in the sequence is evaluated from left to right and if any expression completes abruptly the for statement completes abruptly and evaluation of the sequence ends from where it has completed. 从左到右计算序列中的每个表达式,如果任何表达式突然完成,则for语句突然完成,并且序列的评估从它完成的位置结束。 The values of any of the expression which are evaluated left to right are discarded.
从左到右评估的任何表达式的值都被丢弃。
Now what would cause the expression to complete abruptly? 那么什么会导致表达突然完成? At what stage of evaluating each expression in the sequence is the value of that expression discarded, and if the value of that expression is discarded than how does the rest of the for loop execute?
在评估序列中每个表达式的哪个阶段,该表达式的值被丢弃,并且该表达式的值是否被丢弃,而for循环的其余部分是如何执行的?
An example for first parameter 第一个参数的示例
String a = null;
for (int i = a.length(); i < 5; i++)
For the second parameter 对于第二个参数
String a = null;
for (int i = 1; i < a.length(); i++)
And for the third (can you guess it?) 对于第三个(你能猜到吗?)
String a = null;
for (int i = 1; i < 5; a.append(" "));
Of course the rest of the loop is not executed. 当然,循环的其余部分不会被执行。
UPDATE: I interpret the reference to "discarded values" (can't check now) as: 更新:我将对“废弃值”的引用(现在无法检查)解释为:
int i = 13;
try {
String a = null;
for (int i = 1; i < a.length(); i++)
...
} catch (Exception e) {
}
// Here i = 13
A variant would be: 一个变体是:
int a = getA(); //returns 0
for (int i = 10 / a; i < 5; i++) { //Ooops
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.