Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   How Do You Break Out Of Nested If Statements? (http://www.programmingforums.org/showthread.php?t=1510)

masanovic Dec 12th, 2004 10:16 AM

Hi everyone, I have an if then else statement that contains a for loop and another if statement. It also contains another for loop and if statement after the Else part. What I want to do is jump out of the nested if. I've shown below the structure to make it easier to explain.

If
For.......
If......
..........................
(This is where I want to jump out of all these statements)
End If
Next
.......................
Else
For.......
If.......
............................
End If
Next
..................
End If

(This is where I want to jump to)
.............................
.............................

I know that I can use Exit Sub to jump to the end of the sub, and I know I can use Exit For to jump out of a for loop but they are no use to me here. If anyone knows if it's possible to do what I want, then I could use your help.

Cheers

Ooble Dec 12th, 2004 12:28 PM

I think you might have to use *shudder* goto for this one.

Infinite Recursion Dec 13th, 2004 12:25 AM

isn't there a

break;

??

Ooble Dec 13th, 2004 12:41 PM

There is - Exit For, Exit If or whatever, but he wants to break out of multiple nested statements. You could also set a flag, which it checks and breaks if it's true.

bunker Dec 16th, 2004 5:45 AM

make procedure sub ThisIsIWantToJumpTo, and call it where you need. Don't use GOTO, if .Net got it.

melbolt Feb 16th, 2005 2:22 AM

Quote:

Originally Posted by Ooble
There is - Exit For, Exit If or whatever, but he wants to break out of multiple nested statements. You could also set a flag, which it checks and breaks if it's true.

that's how i'd do it too. just set up a flag(boolean variable) called exitloop or something. in each loop you will need a if(exitloop) then break.

:

bool exitloop = false;

if{
.......
      if(exitloop) break;
if{
......
      if(exitloop) break;
}
}




edit: doh, i forgot i am now in the VB forums, not c++ anymore. close enough though, you get the picture right?


All times are GMT -5. The time now is 3:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC