repetition. control of flow sequence selection (if..else, switch…case) repetition

29
Repetition

Upload: linda-wiggins

Post on 05-Jan-2016

238 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Repetition

Page 2: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Control of Flow

SEQUENCE

SELECTION (if..else, switch…case)

REPETITION

Page 3: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Repetition- Used to repeat one or more programming

statements.

while Statement do-while Statement for Statement

Page 4: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

The while Statement

In cooking, a recipe may tell us that, as there are lumps in a sauce we should stir it.

In Java:

While (there are lumps)

give sauce a stir;

Page 5: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

The while Statement

Loop while loops continue to loop while some condition is true, and stops when the condition becomes false

The condition is a Boolean expression

Will never execute if the condition is initially false

Page 6: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

while ( number <= 100 ) {

sum = sum + number;

number = number + 1;

}

Syntax for the while Statementwhile ( <boolean expression> ) {

<statement>

}

Statement(loop body)

Statement(loop body)

Boolean ExpressionBoolean Expression

Page 7: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

The while Statement

int sum = 0, number = 1;

while ( number <= 100 ) {

sum = sum + number;

number = number + 1;

}

These statements are executed as long as number is less than or equal to 100.

These statements are executed as long as number is less than or equal to 100.

Page 8: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Control Flow of while

int sum = 0, number = 1int sum = 0, number = 1

number <= 100 ?

number <= 100 ?

falsesum = sum + number;

number = number + 1;

sum = sum + number;

number = number + 1;

true

Page 9: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Infinite Loops

A loop that continues executing forever

Can be caused by syntax or logic errors. For example:

while (num < 0) //error--no bracesSystem.out.print("Enter a value: ");num = input.nextInt();

while (num < 0); //error-- semicolonSystem.out.print("Enter a value: ");num = input.nextInt();

Causes an infinite loop.

Page 10: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

while Loop Pitfall - 1

Infinite Loops Both loops will not terminate because the boolean expressions will never become false.

Infinite Loops Both loops will not terminate because the boolean expressions will never become false.

int count = 1;

while ( count != 10 ) {

count = count + 2;

}

22

int product = 0;

while ( product < 500000 ) {

product = product * 5;

}

11

Page 11: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

while Loop Pitfall - 2 Goal: Execute the loop body 10 times.

count = 1;

while (count < 10) {

. . .

count++;

}

11

count = 0;

while (count <= 10) {

. . .

count++;

}

33

count = 1;

while (count <= 10) {

. . .

count++;

}

22

count = 0;

while (count < 10) {

. . .

count++;

}

44

11 33and exhibit off-by-one error.

Page 12: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

The do-while Statement

Alternative form of the while statement

Executes at least once

In do while loops the test condition comes at the end of the loop. So, the loop always iterates at least once.

In Cooking Example:

give sauce a stir;while (there are lumps)

Page 13: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

do {

sum += number;

number++;

} while (sum <= 1000000);

Syntax for the do-while Statement

do {

<statement>

} while (<boolean expression>);

Statement(loop body)

Statement(loop body)

Boolean ExpressionBoolean Expression

Page 14: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

The do-while Statementint sum = 0, number = 1;

do {

sum += number;

number++;

} while ( sum <= 1000000 );

These statements are executed as long as sum is less than or equal to 1,000,000.

These statements are executed as long as sum is less than or equal to 1,000,000.

Page 15: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Control Flow of do-while

int sum = 0, number = 1int sum = 0, number = 1

sum += number;

number++;

sum += number;

number++;

sum <= 1000000 ?sum <= 1000000 ?true

false

Page 16: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Pre-test vs. Post-test loops

Use a pre-test loop for something that may be done zero times

Use a post-test for something that is always done at least once

Page 17: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Checklist for Repetition Control

1. Watch out for the off-by-one error (OBOE).

2. Make sure the loop body contains a statement that will eventually cause the loop to terminate.

3. Make sure the loop repeats exactly the correct number of times.

Page 18: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

The for Statement

Returning one last time to our cooking analogy, we might have instructions to eliminate lumpiness phrased in the form”stir mixture for 100 strokes”

In Java:

int count;

for (count = 0; count < 100; count++)

give sauce a stir;

Page 19: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

The for Statement

Loop structure that executes a set of statements a fixed number of times

Uses a loop control variable (lcv)

The increment (++) or decrement (--) operators are used to change the value of the loop control variable

Page 20: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

for ( i = 0 ; i < 20 ; i++ ) {

number = inputBox.getInteger();

sum += number;

}

Syntax for the for Statementfor ( <initialization>; <boolean expression>; <update> )

<statement>

InitializationInitialization Boolean Expression

Boolean Expression UpdateUpdate

Statement(loop body)

Statement(loop body)

Page 21: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

The for Statementint i, sum = 0, number;

for (i = 0; i < 20; i++) {

number = In.getInt();

sum += number;

}These statements are executed for 20 times ( i = 0, 1, 2, … , 19).

These statements are executed for 20 times ( i = 0, 1, 2, … , 19).

Page 22: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Control Flow of for

i = 0;i = 0;

false

number = inputBox.getInteger( );sum += number;

number = inputBox.getInteger( );sum += number;

true

i ++;i ++;

i < 20 ? i < 20 ?

Page 23: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

The Nested-for Statement

If a first loop contains a second loop, we

say the second is nested within the first.

The first loop is often referred to as the

outer loop and the second as the inner

loop.

Read page 152-153

Page 24: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Which Loop, When?

for loop – when you know how many times the loop will be executed

while loop – when you don’t know how many times the loop will execute, including when the user is in control

while loop – when you may not want the loop to execute even once

do while – when you want the loop to execute even once

Page 25: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Debugging Techniques

The debugger included with many compilers

Variable trace, which is a manual technique of list values of variables at the points of assignment

Additional println() statements for displaying variable values at points of assignment

"Commenting out" code to detect bugs through a process of elimination

Page 26: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Variable Trace

int num1 = 0;int num2 = 0;while (num1 < 10) {

if (num1 % 3 == 0) {num2 += num1;System.out.print(num2 + " ");

}num1 += 1;

}

Page 27: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Using println() to Debug

int num1 = 0;int num2 = 0;System.out.println("num1 before while: " + num1); //debugwhile (num1 < 10) {System.out.println("num1 in while: " + num1); //debugif (num1 % 3 == 0) {num2 += num1;System.out.println("num2: " + num2); //debugSystem.out.print(num2 + " ");

}num1 += 1;

}

Page 28: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Using Comments to Debug

int num1 = 0;int num2 = 0;while (num1 < 10) {

//if (num1 % 3 == 0) {// num2 += num1;// System.out.print(num2 + " ");//}num1 += 1;

}

Page 29: Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Homework

Page 138 # 1, 2, 3, 5 Page 140 # 1 Page 141 # 3, 4 Page 143 # 1