4.2. The while Loop

 
[Page 95]

Chapter 4. Loops

Mayan God Shel, Mexico. Photographer: Philip Coblentz. Courtesy Brand X Pictures.

Objectives

  • To use while , do-while , and for loop statements to control the repetition of statements ( § §4.2 “4.4).

  • To understand the flow of control in loop statements ( § §4.2 “4.4).

  • To use Boolean expressions to control loop statements ( § §4.2 “4.4).

  • To know the similarities and differences between three types of loops ( §4.5).

  • To write nested loops ( §4.6).

  • To implement program control with break and continue ( §4.9).


[Page 96]

4.1. Introduction

Suppose that you need to print a string (e.g., "Welcome to Java!" ) a hundred times. It would be tedious to have to write the following statement a hundred times:

 System.out.println(   "Welcome to Java!"   ); 

Java provides a powerful control structure called a loop that controls how many times an operation or a sequence of operations is performed in succession. Using a loop statement, you simply tell the computer to print a string a hundred times without having to code the print statement a hundred times.

Loops are structures that control repeated executions of a block of statements. The concept of looping is fundamental to programming. Java provides three types of loop statements: while loops, do-while loops, and for loops.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net