Download presentation
Presentation is loading. Please wait.
Published byAdelia Lawson Modified over 9 years ago
2
LOOPING What are the 3 structures of writing code? sequential decision repetition Def. Looping is the repetition of statements in a program. There various types of loops: controlled by a flag, count controlled, state controlled
3
Loop controlled by a boolean flag* and a pretest The flow of logic of a loop is shown in a flowchart: end flag = false !flag == ? true body of loopupdate flag false start *sometimes called a sentinel
4
boolean done = false;//initialization step while ( ! done )//loop condition { statements //body of loop... If (something) done = true; } //update flag based //on some decision //sometimes from user NOTE: this is a pretest loop the body is executed only when the loop condition is TRUE. See ex. p.241
5
Loop controlled by a boolean flag and a posttest //no initialization necessary do {statements;... update flag; } while ( flag ); Note: that means flag is true Note: the test is not made until the body has executed once.
6
Example of a do - while loop : char ch; boolean ok; do { ch = getChar(“Enter a digit”); if (ch ‘9’) ok = false; } while (! ok );
7
Count controlled loops int lcv = 0; // initialization while ( lcv < 10 ) // final value in condition {println ( “ HI! ” ); // body of loop lcv++; // or lcv = lcv + 1 (update) } Note: each execution of the body of the loop is called one iteration. Is this a pre or posttest loop? What change would make it zero iterations? pretest lcv = 10
8
Write a loop that will output the integers 1 to 10 Write a loop that will output the integers 10 to 1.
9
Summation is a common use of looping. Let’s write a loop to find the sum of the integer 1 to 10. int sum = 0; // called the accumulator int count = 0; // initialization of loop control variable while ( count < 10 ) // loop condition {sum = sum + count; // or sum += count count ++; // or count = count + 1 } //end while
10
State controlled loops (similar to flag-controlled) The loop is ended when a state is reached that causes the loop-repetition condition to become false. See ex bottom p 249 See ex bottom p 261 Question: What is the difference between a sentinel-controlled loop and a state-controlled loop? A sentinel is a “dummy data value” an end marker. A state is reached that causes the boolean condition to become false.
11
for loops Note: this is an option that most languages provide to condense code but is not necessary. for ( lcv = initial value; lcv < final value; lcv ++ ) Notice: how 3 of the 4 parts of a loop are in this line. Rewrite the while “count to 10” loop using for. Question: can every while loop be written as a for loop? No. Only when the number of repetitions is known ahead of time. Therefore, not a flag controlled and only some state controlled loops. Question: does the initial value have to be 1?no How many iterations of this loop? For (ct = 10; ct <= 20; ct++) { };
12
What does this loop do? for (int x = 0; x < 32,768; x++); nothing but count to 32,768 - this provides a pause. (It is also a common error: do not put a semi- colon after the parentheses unless you want a loop that does nothing.)
13
What does this do? For (ct = 10; ct != 20; ct +=3 ) { println ( “Hi!”); } Trace: ctoutput 10Hi 13Hi 16Hi 19Hi 22Hi Caution: infinite loop
14
Can the lcv be any type besides int? yes. Any ordinal type: any primitve data type whose elements can be listed in sequential order. (short, int, long, char) Is a for loop a pretest loop, posttest loop or neither? Pretest. Write a loop whose body will never be executed.
15
What does this loop do: for ( char ch = ‘A’; ch <= ‘Z’; ch++) displayResult(ch + “ “ + (int) ch); A 65 B 66 … Z 90
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.