Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.

Similar presentations


Presentation on theme: "1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations."— Presentation transcript:

1 1 Week 9 Loops

2 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations »counting loops »sentinel-controlled loops »infinite loops »minimum of zero or minimum of one iteration l Several programming statement variations »while »do-while »for

3 3 while Loop l Syntax: while(Boolean_Expression) { //body of loop First_Statement;... Last_Statement; } l Initialization statements usually precede the loop. Boolean_Expression is the loop termination condition. The loop will continue executing as long as Boolean_Expression is true. l May be either counting or sentinel loop »Good choice for sentinel loop Something in body of loop should eventually cause Boolean_Expression to be false.

4 4 Semantics of the while Statement while (Boolean_Expression) Body Start Evaluate Boolean_Expression End loop false Execute Body true

5 5 while : A Counting Loop Example l A loop to sum 10 numbers entered by user int next; //Loop initialization int count = 1; int total =0; while (count <= 10) //Termination condition { //Body of loop next = Integer.parseInt(field.getText().trim()); total = total + next; count++; //Loop termination counter }

6 6 while : A Sentinel Controlled Loop Example l A loop to sum positive integers entered by the user next is the sentinel l The loop terminates when the user enters a negative number //Initialization int next = 0; int total = 0; while(next >= 0) //Termination condition { //Body total += next; next = Integer.parseInt(field.getText().trim()); }

7 7 while : A Minimum of Zero Iterations Because the first input value read and the test precedes the loop, the body of the while loop body may not execute at all //Initialization int next; int total = 0; next = Integer.parseInt(field.getText().trim()); while(next >= 0) //Termination condition { total = total + next; next = Integer.parseInt(field.getText().trim()); } l If the first number the user enters is negative, the loop body never executes

8 8 do-while Loop Syntax do { //body of loop First_Statement;... Last_Statement; } while(Boolean_Expression); l Initialization code may precede loop body l Loop test is after loop body so the body must execute at least once (minimum of at least one iteration) l May be either counting or sentinel loop »Good choice for sentinel loop Something in body of loop should eventually cause Boolean_Expression to be false.

9 9 Semantics of the do-while Statement do Body while (Boolean_Expression); Start Evaluate Boolean_Expression End loop false Execute Body true Execute Body

10 10 do-while Example int count = 1; int number = 5; do //Display integers 1 to 5 on one line { field.appendText(count + " "); count++; } while(count <= number); Note that field.appendText() is used and not field.setText() so the numbers will all be in the TextArea together. Output: 1 2 3 4 5

11 11 for Loop l Good choice for counting loop l Initialization, loop test, and loop counter change are part of the syntax l Syntax: for(Initialization; Boolean_Expression; Update_Action) loop body;

12 12 Semantics of the for Statement for(Initialization; Boolean_Expression; Update_Action) loop body; Start Evaluate Boolean_Expression End loop false Execute Body true Execute Initialization Execute Update_Action

13 13 for Example l Count down from 3 to 1 for(int count = 3; count >= 1; count--) { field.appendText("T = " + count); field.appendText(" and counting\n"); } field.appendText("Blast off!"); Output: T = 3 and counting T = 2 and counting T = 1 and counting Blast off!

14 14 Nested Loops l The body of a loop can have any kind of statements, including another loop. l Each time the outer loop body is executed, the inner loop body will execute 5 times, making a total of 20 times. for (line = 0; line < 4; line++) { for (star = 0; star < 5; star++) field.appendText('*'); field.appendText(‘\n'); } body of inner loop body of outer loop Output: *****

15 15 Some Practical Considerations When Using Loops l The most common loop errors are unintended infinite loops and off-by-one errors in counting loops. l Sooner or later everyone writes an unintentional infinite loop l Loops should be tested thoroughly, especially at the boundaries of the loop test, to check for off-by-one and other possible errors.

16 16 Tracing a Variable in a Loop l Tracing a variable: display the variable each time through the loop l A common technique is to test loop counters and troubleshoot off-by-one and other loop errors. l Some systems provide a built-in tracing system that allows you to trace a variable without having to change your program. l If no built-in utility is available, insert temporary output statements to display values.


Download ppt "1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations."

Similar presentations


Ads by Google