Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default Do While Vs Do Until

Which of these should break out of a For Next Loop when x = 1?
I think I'm getting the same action either way. I want it to break when
x = 1 and skip the rest of the For/Next

x = 0
Do While x = 0
For i = 1 to 10
If val1 = val2 Then
x = 1
End If
Next i
Loop

x = 0
Do Until x = 1
For i = 1 to 10
If val1 = val2 Then
x = 1
End If
Next i
Loop

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Do While Vs Do Until

marston,

You're understanding is correct. "Do While" & "Do Until" are both pre-tested
loops.

To get a post-tested loop use "Loop While" or "Loop Until"

examples:-

x = 0
Do
For i = 1 to 10
If val1 = val2 Then
x = 1
End If
Next i
LoopWhile x = 0

x = 0
Do
For i = 1 to 10
If val1 = val2 Then
x = 1
End If
Next i
Loop Until x = 1
======================

In your example you only need the following if you're just trying to exit
the for loop.

For i = 1 To 10
If val1 = val2 Then Exit For

Next i

regards,

JohnI


" wrote in message
oups.com...
Which of these should break out of a For Next Loop when x = 1?
I think I'm getting the same action either way. I want it to break when
x = 1 and skip the rest of the For/Next

x = 0
Do While x = 0
For i = 1 to 10
If val1 = val2 Then
x = 1
End If
Next i
Loop

x = 0
Do Until x = 1
For i = 1 to 10
If val1 = val2 Then
x = 1
End If
Next i
Loop



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Do While Vs Do Until

is the do While doing anything or is that your attempt to break out of the
for next loop. If so, the do while or do unil or loop While or loop until
are all external to the for next loop and would have no effect (conditions
are checked at the Do or Loop line). If you want to break out of the for
next loop

for i = 1 to 10
if val1 = val2 then
exit for
end if
Next

--
Regards,
Tom Ogilvy



" wrote in message
oups.com...
Which of these should break out of a For Next Loop when x = 1?
I think I'm getting the same action either way. I want it to break when
x = 1 and skip the rest of the For/Next

x = 0
Do While x = 0
For i = 1 to 10
If val1 = val2 Then
x = 1
End If
Next i
Loop

x = 0
Do Until x = 1
For i = 1 to 10
If val1 = val2 Then
x = 1
End If
Next i
Loop



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:41 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"