简体   繁体   English

如果在删除的for语句中使用StatementExpressionList,ForInit的值是什么时候?

[英]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.

相关问题 如何将 if else 语句中启动的值用于语句之外? - How to take values initiated in an if else statement to be used outside the statement? 如果不使用else语句,这是怎么回事? - What is happening here when the else statement is not used? 在if语句中使用return时会做什么? - What does return do when used inside an if statement? 什么时候应该使用Boolean的null值? - When should null values of Boolean be used? Intent.putExtra在switch语句中使用时返回null - Intent.putExtra returns null when used in switch Statement jsp命令&lt;%=%&gt;在taglib标记语句内的Javascript语句中使用时会被忽略吗? - jsp command <%=%> being ignored when it is used in a Javascript statement, inside a taglib tag statement? 何时必须与递归关联使用return语句时无法理解 - Unable to understand when a return statement must be used in association with recursion 在 if-else 语句中使用 yaml 时出现“密钥重复”错误 - “Key is duplicated” error in yaml when used in if-else statement 如何从SQlite数据库中检索要在if else语句中使用的空字节值? - How to retrieving null Byte values from SQlite database to be used in if else statement? 如何编写通用用户输入语句以多次使用不同的值? - How do I write a general user input statement to be used multiple times with different values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM