Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITM 352 - © Port, Kazman1 ITM 352 Flow-Control: Loops Lecture #8.

Similar presentations


Presentation on theme: "ITM 352 - © Port, Kazman1 ITM 352 Flow-Control: Loops Lecture #8."— Presentation transcript:

1 ITM 352 - © Port, Kazman1 ITM 352 Flow-Control: Loops Lecture #8

2 ITM 352 - © Port, Kazman2 Iteration Summary of loops while-do for do-while  avoid using this! while loop examples for loop examples

3 ITM 352 - © Port, Kazman3 Iteration Traditional method of repeating statements (not using recursion) loop control { --------------- } repeat these statements zero or more times, controlled by loop control

4 ITM 352 - © Port, Kazman4 Iteration (cont.) Three different forms of iteration while for do-while

5 ITM 352 - © Port, Kazman5 While loops while ( 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.

6 ITM 352 - © Port, Kazman6 For loops for ( init ; cond ; incr ) Statement init ; //for condition while ( cond ) { Statement incr ; }  Mostly for convenience and readability

7 ITM 352 - © Port, Kazman7 Do-while loops do Statement while ( cond ) Statement ; while ( cond ) Statement 

8 ITM 352 - © Port, Kazman8 While loops 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.

9 ITM 352 - © Port, Kazman9 More While loop Examples $x = 5; $s = 0; while ($x > 0) { $s = $s + $x; $x = $x-1; }

10 ITM 352 - © Port, Kazman10 More While loop Examples $s = 0; $x = 1; while ($x <= 10) { $s = $s + $x; $x = $x + 1; }

11 ITM 352 - © Port, Kazman11 Infinite and Empty 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);

12 ITM 352 - © Port, Kazman12 While loop examples while (true) echo "I will do the reading assignments"; while (false) echo "I will start my HW early"; Output? Do Lab Exercise 1

13 ITM 352 - © Port, Kazman13 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

14 ITM 352 - © Port, Kazman14 exit,die example $x = 5; while ($x++>0) { // terminate program at 3 if($x == 3) die(“done!”); echo “$x ”; } echo “I will not execute  ”;

15 ITM 352 - © Port, Kazman15 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

16 ITM 352 - © Port, Kazman16 break example $x = 5; while ($x++>0) { // stop early at 3 if($x == 3) break; echo “$x ”; } echo “I will always execute”;

17 ITM 352 - © Port, Kazman17 continue example $x = 5; while ($x++>0) { // skip 3 if($x == 3) continue; echo “$x ”; } echo “I will always execute”; Do Lab Exercise 2

18 ITM 352 - © Port, Kazman18 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 ; } 

19 ITM 352 - © Port, Kazman19 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; 

20 ITM 352 - © Port, Kazman20 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; }

21 ITM 352 - © Port, Kazman21 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.

22 ITM 352 - © Port, Kazman22 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.

23 ITM 352 - © Port, Kazman23 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:

24 ITM 352 - © Port, Kazman24 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

25 ITM 352 - © Port, Kazman25 Practical Considerations When 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

26 ITM 352 - © Port, Kazman26 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.


Download ppt "ITM 352 - © Port, Kazman1 ITM 352 Flow-Control: Loops Lecture #8."

Similar presentations


Ads by Google