?
Solved

Easy one for you guys here!

Posted on 2003-03-24
14
Medium Priority
?
150 Views
Last Modified: 2010-05-01
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
Comment
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
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +4
14 Comments
 
LVL 4

Accepted Solution

by:
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

by:supunr
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

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

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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

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

Expert Comment

by:tiseffies
ID: 8199082
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 :)
0
 
LVL 1

Expert Comment

by:FunkyMeister
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

by:avya2k
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

by:FunkyMeister
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

by:zero303
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

by:FunkyMeister
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

by:trkcorp
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

by:FunkyMeister
ID: 8216055
Actually, yes, I did.
0
 
LVL 4

Expert Comment

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

Expert Comment

by:FunkyMeister
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question