Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

solution ,how to exit current iteration and continue with next on a condi in For Loop in VBS

Status
Not open for further replies.

QTPShann

Programmer
Apr 12, 2013
1
0
0
US
Hi Friends here is how we can exit the current iteration on a condition and continue with next iteration with a for loop in VB Scripting. Like Continue in VB.

For i=0 to 10
Do
IF i<5 Then
Exit DO
End IF
Statement 1
Statement 2
Statement 3
Statement 4
Exit Do
Loop
Next

for the first 5 iterations, Statements 1 thro 4 will not be executed. Next 6 iterations, Statements 1 thro 4 will be executed.
 
Yes .... and do you have a question???

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Why do you have a Do...Loop inside the Foe...Next loop?

You simply need an appropriate If statement!

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Why don't you simply loop like this:
Code:
for i = 5 to 10
?

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
@MakeItSo - While your code is an accurate simplification of the OP, it is only circumstantial. I believe the OP was trying to suggest an example solution for a problem he encountered - the values used are likely arbitrary.

@QTPShann, what Skipvought said is true, "Why do you have a Do...Loop inside the Foe...Next loop?" Remove then do..loop and move the statements inside the if..then and you eliminate 5 lines of superfluous code and improve resource cost.

Code:
for i = 0 to 10
   if (i < 5) then
      Statement 1
      Statement 2
      Statement 3
      Statement 4
   end if
next

-Geates

 
@MakeItSo - While your code is an accurate simplification of the OP, it is only circumstantial. I believe the OP was trying to suggest an example solution for a problem he encountered - the values used are likely arbitrary
Often the best solution to a problem is to avoid the problem!
If the problem of the OP is different to the one stated, he should rather post information relevant to his problem! Then the best solution might be not solving his problem but avoiding it in the first place by implementing proper logic!
Your solution of creating a loop for 0 to 10 and then executing something in the first 5 cases and ignoring the rest is not the way to go IMHO.

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top