Upcoming SlideShare
×

# Unit 8: Control Statements

702 views

Published on

Published in: Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
702
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
8
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Unit 8: Control Statements

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