5-1 repetition statements repetition statements allow us to execute a statement multiple times often...
TRANSCRIPT
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
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
5-3
Logic of a while Loop
statement
true false
conditionevaluated
4
Trace while Loop
int count = 0;
while (count < 2) {
System.out.println("Welcome to Java!");
count++;
}
Initialize count
animation
5
Trace while Loop, cont.
int count = 0;
while (count < 2) {
System.out.println("Welcome to Java!");
count++;
}
(count < 2) is true
animation
6
Trace while Loop, cont.
int count = 0;
while (count < 2) {
System.out.println("Welcome to Java!");
count++;
}
Print Welcome to Java
animation
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
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
Trace while Loop, cont.
int count = 0;
while (count < 2) {
System.out.println("Welcome to Java!");
count++;
}
Print Welcome to Java
animation
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
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
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
do-while Loop
do {
// Loop body;
Statement(s);
} while (loop-continuation-condition);
Loop Continuation Condition?
true
Statement(s) (loop body)
false
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
15
Trace for Loop
int i;for (i = 0; i < 2; i++) { System.out.println( "Welcome to Java!"); }
Declare i
animation
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
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
Trace for Loop, cont.
int i;for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }
Print Welcome to Java
animation
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
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
Trace for Loop, cont.
int i;for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }
Print Welcome to Java
animation
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
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
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
Note
for ( ; ; ) { // Do something } (a)
Equivalent while (true) { // Do something }
(b)
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
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
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
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.
5-30
Logic of an if statement
conditionevaluated
statement
truefalse
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 (=)
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
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.
5-35
Logic of an if-else statement
conditionevaluated
statement1
true false
statement2
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
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
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
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:
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