Upload
arthur-todd
View
224
Download
2
Embed Size (px)
Citation preview
ITM 352 - © Port, Kazman 2
Iteration
• Summary of loops• while-do• for• do-while avoid using this!
• while loop examples• for loop examples
ITM 352 - © Port, Kazman 3
Iteration• Traditional method of repeating statements
(not using recursion)
loop control { --------------- ---------------}
repeat these statementszero or more times,controlled by loop control
ITM 352 - © Port, Kazman 4
Iteration (cont.)
• Three different forms of iteration• while• for• do-while
ITM 352 - © Port, Kazman 5
While loops
while (condition) statement
contains variablesthat are changed
in loop
repeat untilcondition
becomes false
Keep in mind that statement can be a compound or block statement.
ITM 352 - © Port, Kazman 6
For loops
for (init; cond; incr) Statement
init; //for conditionwhile (cond) { Statement incr;}
Mostly for convenience and readability
ITM 352 - © Port, Kazman 8
While 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.
ITM 352 - © Port, Kazman 9
More While loop Examples
$x = 5;$s = 0;while ($x > 0) { $s = $s + $x; $x = $x-1;}
ITM 352 - © Port, Kazman 10
More While loop Examples
$s = 0; $x = 1;while ($x <= 10) { $s = $s + $x; $x = $x + 1;}
ITM 352 - © Port, Kazman 11
Infinite and Empty Loopswhile (true); is an infinite loop and you will have to waita long time! while (false){//stuff} and while ($n++ < 10);
{//stuff} are empty loops and will never execute anything
You should take care to ensure your loop is good by (1) Checking that the initial condition is true(2) Checking that the loop condition will eventually change to
false(3) Do NOT EVER use ; after while()(4) Make sure you initialize loop variables BEFORE the loopGenerally PHP has a maximum execution time to help avoid crashing the system (default 60 secs). You can set this to a lower value such as 5 sec by using set_time_limit (5);
ITM 352 - © Port, Kazman 12
While loop examples
while (true)echo "I will do the reading assignments";
while (false) echo "I will start my HW early";
Output?
Output?
Do Lab Exercise 1
ITM 352 - © Port, Kazman 13
The 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
ITM 352 - © Port, Kazman 14
exit,die example
$x = 5;while ($x++>0) {// terminate program at 3 if($x == 3) die(“done!”); echo “$x<br>”;}echo “I will not execute ”;
ITM 352 - © Port, Kazman 15
break and continue • If you have a situation where need to exit
the loop but not terminate the program use break
• If want to stop executing the statements in the loop for a given cycle and immediately jump to the top of the loop to start the next cycle use continue
ITM 352 - © Port, Kazman 16
break example
$x = 5;while ($x++>0) {// stop early at 3 if($x == 3) break; echo “$x<br>”;}echo “I will always execute”;
ITM 352 - © Port, Kazman 17
continue example
$x = 5;while ($x++>0) {// skip 3 if($x == 3) continue; echo “$x<br>”;}echo “I will always execute”;
Do Lab Exercise 2
ITM 352 - © Port, Kazman 18
for loops• Convenient syntax for loops with loop variables that
are incremented or decremented each time through the loop (as in previous examples).
• Recall:
for (init; cond; incr) statement
init;while (cond) { statement incr;}
ITM 352 - © Port, Kazman 19
for loop examples
Previous examples:
$s = 0; $x = 5;while ($x > 0) { $s = $s + $x; $x = $x-1;}
$s = 0; for ($x = 5; $x > 0; $x = $x-1) $s = $s + $x;
ITM 352 - © Port, Kazman 20
for loop examples
$s = 0; for ($x = 1; $x <= 10; $x = $x+1) $s = $s + $x;
$s = 0; $x = 1;while ($x <= 10) { $s = $s + $x; $x = $x + 1;}
ITM 352 - © Port, Kazman 21
for loop usage
for ($i = 1; $i <= n; $i++) statement
One use of for loops is simply to execute a statement, or sequence of statements, a fixed number of times:
executes statement exactly n times.
ITM 352 - © Port, Kazman 22
for loop usage (cont.)
for ($i = 1; $i <= 100; $i++) echo "I will not turn HW in late");
Example: print "I will not turn HW in late" a hundred times.
ITM 352 - © Port, Kazman 23
for loop usage (cont.)
for ($i = 0; $i < 20; $i++) {if ($i%2 == 1)
echo "My robot loves me\n"; else echo "My robot loves me not\n";}
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:
ITM 352 - © Port, Kazman 24
for loop usage (cont.)
for ($i = 1; $i <= 20; $i++) { echo "\nMy robot loves me"; if ($i%2 == 0) echo " not"; }
An alternate approach. Which is more "elegant"?
Do Lab Exercise #3
ITM 352 - © Port, Kazman 25
Practical 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
ITM 352 - © Port, Kazman 26
Tracing 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.