Site MapHelpFeedbackRepetition Statements
Repetition Statements


The selection statements we covered in Chapter 5 alter the control flow of a program. In this chapter we will cover another group of control statements called repetition statements. Repetition statements control a block of code to be executed for a fixed number of times or until a certain condition is met. We will describe Java’s three repetition statements: while, do–while, and for. In addition to the repetition statements, we will introduce the third useful method of the JOptionPane class, called showConfirmDialog. The confirmation dialog is often used in conjunction with a repetition statement. For example, we can set up a repetition statement to keep playing a game while the user replies yes to a confirmation dialog. Finally, in optional Section 6.11, we will describe recursive methods. A recursive method is a method that calls itself. Instead of a repetition statement, a recursive method can be used to program the repetition control flow.

O b j e c t i v e s

After you have read and studied this chapter, you should be able to

Implement repetition control in a program using while statements.

Implement repetition control in a program using do–while statements.

Implement a generic loop-and-a-half repetition control statement.

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.

(Optional) Write simple recursive methods.

Format output values by using the Formatter class.







WuOnline Learning Center

Home > Chapter 6