使用return退出循环? [英] Using return to exit a loop?

查看:74
本文介绍了使用return退出循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我写了,做或while循环,才有可能走出这与return关键字

If I write a for, do, or while loop, is it possible to come out of this with the return keyword?

例如:

class BreakTest 
{
public static void Main() 
{
  for (int i = 1; i <= 100; i++) 
  {
     if (i == 5) 
        **return;**
     Console.WriteLine(i);
    }
   }
}



我知道可以用来回报所以我很好奇这是我从来没有尝试过(并且无法访问我的软件来编写代码来测试这个)if语句来退出。

I know return can be used to exit if statements so I am curious about this as I have never tried it (and can't access my software to write the code to test this).

推荐答案

收益将退出当前方法(主要在你的例子)。使用来退出循环。

return will exit the current method (Main in your example). Use break to exit the loop.

这篇关于使用return退出循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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