break语句是断开循环还是仅断开if语句

break语句是断开循环还是仅断开if语句

本文介绍了break语句是断开循环还是仅断开if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中, break 语句是否仅在 if 语句之外或从 for 循环呢?

In the following code, does the break statement break out of the if statement only or out of the for loop too?

我也需要它来摆脱循环。

I need it to break out of the loop too.

for (int i = 0; i < 5; i++) {
    if (i == temp)
        // do something
    else {
        temp = i;
        break;
    }
}


推荐答案

那会打破for循环。实际上 break 只在讨论循环时才有意义,因为它们会从循环中断/ code>完全,而继续只进入下一个迭代

That would break out of the for loop. In fact break only makes sense when talking about loops, since they break from the loop entirely, while continue only goes to the next iteration.

这篇关于break语句是断开循环还是仅断开if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:43