5-1 repetition statements repetition statements allow us to execute a statement multiple times often...

39
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements, they are controlled by boolean expressions Java has three kinds of repetition statements: – the while loop – the do loop – the for loop The programmer should choose the right kind of loop for the situation

Upload: percival-fowler

Post on 02-Jan-2016

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-1

Repetition Statements• Repetition statements allow us to execute a statement

multiple times• Often they are referred to as loops• Like conditional statements, they are controlled by boolean

expressions• Java has three kinds of repetition statements:

– the while loop– the do loop– the for loop

• The programmer should choose the right kind of loop for the situation

Page 2: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-2

The while Statement• A while statement has the following syntax:

while ( condition ) statement;

• If the condition is true, the statement is executed

• Then the condition is evaluated again, and if it is still true, the statement is executed again

• The statement is executed repeatedly until the condition becomes false

Page 3: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-3

Logic of a while Loop

statement

true false

conditionevaluated

Page 4: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

4

Trace while Loop

int count = 0;

while (count < 2) {

System.out.println("Welcome to Java!");

count++;

}

Initialize count

animation

Page 5: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5

Trace while Loop, cont.

int count = 0;

while (count < 2) {

System.out.println("Welcome to Java!");

count++;

}

(count < 2) is true

animation

Page 6: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

6

Trace while Loop, cont.

int count = 0;

while (count < 2) {

System.out.println("Welcome to Java!");

count++;

}

Print Welcome to Java

animation

Page 7: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

7

Trace while Loop, cont.

int count = 0;

while (count < 2) {

System.out.println("Welcome to Java!");

count++;

}

Increase count by 1count is 1 now

animation

Page 8: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

8

Trace while Loop, cont.

int count = 0;

while (count < 2) {

System.out.println("Welcome to Java!");

count++;

}

(count < 2) is still true since count is 1

animation

Page 9: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

9

Trace while Loop, cont.

int count = 0;

while (count < 2) {

System.out.println("Welcome to Java!");

count++;

}

Print Welcome to Java

animation

Page 10: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

10

Trace while Loop, cont.

int count = 0;

while (count < 2) {

System.out.println("Welcome to Java!");

count++;

}

Increase count by 1count is 2 now

animation

Page 11: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

11

Trace while Loop, cont.

int count = 0;

while (count < 2) {

System.out.println("Welcome to Java!");

count++;

}

(count < 2) is false since count is 2 now

animation

Page 12: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

12

Trace while Loop

int count = 0;

while (count < 2) {

System.out.println("Welcome to Java!");

count++;

}

The loop exits. Execute the next statement after the loop.

animation

Page 13: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

13

do-while Loop

do {

// Loop body;

Statement(s);

} while (loop-continuation-condition);

Loop Continuation Condition?

true

Statement(s) (loop body)

false

Page 14: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

14

