Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to break out of "if" block in VB.NET

Tags:

asp.net

break

How can I break out of an if statement?

Exit only works for "for", "sub", etc.

like image 577
tim Avatar asked Aug 05 '10 09:08

tim


People also ask

Do breaks break out of if statements?

breaks don't break if statements. But break s don't break out of if s. Instead, the program skipped an entire section of code and introduced a bug that interrupted 70 million phone calls over nine hours. You can't break out of if statement until the if is inside a loop.

How do you end an if statement in C#?

In C#, the break statement is used to terminate a loop(for, if, while, etc.) or a switch statement on a certain condition. And after terminating the controls will pass to the statements that present after the break statement, if available.


3 Answers

In VB.net:

if i > 0 then
   do stuff here!
end if

In C#:

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

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.

An example of what you are trying to achieve would help clarify, but I suspect you are structuring it incorrectly.

like image 109
Tom Gullen Avatar answered Oct 21 '22 16:10

Tom Gullen


There isn't such an equivalent but you should't really need to with an If statement. You might want to look into using Select Case (VB) or Switch (C#) statements.

like image 26
Radu Avatar answered Oct 21 '22 17:10

Radu


In C# .NET:

if (x > y) 
{
    if (x > z) 
    {
        return;
    }

    Console.Writeline("cool");
}

Or you could use the goto statement.

like image 39
thelost Avatar answered Oct 21 '22 17:10

thelost