for和if语句终止 [英] for and if statement termination

查看:468
本文介绍了for和if语句终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在我的方法中,如果有IF条件失败,则应该在另一个for循环的for循环中使用IF语句.我是新手,请帮助我如何终止方法

解决方案

由于其他所有人都夸大了显而易见的内容,因此可以使用breakreturn退出任​​何循环.


使用break;

阅读有关的内容: break(C#参考) [ 公共 void TestMethod() { for ( int i = 0 ; i < 1000 ; ++ i) { 如果(i == 10 ) break ; } }



在这段代码中,我们设置了一个循环来迭代1000次.

但是,if语句将检查i == 10的时间,这时将发生break 语句,并且循环将退出.

因为此后没有其他代码,所以该方法将在退出循环后立即退出.


hi
in my method i have IF statement in for loop in another for loop , if once IF condition fails the execution of method should be terminated . i am new please help me how to terminate method

Since everyone else is overstating the obvious, you can use break or return to exit any loop.


Use break;

Read about it:
break (C# Reference)[^]


As other answers have said, just use the break statement to exit your loop. Consider the following code

public void TestMethod()
{
    for (int i = 0; i < 1000; ++i)
    {
        if (i == 10)
            break;
    }
}



In this code, we''ve set a loop to iterate 1000 times.

However, the if statement is checking for when i == 10, when this happens the break statement will be hit and the loop will exit.

Because there is no other code after this, the method will exit as soon as we exit the loop.


这篇关于for和if语句终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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