for Loopsfor (initial-action; loop-

continuation-condition; action-after-each-iteration) {

// loop body; Statement(s);}

int i;for (i = 0; i < 100; i++) { System.out.println( "Welcome to Java!"); }

Loop Continuation Condition?

true

Statement(s) (loop body)

false

(A)

Action-After-Each-Iteration

Initial-Action

(i < 100)?

true

System.out.println( "Welcome to Java");

false

(B)

i++

i = 0

Page 15: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

15

Trace for Loop

int i;for (i = 0; i < 2; i++) { System.out.println( "Welcome to Java!"); }

Declare i

animation

Page 16: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

16

Trace for Loop, cont.

int i;for (i = 0; i < 2; i++) { System.out.println( "Welcome to Java!"); }

Execute initializeri is now 0

animation

Page 17: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

17

Trace for Loop, cont.

int i;for (i = 0; i < 2; i++) { System.out.println( "Welcome to Java!"); }

(i < 2) is true since i is 0

animation

Page 18: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

18

Trace for Loop, cont.

int i;for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

Print Welcome to Java

animation

Page 19: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

19

Trace for Loop, cont.

int i;for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

Execute adjustment statement i now is 1

animation

Page 20: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

20

Trace for Loop, cont.

int i;for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

(i < 2) is still true since i is 1

animation

Page 21: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

21

Trace for Loop, cont.

int i;for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

Print Welcome to Java

animation

Page 22: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

22

Trace for Loop, cont.

int i;for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

Execute adjustment statement i now is 2

animation

Page 23: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

23

Trace for Loop, cont.

int i;for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

(i < 2) is false since i is 2

animation

Page 24: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

24

Trace for Loop, cont.

int i;for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

Exit the loop. Execute the next statement after the loop

animation

Page 25: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

25

Note

for ( ; ; ) { // Do something } (a)

Equivalent while (true) { // Do something }

(b)

Page 26: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

26

Caution

Adding a semicolon at the end of the for clause before the loop body is a common mistake, as shown below:

Logic Error

for (int i=0; i<10; i++);

{

System.out.println("i is " + i);

}

Page 27: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

27

Caution, cont.Similarly, the following loop is also wrong:int i=0; while (i < 10);{ System.out.println("i is " + i); i++;}In the case of the do loop, the following semicolon is needed to end the loop.int i=0; do { System.out.println("i is " + i); i++;} while (i<10);

Logic Error

Correct

Page 28: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-28

Conditional Statements• A conditional statement lets us choose which

statement will be executed next

• Therefore they are sometimes called selection statements

• Conditional statements give us the power to make basic decisions

• The Java conditional statements are the:

– if statement– if-else statement– switch statement

Page 29: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-29

The if Statement• The if statement has the following syntax:

if ( condition ) statement;

if is a Javakeyword

The condition must be aboolean expression. It mustevaluate to either true or false.

If condition is true: statement is executed.If condition is false: statement is skipped.

Page 30: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-30

Logic of an if statement

conditionevaluated

statement

truefalse

Page 31: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-31

Boolean Expressions• A condition often uses one of Java's equality

operators or relational operators, which all return boolean results:

== equal to!= not equal to< less than> greater than<= less than or equal to>= greater than or equal to

• Note the difference between the equality operator (==) and the assignment operator (=)

Page 32: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-33

Logical AND and Logical OR

• The logical AND expression

a && b

is true if both a and b are true, and false otherwise

• The logical OR expression

a || b

is true if a or b or both are true, and false otherwise

Page 33: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-34

The if-else Statement• An else clause can be added to an if

statement to make an if-else statementif ( condition ) statement1;else statement2;

• If the condition is true, statement1 is executed; if the condition is false, statement2 is executed

• One or the other will be executed, but not both.

Page 34: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-35

Logic of an if-else statement

conditionevaluated

statement1

true false

statement2

Page 35: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-36

The switch Statement• The switch statement provides another way to

decide which statement to execute next

• The switch statement evaluates an expression, then attempts to match the result to one of several possible cases

• Each case contains one value (a constant) and a list of statements

• The flow of control transfers to statement associated with the first case value that matches

Page 36: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-37

The switch Statement• The general syntax of a switch statement is:

switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ...

}

switchandcaseare

keywords

If expressionmatches value2,control jumpsto here

Page 37: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-38

The switch Statement• Often a break statement is used as the last

statement in each case's statement list

• A break statement causes control to transfer to the end of the switch statement

• If a break statement is not used, the flow of control will continue into the next case

• Sometimes this may be appropriate, but often we want to execute only the statements associated with one case

Page 38: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-39

The switch Statement

switch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break;}

• An example of a switch statement:

Page 39: 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,

5-40

The switch Statement• A switch statement can have an optional

default case

• The default case has no associated value and simply uses the reserved word default

• If the default case is present, control will transfer to it if no other case value matches

• If there is no default case, and no other value matches, control falls through to the statement after the switch