Repetition Statements - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Repetition Statements

Description:

Implement repetition control in a program using do while ... if (num == 0) //sentinel. messageBox.show('Sum = ' sum); else if (num % 2 == 0) //invalid data ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 30
Provided by: drcaf
Category:

less

Transcript and Presenter's Notes

Title: Repetition Statements


1
Lecture 3-1
  • Repetition Statements

2
Objectives
  • 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.

3
The while Statement
4
Example 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.

5
while 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
6
while 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.
7
while Loop Pitfall - 3
  • Goal Execute the loop body 10 times.

8
Checklist 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.

9
Useful Shorthand Operators
10
The do-while Statement
11
do-while Statement
do ltstatementgt while ( ltboolean expressiongt )
12
Loop Control without Boolean Variable
13
Loop Control with Boolean Variable
14
Question-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)

15
ResponseBox
  • The ResponseBox class is used to get a YES or NO
    response from the user.

16
ResponseBox Processing the Selection
  • To determine which button the user clicked, we
    write

17
ResponseBox Sample Usage
  • Heres a typical use of ResponseBox

18
ResponseBox Other Usage
  • ResponseBox can have up to three buttons with
    user-designated labels.

19
ResponseBox Methods
20
The for Statement
21
Syntax for the for Statement
for ( ltinitializationgt ltboolean expressiongt
ltincrementgt ) ltstatementgt
22
More Examples
i 0, 5, 10, , 95
j 2, 4, 8, 16, 32
k 100, 99, 98, 97, ..., 1
23
The 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.

24
Generating the Table
25
Question-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?)

26
Formatting Integers
27
Formatting Real Numbers
28
Formatting Strings
29
Methods in The Format Class
Write a Comment
User Comments (0)
About PowerShow.com