Hey!
Learn how to create a blog and make money online with Google on Googling Matters

Wednesday, March 5, 2008

Two nested Loop? How to exit then!

I think I write a lot about web and forget dot net tricks and solutions!
It’s because searching is the most important things and… and let’s start solving our problem.
Finally the problem comes upon every beginner on programming.
If you have a loop, you can exit it at any moment by executing an Exit Do statement and continue...
Let suppose you have two nested loops or for…Next
Let suppose that you want to exit them from the innermost loop
In the following code you want to exit the both loop but this code don’t work as desired!
For I as integer = 1 To 10
For J as integer = 1 To 10
If I = 0 then Exit
For
Next
Next
The solution is to use two different kinds of loops.
For I as integer = 1 To 10
Do while J <= 10
If I = 0 then exit for
J += 1
Loop
Next
The following code will quit both of the loops! Nice, no?
Still get a problem? Post a comment.

No comments: