Conditional Looping

Conditional Looping or Repetitive Looping is where a set of statements are executed again and again if a given condition holds true. These are very helpful for developing logic and if it were not for the conditional loops, programming codes would be much longer.

FOR NEXT

The FOR NEXT loop is used to run a set of statements for a specified number of times. The FOR statements specifies a counter variable and its start & end values. The NEXT statement increases the counter.

Syntax of FOR NEXT loop: FOR counter = start TO end [STEP]
  (Statements)
NEXT [counter]
An example of FOR NEXT loop: FOR I = 0 TO 10
  Msgbox "The number is " & I
  Msgbox "The square of the number is " & I * I
  Msgbox "The cube of the number is " & I * I * I
NEXT I

Using STEP in the FOR NEXT loop

With the help of STEP, you can increase or decrease the counter variable by the value you specify.FOR I = 0 TO 10 STEP 2
  Msgbox "The number is " & i
  Msgbox "The square of the number is " & I * I
  Msgbox "The cube of the number is " & I * I * I
NEXT I

FOR I = 10 TO 0 STEP -2
  Msgbox "The number is " & I
  Msgbox "The square of the number is " & I * I
  Msgbox "The cube of the number is " & I * I * I
NEXT I

DO WHILE LOOP

The DO WHILE LOOP repeats a block of statements while a condition holds true.

Syntax of DO WHILE LOOP: DO WHILE condition(s)
  (statements)
LOOP

Or

DO
  (statements)
LOOP WHILE condition(s)
An example of DO WHILE LOOP:Marks = 0
DO WHILE Marks < 100
  Marks = Marks + 10
LOOP
Msgbox "The Marks are " & Marks
'The output will be: The Marks are 100

DO
  Marks = Marks + 10
LOOP WHILE Marks < 100
Msgbox "The Marks are " & Marks
'The output will be: The Marks are 110)
The above example uses both DO WHILE LOOP and DO LOOP WHILE cases. The main difference is that in latter case, the set of statements is run at least once before evaluating the condition. So, in the above example, you would have noticed that even though the marks have reached 100, the "Marks = Marks + 10" is still executed and Marks become 110. After executing that statement, the condition of WHILE Marks <100 is evaluated and found to be false. So the conditional loop is exited.

DO UNTIL LOOP

The DO UNTIL LOOP repeats a block of statements until a condition becomes true.

Syntax of DO UNTIL LOOP: DO UNTIL condition(s)
  (statements)
LOOP

Or

DO
  (statements)
LOOP UNTIL condition(s)
An example of DO UNTIL LOOP:Marks = 0
DO UNTIL Marks >= 100
  Marks = Marks + 10
LOOP
Msgbox "The Marks are " & Marks
'The output will be: The Marks are 100

DO
  Marks = Marks + 10
LOOP UNTIL Marks >= 100
Msgbox "The Marks are " & Marks
'The output will be: The Marks are 110
Now, if you are attentive enough, you will realize that the output for both the DO WHILE & DO UNTIL examples above is exactly the same! At the same time, the structure looks very similar, but if you look closely, you will notice the tiny difference of the comparison operators used in the above examples. The comparison operators can be very tricky at times and you tend to have pitfalls in the code if you use them incorrectly. The DO WHILE & DO UNTIL LOOPS are very useful part of conditional looping and help in building required programming logic.

USING NESTED LOOPS

Just like the NESTED IF statement, you can have a NESTED DO WHILE or a NESTED FOR NEXT loop. These NESTED statements can get a bit complex, but if used smartly, they can give you good results.



Below is an example of a NESTED FOR Loop. There are 5 Harry Potter characters in a string array variable named HP_Characters. Our aim is to sort these characters in ascending order and display them in a message box.Dim HP_Characters(5) As String, Temp_Character As String
HP_Characters(1) = "Harry Potter"
HP_Characters(2) = "Albus Dumbledore"
HP_Characters(3) = "Ronald Weasley"
HP_Characters(4) = "Sirius Black"
HP_Characters(5) = "Hermoine Granger"

FOR I = 1 To 5
  FOR J = I To 5
    IF HP_Characters(I) > HP_Characters(J) Then
      Temp_Character = HP_Characters(I)
      HP_Characters(I) = HP_Characters(J)
      HP_Characters(J) = Temp_Character
    End If
  NEXT J
NEXT I
MsgBox HP_Characters(1) & " " & HP_Characters(2) & " " & HP_Characters(3) & " " & HP_Characters(4) & " " & HP_Characters(5)
In conditional looping DO LOOPS work well when you do not know in advance how many times you need to execute the statements in the loop. However, when you expect to execute the loop a specific number of times, a FOR NEXT loop is a better choice.

An important thing to note here is that conditional looping has the potential to become an infinite loop when nothing in the loop's statements can affect the outcome of the loop's condition. However, infinite loops can sometimes be used purposely, often with an exit statement. In the above loops, you can use EXIT DO and EXIT FOR to come out of the loop unconditionally.

That's pretty much all about the basics of conditional looping and how these are used to build programming logic.

Back to Visual Basic for Beginners