Darrell Bethea May 18, 2011
1
Darrell Bethea May 18, 2011 1 Later - No Lab! Time to work on - - PowerPoint PPT Presentation
Darrell Bethea May 18, 2011 1 Later - No Lab! Time to work on Programs 1 and 2 independently Program 1 due today Program 2 due Monday O ffj ce hours today 1-2 PM 2 3 Use a switch statement when you have more than 2
1
Later - No Lab!
Program 1 due today Program 2 due Monday Offjce hours today
2
3
Use a switch statement when you have more
Example: Weekdays – if you have a difgerent
4
Use an if-else for all other scenarios:
Example: Grades - each grade (A, B, C, D, F) has
4
Loop - part of program that repeats Body - statements being repeated Iteration - each repetition of body Stopping condition
5
while
do-while
for
6
Also called a while loop A while statement repeats while a
The loop body typically contains an
Evaluate Boolean Expression Execute Body End Loop true false
Syntax
Also called a do-while loop Similar to a while statement, except that
Syntax
Don’t forget the semicolon!
Evaluate Boolean Expression Execute Body End Loop true false
13
First, the loop body is executed. Then the boolean expression is checked.
Equivalent while statement
A for statement executes the body of a
Also known as a for loop. Example
Syntax
Body_Statement can be either a simple
Corresponding while statement
Evaluate Boolean Expression Execute Body End Loop true false Execute Initialization Execute Update Action
Possible to declare variables within a for
Note that variable n is local to the loop
while
do-while
for
6
Problem with program preventing correct
Two most common mistake in loops
23
A loop which repeats without ever ending
If the controlling boolean expression
25
26
27