if else语句如何中断 [英] how to break if else statement

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

问题描述

如果statemnet我想走出困境,但只有在if else语句有可能的帮助下,才行.

i want to go outseide if statemnet but only with help of if else statement hoe it can be possible

推荐答案

你不能''break out'' if statement.

如果您尝试这样做,那么您的逻辑是错误的,并且您将以错误的角度来对待它.

VB.net:
AFAIK you can''t ''break out'' of an if statement.

If you are attempting this, your logic is wrong and you are approaching it from the wrong angle.

In VB.net:
if i > 0 then
      'Write your code here..
   end if


C#:


In C#:

if (i > 0)
   {
     //Write your code here!
   }



发布您要尝试的代码片段以及您想在什么位置"break".您试图实现的目标的一个示例将有助于阐明,但是我怀疑您在错误地构造它.



Post your code snippets of what you''re trying and where exactly you want to ''break''. An example of what you are trying to achieve would help clarify, but I suspect you are structuring it incorrectly.


您可以使用return; 关键字来破坏if..else..语句.
请参阅链接以获取更多详细信息:
MSDN:返回 [
You can use return; keyword to break if..else.. statement.
Refer the links for more details:
MSDN : return[^]

--Amit


对于if ... else结构,没有for循环和switch语句的"break"语句.
您可能需要查看逻辑,更改执行顺序,或重构代码以使用方法代替-您可以随时使用return语句退出方法.

还有其他两种选择:
1)如果这是错误情况,那么抛出异常是个好主意.
2)有一个goto语句,但是您真的不需要它-我不记得上一次真正需要它的时间,但这可能是十年前或更久了!除非没有其他选择,否则请避免使用,请多加评论,并期望无论如何首先使用它都会受到滥用. Goto是在代码审查中发现的第一件事,因此最好有一个非常非常好的理由.
There is no "break" statement for if...else constructs in the way there is for loops and switch statements.
You probably need to look at your logic, and change the order in which you are doing things, or refactor your code to use methods instead - you can exit a method with a return statement at any time.

There are two other alternatives:
1) If this is an error condition, then throwing an exception is a good idea.
2) There is a goto statement, but you really shouldn''t need it - I can''t remember the last time I really needed one, but it is probably ten years ago or more! Avoid unless there is no other alternative, comment profusely, and expect to get abuse anyway for using it in the first place. Goto is the first thing to be spotted at a code review, so there had better be a very, very good justification.


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

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