REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often...
-
Upload
percival-fowler -
Category
Documents
-
view
223 -
download
0
Transcript of 5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often...
![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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/1.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/2.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/3.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/4.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/5.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/6.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/7.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/8.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/9.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/10.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/11.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/12.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/13.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/14.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/15.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/16.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/17.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/18.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/19.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/20.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/21.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/22.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/23.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/24.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/25.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/26.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/27.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/28.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/29.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/30.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/31.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/32.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/33.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/34.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/35.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/36.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/37.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/38.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649eda5503460f94be8cf6/html5/thumbnails/39.jpg)
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