Title: Repetition Statements
1Lecture 3-1
2Objectives
- Implement repetition control in a program using
while statements. - Implement repetition control in a program using
dowhile statements. - Implement repetition control in a program using
for statements. - Nest a loop repetition statement inside another
repetition statement. - Choose the appropriate repetition control
statement for a given task. - Prompt the user for a yesno reply using the
ResponseBox class from the javabook package. - Output formatted data using the Format class from
the javabook package.
3The while Statement
4Example Testing Input Data
- Heres a realistic example of using the while
loop to accept only the valid input data. - Accepts age between 0 and 130, exclusively.
5while Loop Pitfall - 1
Infinite Loops Both loops will not terminate
because the boolean expressions will never become
false.
Note With JAVA an Overflow Error (due to an
attempt to assign a value larger than the maximum
value a variable can hold) will not terminate a
program
6while Loop Pitfall - 2
Using Real Numbers Loop 2 terminates, but Loop 1
does not because only an approximation of a real
number can be stored in a computer memory.
7while Loop Pitfall - 3
- Goal Execute the loop body 10 times.
8Checklist for Repetition Control
- Watch out for the off-by-one error (OBOE).
- Make sure the loop body contains a statement that
will eventually cause the loop to terminate. - Make sure the loop repeats exactly the correct
number of times. - If you want to execute the loop body N times,
then initialize the counter to 0 and use the test
condition counter lt N or initialize the counter
to 1 and use the test condition counter lt N.
9Useful Shorthand Operators
10The do-while Statement
11do-while Statement
do ltstatementgt while ( ltboolean expressiongt )
12Loop Control without Boolean Variable
13Loop Control with Boolean Variable
14Question-5
- Rewrite the following while loop as a do-while
loop. - int count0, sum0
- while (countlt10)
- sum count
- count
-
- Answer
- int count0, sum0
- do
- sum count
- count
- while (countlt10)
15ResponseBox
- The ResponseBox class is used to get a YES or NO
response from the user.
16ResponseBox Processing the Selection
- To determine which button the user clicked, we
write
17ResponseBox Sample Usage
- Heres a typical use of ResponseBox
18ResponseBox Other Usage
- ResponseBox can have up to three buttons with
user-designated labels.
19ResponseBox Methods
20The for Statement
21Syntax for the for Statement
for ( ltinitializationgt ltboolean expressiongt
ltincrementgt ) ltstatementgt
22More Examples
i 0, 5, 10, , 95
j 2, 4, 8, 16, 32
k 100, 99, 98, 97, ..., 1
23The Nested-for Statement
- Nesting a for statement inside another for
statement is commonly used technique in
programming. - Lets generate the following table using
nested-for statement.
24Generating the Table
25Question-6
- What will be the value of sum after the following
nested-for loop is executed - int sum0
- for (int i0 ilt5 i)
- sumsumi
- for (int ji jlt5 j)
- sumsumj
-
-
- Answer 50 (How?)
26Formatting Integers
27Formatting Real Numbers
28Formatting Strings
29Methods in The Format Class