19  Control Flow

Control flow is the order in which individual statements, instructions, or function calls of an imperative program are executed or evaluated. The control flow of a program is regulated by control structures. These control structures can be categorized into three main types: sequential, conditional, and loop structures.

19.1 Sequential Structure

graph LR
  Start((Start)) --> Statement1[Statement 1]
  Statement1 --> Statement2[Statement 2]
  Statement2 --"..."--> Statement3[Statement N]
  Statement3 --> End((End))

19.2 Conditional Structures

19.2.1 If Statement

graph LR
    Start((Start)) -->in[/n/]
    n --> ifOdd{n Mod 2 = 1?}
    ifOdd -- True --> printOdd[Odd]
    printOdd --> ifEven
    ifOdd -- False --> ifEven{n Mod 2 = 0?}
    ifEven -- True --> printEven[Even]
    ifEven -- False --> End((End))
    printEven --> End

19.2.2 If Else Statement

graph LR
    Start((Start)) --> in[/n/]
    in --> ifOdd{n Mod 2 = 1?}
    ifOdd -- True --> printOdd[Odd]
  
    ifOdd -- False --> printEven[Even]

    printOdd --> End((End))
    printEven --> End

19.2.3 If ElseIf Statement

graph LR
    Start((Start))-->input[/score/]
    input-->ifA{score >= 90?}
    ifA -- True --> printA[/A/]
    ifA -- False --> ifB{score >= 80?}
    ifB -- True --> printB[/B/]
    ifB -- False --> ifC{score >= 70?}
    ifC -- True --> printC[/C/]
    ifC -- False --> ifD{score >= 60?}
    ifD -- True --> printD[/D/]
    ifD -- False --> printF[/F/]
    printA --> End((End))
    printB --> End((End))
    printC --> End((End))   
    printD --> End((End))
    printF --> End((End))
  

19.2.4 Select Case Statement

graph LR
    Start((Start)) --> Case["`Select Case
    **Grade**`"]
    Case-->ifA{Case A}
    ifA -- True --> printA[Excellent!]
    ifA -- False --> ifB{Case B}
    ifB -- True --> printB[Well done!]
    ifB -- False --> ifC{Case C}
    ifC -- True --> printC[Good job.]
    ifC -- False --> ifD{Case Else}
    ifD --> printD[Needs improvement]
    printA --> End((End))
    printB --> End((End))
    printC --> End((End))   
    printD --> End((End))
  

19.3 Loop Structures

19.3.1 Do While Loop

graph LR
  Start((Start)) --> LoopStart[Initialize<br>iteration variable]
  LoopStart --> While[Do While]
  While-->Condition{Condition}
  Condition--True-->Statement1["`**Loop Body**
  Statement 1
  Statement 2
  ...
  Statement N
  _(Update iteration variable)_`"]
  Statement1--->Condition
  Condition ---->|False| End((End))

Example:

Dim n as Integer
n = 1
Do While n < 5
    Debug.Print n
    n = n + 1
Loop

19.3.2 Do Until Loop

graph LR
  Start((Start)) -->LoopStart[Initialize<br>iteration variable]
  LoopStart --> Until[Do Until]
  Until-->Condition{Condition}
  Condition--False-->Statement1["`**Loop Body**
  Statement 1
  Statement 2
  ...
  Statement N
  _(Update iteration variable)_`"]
  Statement1--->Condition
  Condition ---->|True| End((End))

Example:

Dim n as Integer
n = 1
Do Until n > 5
    Debug.Print n
    n = n + 1
Loop

19.3.3 Do Loop While

graph LR
  Start((Start)) --> LoopStart[Initialize<br>iteration variable]
  LoopStart --> While[Do]
  While-->Statement1["`**Loop Body**
  Statement 1
  Statement 2
  ...
  Statement N
  _(Update iteration variable)_`"]
  Statement1--->Condition -->|True| LoopStart
  Condition{While<br>Condition} -->|False| End((End))

Example:

Dim n as Integer
n = 1
Do
    Debug.Print n
    n = n + 1
Loop While n <= 5

19.3.4 Do Loop Until

graph LR
  Start((Start)) --> LoopStart[Initialize<br>iteration variable]
  LoopStart --> Do
  Do -->Statement1["`**Loop Body**
  Statement 1
  Statement 2
  ...
  Statement N
  _(Update iteration variable)_`"]
  Statement1-->Condition -->|False| Do
  Condition{Until<br>Condition} -->|True| End((End))

Example:

Dim n as Integer
n = 1
Do
    Debug.Print n
    n = n + 1
Loop Until n > 5

19.3.5 For Loop Next

graph LR
  Start((Start))-->For
  For --> LoopStart[Initialize<br>iteration variable]
    LoopStart-->Condition{Condition}
  Condition-->|True|Statement1["`**Loop Body**
  Statement 1
  Statement 2
  ...
  Statement N`"]
  Statement1 --> ForNext["Update iteration
                          variable"]
  ForNext --> Condition
  Condition{Condition} ---->|False| End((End))

Example:

Dim n as Integer
n = 1
For n = 1 To n <= 5 Step 1
    Debug.Print n
Next n

19.3.6 For Each Next

graph LR
  Start((Start))-->LoopStart["Initialize<br>collection"]
  LoopStart-->For[For Each]
  For-->Condition{"Next element 
  in collection?"}
  Condition--->|True|Statement1["`**Loop Body**
  Statement 1
  Statement 2
  ...
  Statement N`"]
  Statement1--->Condition
  Condition---->|False| End((End))

Example:

'Creating collection
Dim numbers As Collection
Set numbers = New Collection

'Populating collection
numbers.Add 1
numbers.Add 2
numbers.Add 3
numbers.Add 4
numbers.Add 5

Dim n As Variant
For Each n in numbers
    Debug.Print n
Next n