有没有办法在原始条件变为假之前跳出 while 循环? [英] Is there a way to break out of a while loop before the original condition is made false?

查看:34
本文介绍了有没有办法在原始条件变为假之前跳出 while 循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在原始条件变为假之前跳出 while 循环?

Is there a way to break out of a while loop before the original condition is made false?

例如,如果我有:

while (a==true) 
{ 
    doSomething() ; 
    if (d==false) get out of loop ;
    doSomething_that_i_don't_want_done_if_d_is_false_but_do_if_a_and_d_are_true() ;
} 

有没有办法做到这一点?

Is there any way of doing this?

推荐答案

使用 break 语句.

Use the break statement.

if (!d) break;

请注意,您不需要在布尔表达式中与 truefalse 进行比较.

Note that you don't need to compare with true or false in a boolean expression.

这篇关于有没有办法在原始条件变为假之前跳出 while 循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