method of repeating statements (not using recursion) loop control { --------------- --------------- } repeat these statements zero or more times, controlled by loop control
(condition) statement contains variables that are changed in loop repeat until condition becomes false Keep in mind that statement can be a compound or block statement.
Syntax: while (cond) statement • What it means: Test cond. If false, then while loop is finished; go on to next statement If true, execute statement, then go back and start again Continue like this until cond becomes false or a break is executed.
Loops while (true); is an infinite loop and you will have to wait a 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 loop Generally 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);
• 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
• 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
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; } ≡
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.
(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:
(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
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
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.