Link to home
Start Free TrialLog in
Avatar of zero303
zero303

asked on

Easy one for you guys here!

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!
ASKER CERTIFIED SOLUTION
Avatar of trkcorp
trkcorp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of supunr
supunr

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!

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;
    // your code...
}

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

Hi,
the equivalent of Break is Exit For
in the case of a For Loop
Dim i as integer
Dim whatever as Integer

for i = 0 to whatever-1
  if (i = 3) then goto Cont
  ' your code...
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
  ' your code...
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 :)
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".
Avatar of avya2k
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"
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).
Avatar of zero303

ASKER

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
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.)  :)
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!
Actually, yes, I did.
Well, excuse me FM, I now see that you did.
We'll blame it on the small font size.  Yeah, thats it.  All that coding made your vision fuzzy, like logic.  :D