200 likes | 209 Views
ITM 352 Flow-Control: Loops. Iteration. Traditional method of repeating statements. loop control { --------------- --------------- }. repeat these statements zero or more times, controlled by loop control. Repeating Code.
E N D
Iteration • Traditional method of repeating statements loop control{ --------------- --------------- } repeat these statements zero or more times, controlled by loop control
Repeating Code • A loop statement is a control structure that repeatedly executes a statement or a series of statements while a specific condition is TRUE or until a specific condition becomes TRUE • There are four types of loop statements: • while statements • for statements • do...while statements • foreach statements don't use! will cover later!
while Statements • Tests the condition prior to executing the series of statements at each iteration of the loop • The syntax for the while statement is: while (conditional expression) { statement(s); } • As long as the conditional expression evaluates to TRUE, the statement or command block that follows executes repeatedly
while Statements (continued) • Each repetition of a looping statement is called an iteration • A while statement keeps repeating until its conditional expression evaluates to FALSE • A counter is a variable that increments or decrements with each iteration of a loop statement
while Statements (continued) $Count = 1; while ($Count <= 5) { echo " $Count<br /> "; $Count++; } echo " <p>You have displayed 5 numbers.</p> ";
while Statements (continued) $Count = 10; while ($Count > 0) { echo “$Count<br />”; $Count--; } echo " <p>We have liftoff. </p> ";
while Statements (continued) $Count = 1; while ($Count <= 100) { echo " $Count<br /> "; $Count *= 2; }
while Statements (continued) • In an infinite loop, a loop statement never ends because its conditional expression is never FALSE $Count = 1; while ($Count <= 10) { echo " The number is $Count "; }
for Statements • Combine the initialize, conditional evaluation, and update portions of a loop into a single statement • Repeat a statement or a series of statements as long as a given conditional expression evaluates to TRUE • If the conditional expression evaluates to TRUE, the for statement executes and continues to execute repeatedly until the conditional expression evaluates to FALSE
for Statements • Can also include code that initializes a counter and changes its value with each iteration • The syntax of the for statement is: for (counter declaration and initialization; condition; update statement) { statement(s); }
for Statement Example for ($potato = 1; $potato < 5; $potato++) { if ($potato == 4) echo $potato . "<BR>"; else echo $potato . " potato<BR>"; }
for Statements Example for ($Count = 0; $Count < 5; $Count++) { echo "This is the " . $Count ; switch ($Count) { case '1': echo "st"; break; case '2': echo "nd"; break; case '3': echo "rd"; break; default: echo "th"; } echo " time through the loop <br> "; }
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
The exit Function for ($Count = 0; $Count < 5; $Count++) { echo "Iteration " . $Count . "<BR>"; if ($Count == 3) exit("I'm outta here!"); }
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
break example for ($potato = 1; $potato < 5; $potato++) { if ($potato == 3) break; elseif ($potato == 4) echo $potato . "<BR>"; else echo $potato . " potato<BR>"; }
continue example for ($potato = 1; $potato < 5; $potato++) { if ($potato == 3) continue; elseif ($potato == 4) echo $potato . "<BR>"; else echo $potato . " potato<BR>"; }
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
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.