Explain about FLEXIBLE Loops

     

  • Condtion: Is a logical expression for Loop determinant
  • Statement: Body of Loop
  • VBA checks determinant conditon before executing the body of loop
  •  

  • WHILE indicates that: VBA execute the loops, as long as condition mentioned in Logical expresssion is TRUE
  •  

  • If condition is TRUE, then only body executes
  • If condition is false, then execution of loop stops
  •  

     

    Process:

     

  • First test the Logical Expression
  • If it is TRUE executes the statement
  • Upon reaching of key word LOOP, again checks the condition is true or false
  • If it is TRUE against executes the loop, it it is false it stops the execution
  •  

    Do Loops - Flexible Loops

     

  • Like For Loop, Do loop's iteration doesn't fixed
  •  

  • Iterations of DO Loops entirely depends on satiation of Loop Determinant Condition
  •  

  • We can classify DO Loops in 4 ways based on Testing of Loop Determinant Condition
  •  

     

    • Do ----while Loop
    • Do ----until Loop
    •  

      Do ----while Loop

       

    • (i)Testing condition before executing the body of the Loop
    •  

    • (ii)Testing condition after executing the body of the Loop
    •  

      Do ----Until Loop

       

    • (i)Testing condition before executing the body of the Loop
    •  

    • (ii)Testing condition after executing the body of the Loop
    •  

     

  • Condition: represents to Logical Expression for Loop determinant
  • Statements: represents to Body of Loop
  •  

    What is the difference between WHILE loop and UNTIL loop:

     

     

  • Do - While Loop executes when condition is TRUE
  • Do - While Loop doesn't execute when condition is FALSE
  • DO WHILE LOOP EXECUTES AS LONG AS CONDITION IS TRUE

     

     

  • Do - Until Loop executes when condition is FALSE
  • Do - Until Loop doesn't execute when condition is TRUE
  • DO UNTIL LOOP EXECUTES UNTIL CONDITION IS TRUE

     

     

    Do Loops - Condition Execution After Loop

     

     

    Testing Loop Determinant - before execution of Body

     

     

     

     

    Do While Loop:

     

  • Condition: represents to Logical Expression for Loop determinant
  •  

  • Statements: represents to Body of Loop
  •  

  • If Logical expression is TRUE, then VBA executes the body
  •  

  • If Logical expression is FLASE, then VBA stops to executes the body
  •  

  • Loops iterates untill the condition is FALSE
  •  

  • As condition exists before the statements, sometimes Loop doesn't execute at least once(based on satiation of condition)
  •  

    Print Numbers upto 10

     

    Do Until Loop

     

     

    Do While Loop

     

     

     

    Do Until Loop - Example

     

    Private Sub CommandButton1_Click()
    Dim i As Integer
    i = 1
    Do Until Range("A" & i).Value = 55
    Range("A" & i).Interior.ColorIndex = 3
    i = i + 1
    Loop
    End Sub

     

     

     

    Do Until Loop - Example

     

    Private Sub CommandButton1_Click()
    Dim i As Integer
    i = 1
    Do While Range("A" & i).Value <> 55
    Range("A" & i).Interior.ColorIndex = 3
    i = i + 1
    Loop
    End Sub

     

    Testing Loop Determinant - After execution of Body

     

     

  • As condition exists after the statement, Loop executes at least once
  •  

     

     

     

    Exit For Loop: Print Numbers

     

    Private Sub CommandButton1_Click()
    Dim i As Integer
    For i = 1 To 10
    Cells(i, 1) = i
    If Cells(i, 1) = 5 Then
    Exit For
    End If
    Next
    End Sub

     

    Download The Workbook