简体   繁体   English

是否有可能在Java中声明几个'for'循环终止?

[英]Is it possible to declare several 'for' loop terminations in Java?

I know the sintax in the for loop in Java. 我知道Java for循环中的sintax。 Normally looks something like 通常看起来像

for(initialization; termination; increment){
    ...
}
//Example
for(int i=0; i<100; i++){
    ...
}

And I also know already that we can declare several initializations and increments, such as 而且我也已经知道我们可以声明几个初始化和增量,例如

//Example
int i,j;
//  ini1,  ini2 ;  term ;  inc1,  inc2
for(i=0,   j=100;  i<100;  i++,   j--){
    ...
}

My very clear question is: Is it possible to have more than one termination as well? 我的明确问题是:是否也可以有多个终止? something may be like 事情可能会像

//Example
int i,j;
//  ini1,   ini2 ;   term1,   term2;   inc1,  inc2
for(i=0,    j=100;   i<100,   j>34;    i++,   j--){
    ...
}

Because I'm trying it and it's giving me an error, and I'd rather not to use too many if sentences inside the loop to determine wether to continue or break. 因为我正在尝试,并且给了我一个错误,所以我宁愿不要在循环中使用太多的if语句来确定是否继续或中断。

Is it sintax, or it's just not pssible? 是sintax,还是不可行? Thanks in advance. 提前致谢。

You can supply any boolean condition, which may include logical boolean operators such as ! 您可以提供任何boolean条件,其中可能包含逻辑布尔运算符,例如! , && , and/or || && ,和/或|| :

for(i=0,    j=100;  i<100 && j>34;    i++,   j--){

They can be as simple or complex as you'd like, as long as it evaluates to a boolean . 它们可以像你想的那样简单或复杂,只要它的计算结果为boolean

//Example
int i,j;
//  ini1,   ini2 ;   term1,   term2;   inc1,  inc2
for(i=0,    j=100;   i<100 &&  j>34;    i++,   j--){
    ...
}

Just use the logical operators || 只需使用逻辑运算符|| and && to indicate your stopping conditions: &&表示您的停止条件:

for(i=0, j=100; i < 100 || j > 34; i++, j--)

You are trying to stop when i<100 AND j>34 . i<100 AND j>34时,您正试图停止。 In java, an AND is writen with &&. 在java中,使用&&编写AND。 So, your loop can be: 所以,你的循环可以是:

int i,j;
for(i=0,    j=100;   i<100 &&   j>34;    i++,   j--){
    ...
}

Read, at least, this for more information. 至少阅读这些以获取更多信息。

There can be only one... condition in for loop, but this condition can be build from few others, using logical operators like && which means "AND", || for循环中只能有一个...条件,但是可以使用&&类的逻辑运算符(由“ AND”, ||等逻辑运算符)来构建其他条件。 which means "OR"? 这意味着“或”?

If you want to end loop if all conditions i<100 , j>34 are fulfilled then you should use logical AND operator i<100 && j>34 . 如果要在满足所有条件i<100j>34情况下结束循环,则应使用逻辑AND运算符i<100 && j>34
If you want to end loop if at least one of conditions i<100 , j>34 is fulfilled then you should use logical OR operator i<100 || j>34 如果你想要满足条件i<100j>34的情况下结束循环,那么你应该使用逻辑OR运算符i<100 || j>34 i<100 || j>34 . i<100 || j>34

you can give as many conditions,combined by Logical AND/OR,as you wish given that your expression evaluate to a boolean true/false value. 如果表达式的计算结果为布尔值true / false,则可以根据逻辑AND / OR组合给出任意多个条件。

As per Java Language specification,Java SE 7 Edition: 根据Java语言规范Java SE 7 Edition:

"The basic for statement executes some initialization code, then executes an Expression, a Statement, and some update code repeatedly until the value of the Expression is false. “语句的基本执行一些初始化代码,然后重复执行一个Expression,一个Statement和一些更新代码,直到Expression的值为false。

BasicForStatement: for ( ForInitopt ; Expressionopt ; ForUpdateopt ) Statement" BasicForStatement:for(ForInitopt; Expressionopt; ForUpdateopt)Statement“

...The Expression must have type boolean or Boolean, " ... Expression必须具有boolean或Boolean类型,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM