1 / 20

ITM 352 Flow-Control: Loops

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.

Download Presentation

ITM 352 Flow-Control: Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITM 352Flow-Control: Loops

  2. Iteration • Traditional method of repeating statements loop control{ --------------- --------------- } repeat these statements zero or more times, controlled by loop control

  3. 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!

  4. 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

  5. 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

  6. while Statements (continued) $Count = 1; while ($Count <= 5) { echo " $Count<br /> "; $Count++; } echo " <p>You have displayed 5 numbers.</p> ";

  7. while Statements (continued) $Count = 10; while ($Count > 0) { echo “$Count<br />”; $Count--; } echo " <p>We have liftoff. </p> ";

  8. while Statements (continued) $Count = 1; while ($Count <= 100) { echo " $Count<br /> "; $Count *= 2; }

  9. 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 "; }

  10. 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

  11. 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); }

  12. for Statement Example for ($potato = 1; $potato < 5; $potato++) { if ($potato == 4) echo $potato . "<BR>"; else echo $potato . " potato<BR>"; }

  13. 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> "; }

  14. 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

  15. The exit Function for ($Count = 0; $Count < 5; $Count++) { echo "Iteration " . $Count . "<BR>"; if ($Count == 3) exit("I'm outta here!"); }

  16. 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

  17. break example for ($potato = 1; $potato < 5; $potato++) { if ($potato == 3) break; elseif ($potato == 4) echo $potato . "<BR>"; else echo $potato . " potato<BR>"; }

  18. continue example for ($potato = 1; $potato < 5; $potato++) { if ($potato == 3) continue; elseif ($potato == 4) echo $potato . "<BR>"; else echo $potato . " potato<BR>"; }

  19. 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

  20. 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.

More Related