Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,"— Presentation transcript:

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, 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

2 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

3 5-3 Logic of a while Loop statement true false condition evaluated

4 4 Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Initialize count animation

5 5 Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } (count < 2) is true animation

6 6 Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Print Welcome to Java animation

7 7 Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Increase count by 1 count is 1 now animation

8 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

9 9 Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Print Welcome to Java animation

10 10 Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Increase count by 1 count is 2 now animation

11 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

12 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

13 13 do-while Loop do { // Loop body; Statement(s); } while (loop-continuation-condition);

14 14 for Loops for (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!"); }

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

16 16 Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System.out.println( "Welcome to Java!"); } Execute initializer i is now 0 animation

17 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

18 18 Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } Print Welcome to Java animation

19 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

20 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

21 21 Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } Print Welcome to Java animation

22 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

23 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

24 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

25 25 Note

26 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); }

27 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

28 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

29 5-29 The if Statement The if statement has the following syntax: if ( condition ) statement; if is a Java keyword The condition must be a boolean expression. It must evaluate to either true or false. If condition is true: statement is executed. If condition is false: statement is skipped.

30 5-30 Logic of an if statement condition evaluated statement true false

31 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 ( = )

32 © 2004 Pearson Addison- Wesley. All rights reserved 5-32 Logical Operators Boolean expressions can also use the following logical operators: ! Logical NOT && Logical AND || Logical OR They all take boolean operands and produce boolean results Logical NOT is a unary operator (it operates on one operand) Logical AND and logical OR are binary operators (each operates on two operands)

33 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

34 5-34 The if-else Statement An else clause can be added to an if statement to make an if-else statement if ( 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.

35 5-35 Logic of an if-else statement condition evaluated statement1 true false statement2

36 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

37 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... } switch and case are keywords If expression matches value2, control jumps to here

38 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

39 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:

40 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


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

Similar presentations


Ads by Google