Solved

# Easy one for you guys here!

Posted on 2003-03-24
Medium Priority
150 Views
OK im a complete VB newbie, self taught in the last three days, in fact purely because some fool at uni in my team thought it would be a good idea to do our prototype in VB then decided that it would be a good idea if did it LOL

I have a for loop with an if statement inside... If i was using java i would use break to accomplish what i want to achieve...

For(int i;i < whatever;i++) {
if(something.equals(Something)) {
break;
}
}

Break will kill the for loop

Is there an equivelent in VB to this break statement?

I know there's many more elegant ways of doing this but i dont know what im doing with this blasted language and i have a few days to do a lot!!!

Help!!! I'm putting 125 points on this for a quick and simple response!
0
Question by:zero303
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3
• 2
• +4

LVL 4

Accepted Solution

trkcorp earned 500 total points
ID: 8198840
dim x as long
For x = 1 to 10
If condition_met then Exit For
Next
0

LVL 11

Expert Comment

ID: 8198868
yes, to exit out of for loop, use "exit for" statement. to exit out of do loop, use "exit do" statment...

e.g

For i = 0 to whatever
if(something = SomethingElse) then
exit for
end if
Next i

Good Luck!

0

LVL 11

Expert Comment

ID: 8198887
only problem with VB is that there is no equivalent statement for "continue" in C/C++. to do similar effect you have to use "goto " command...

eg.

in C:

for (i=0; i<10; i++)
{
if (i == 3) continue;
}

in VB:
for i = 0 to 10
if (i = 3) then goto Cont
Cont:
next i

0

LVL 3

Expert Comment

ID: 8198972
Hi,
the equivalent of Break is Exit For
in the case of a For Loop
0

Expert Comment

ID: 8199082
Dim i as integer
Dim whatever as Integer

for i = 0 to whatever-1
if (i = 3) then goto Cont
next i

//code you may want to skip
Cont:

//code to continue with if the for loop exits abruptly

OR

Dim i as integer
Dim whatever as Integer

for i = 0 to whatever-1
if (i = 3) then exit for
next i

if i < whatever then
//code if for loop exits abruptly ie exit for
else
//code to continue with if the for loop completes
end if

Good luck :)
0

LVL 1

Expert Comment

ID: 8199723
For(int i;i < whatever;i++) {
if(something.equals(Something)) {
break;
}
}

To be correct with respect to the request:

i=0 ' Assumed as being null since there's no define.

'Start the loop
Do

'Initial Java test ( i < whatever ) to see if loop code can be run.

If i < whatever Then

'Run Loop Code

if something = Something Then Exit Do

i = i + 1 'Perform i++

End If

Loop Until Not( i < whatever )  'Repeat Java test (i < whatever) to see if loop can continue.

The thing with the initial request is the value starts at Some value and ends when the expression PASSES "whatever" (i++ means work with I, then increase it's value by 1)

So what they're checking for is if I is less than "whatever", but the for/next loops above all start and end at static numbers (meaning you say It must STOP here), possibly confusing to the newcomer, but the above code I placed will work the same as the Java code (in theory, minus the fact there are no real values).  The other problem with the for/next loops, is in Java, the "whatever" can be manipulated inside the loop (to continue it or stop it).  For/next loops across platforms typically have varied results in "playing with the end".
0

LVL 5

Expert Comment

ID: 8200261
you can do it many ways explained

dim i as integer
'Reverse for loop
for i=10 to 0 step -1
if(i=-1)then goto pop 'executes from the specified step
'OR Commenting
if(i=0)then exit for ' Executes very next step after for
next

msgbox "For loop exited by exit for"
pop:
msgbox "For loop exited by goto statement"
0

LVL 1

Expert Comment

ID: 8200828
Actually, goto'ing outside of a for loop is typically a stack nightmare and doing so doesn't "exit" a for loop.

Doing that too many times in one function could land you with an out of memory error.

With Object Orientated Programming, static GoTo's went the way of the Dinos.  Mostly those are used to pass by error routines to get to the end of the function/sub or to pass over subroutines that reduce redundant code (when not useful to the rest of the module).
0

LVL 3

Author Comment

ID: 8201049
Cheers mate - i found it out anyway but a spot on answer anyway so you deserve the points... Ok back to work then, another all nighter coming up i feel!

/me pukes LOL

cheers, Gar
0

LVL 1

Expert Comment

ID: 8203833
Programmers don't need sleep, we just need caffeine.

A good source of caffeine is Jolt Cola.  (But watch out for that sugar explosion, can cause LOTS of bugs.)  :)
0

LVL 4

Expert Comment

ID: 8204551
zero303,
It appears that no one really rovided a do loop example. It would look something like this where x must be <= to even enter the loop:
dim x as long
x = Some_Value
Do While x <= Some_Other_Value
If condition_met then Exit Do
Loop

OR where the value of x is inconsequential to entering the loop and it will execute until (1. x is >= y or a condition satisfies your requirements:
dim x as long
dim y as long
x = 0
y = 10
Do Until x >= y
x = x + 1
If Not condition_met Then
If needs_be_to_extend_the_operation Then y = y + 1
Else
then Exit Do
End If
Loop

Happy Coding!
0

LVL 1

Expert Comment

ID: 8216055
Actually, yes, I did.
0

LVL 4

Expert Comment

ID: 8218130
Well, excuse me FM, I now see that you did.
0

LVL 1

Expert Comment

ID: 8218473
We'll blame it on the small font size.  Yeah, thats it.  All that coding made your vision fuzzy, like logic.  :D
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture. Â  There is also a companion Debug Toolbar that looks like the followinâ€¦
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applicâ€¦
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can launâ€¦
###### Suggested Courses
Course of the Month9 days, 1 hour left to enroll