How to exit the if block in VB.NET

How can I break out of an if statement ?

The output works only for "for", "under", etc.

+6
break
source share
5 answers

At VB.net:

if i > 0 then do stuff here! end if 

In C #:

 if (i > 0) { do stuff here! } 

You cannot escape from the if statement. If you try to do this, your logic is wrong, and you are approaching it from the wrong angle.

An example of what you are trying to achieve will help clarify, but I suspect that you are not structuring it correctly.

+8
source share

There is no such equivalent, but you really need to have an If statement. You might want to study Select Case (VB) or Switch (C #) instructions.

+2
source share

In C # .NET:

 if (x > y) { if (x > z) { return; } Console.Writeline("cool"); } 

Or you can use the goto operator.

+2
source share

you can use

 bool result = false; if (i < 10) { if (i == 7) { result = true; break; } } return result; 
0
source share

I know this is an old post, but I was looking for the same answer, and then I realized that he

  try{ if (i > 0) // the outer if condition { Console.WriteLine("Will work everytime"); if (i == 10)//inner if condition.when its true it will break out of the outer if condition { throw new Exception(); } Console.WriteLine("Will only work when the inner if is not true"); } } catch (Exception ex) { // you can add something if you want } 

`

-3
source share

All Articles