Title: Changes Made
1Changes Made
- Fixed smart quotes
- Changed title
- COUNT 21 slides (not including title)
2ITM 352Flow-Control LoopsLecture 8
3Iteration
- Summary of loops
- while-do
- for
- do-while
- while loop examples
- for loop examples
4Iteration
- Traditional method of repeating statements (not
using recursion)
loop control ---------------
---------------
repeat these statements zero or more
times, controlled by loop control
5Iteration (cont.)
- Three different forms of iteration
- while
- for
- do-while
6While loops
contains variables that are changed in loop
while (condition) statement
repeat until condition becomes false
Keep in mind that statement can be a compound or
block statement.
7For loops
init //for condition while (cond)
Statement incr
?
for (init cond incr) Statement
Mostly for convenience and readability
8Do-while loops
Statement while (cond) Statement
do Statement while (cond)
?
9While loops
- Syntax while (cond) statement
- What it means
- Test cond.If false, then while loop is
finished go on to next statementIf true,
execute statement, then go back and start
again - Continue like this until cond becomes false or
a break is executed.
10More while loop examples
x 5 while (xgt0) echo x x x-1
11More while loop examples
x 5 s 0 while (x gt 0) s s x
x x-1
12More while loop examples
s 0 x 1 while (x lt 10) s s
x x x 1
13While loop examples
while (true) echo "I will do the reading
assignments"
Output?
while (false) echo "I will start my HW early"
Output?
Do lab exercise 1
14The exit Function
- If you have a situation where it is pointless to
continue execution you can terminate the program
with the exit() or die() functions - A string is often used as an argument to identify
if the program ended normally or abnormally
Do lab exercise 2
15for loops
- Convenient syntax for loops with loop variables
that are incremented or decremented each time
through the loop (as in previous examples). - Recall
init while (cond) statement incr
?
for (init cond incr) statement
16for loop examples
s 0 x 5 while (x gt 0) s s
x x x-1
s 0 for (x 5 x gt 0 x x-1)
s s x
?
17for loop examples
s 0 x 1 while (x lt 10) s s
x x x 1
s 0 for (x 1 x lt 10 x
x1) s s x
?
18for loop usage
One use of for loops is simply to execute a
statement, or sequence of statements, a fixed
number of times
for (i 1 i lt n i) statement
executes statement exactly n times.
19for loop usage (cont.)
Example print I will not turn HW in late a
hundred times.
for (i 1 i lt 100 i) echo "I will not
turn HW in late")
20for loop usage (cont.)
Keep in mind that loop bodies can be as
complicated as you like. This loop prints My
robot loves me and My robot loves me not on
alternate lines, ten times each
for (i 0 i lt 20 i) if (i2
1) echo "My robot loves me\n" else
echo "My robot loves me not\n"
21for loop usage (cont.)
An alternate approach. Which is more elegant?
for (i 1 i lt 20 i) echo "\nMy
robot loves me" if (i2 0) echo "
not"
Do lab exercise 3
22Practical ConsiderationsWhen Using Loops
- The most common loop errors are unintended
infinite loops and off-by-one errors in counting
loops - An off-by-one error is an error that occurs when
a loop is executed one too many or one too few
times. - Sooner or later everyone writes an unintentional
infinite loop - To get out of an unintended infinite loop enter
C (control-C) or stop button on browser - Loops should tested thoroughly, especially at the
boundaries of the loop test, to check for
off-by-one and other possible errors
23Tracing a Variable in a Loop
- Tracing a variable print out the variable each
time through the loop - A common technique to test loop counters and
troubleshoot off-by-one and other loop errors - Some systems provide a built-in tracing system
that allows you to trace a variable without
having to change your program. - If no built-in utility is available, insert
temporary output statements to print values.