2. Control Structures All applications can be written in terms of: Sequence Structures Selection Structures (or Selection Statements) Repetition Structures (or Repetition Statements) 4/29/2010 M. Campbell - 2010 2 p. 93
4. Sequence Structures Typically built into language Allows statements to be executed one after another Add x and y together. Display the result. 4/29/2010 M. Campbell - 2010 4 p. 93
5. Selection Structures Allows code to be executed based on conditions If the number is even display it Otherwise, do not display it 4/29/2010 M. Campbell - 2010 5
6. Repetition Structures Allows code to be repeated as necessary While the number is less than or equal to 100, display it. Once it is over 100, end the program 4/29/2010 M. Campbell - 2010 6
7. The If…Then Statement Selection Statement: IfconditionThen ' statements go here ElseIfcondition2Then ' more statements Else ' more statements End If 4/29/2010 M. Campbell - 2010 7 p. 93
8. The For Loop Repetition Statement: Forcounter = startToend ' statements go here Nextcounter 4/29/2010 M. Campbell - 2010 8 p. 94
9. The For Loop By default a For loop: Sets counter to the value start Executes the code within the loop body Increments counter Continues looping until counter exceeds the end value Can use Exit For to leave For loop early 4/29/2010 M. Campbell - 2010 9 p. 95
10. The For Loop Can change the step size: Fori = 1 To 10 Step 2 ' code block goes here Nexti Can change the direction: Fori = 1 To 10 Step-5 ' code block goes here Nexti 4/29/2010 M. Campbell - 2010 10 p. 95
11. The For Each Loop Repetition Statement: For EachobjectVarIncollection ' code block goes here NextobjectVar 4/29/2010 M. Campbell - 2010 11 p. 96
12. The For Each Loop Loops through a collection of objects Each iteration places the next object in the variable objectVar 4/29/2010 M. Campbell - 2010 12 p. 96
13. The DoLoop Repetition Statement: Do While condition ' code block goes here Loop Repeats the code block while condition is true 4/29/2010 M. Campbell - 2010 13 p. 96
14. The DoLoop Repetition Statement: Do ' code block goes here Loop While condition Repeats the code block while condition is true Will execute at least once (as condition is checked last) 4/29/2010 M. Campbell - 2010 14 p. 96
15. The DoLoop Repetition Statement: Do Until condition ' code block goes here Loop Repeats the code block until condition becomes true 4/29/2010 M. Campbell - 2010 15 p. 96
16. The Select CaseStatement Selection Statement: Select Case testVar Case value1 ' code to execute if testVar = value1 Case value2 ' code to execute if testVar= value2 Case Else ' statements to execute otherwise End Select 4/29/2010 M. Campbell - 2010 16