Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

C. Control Statements > C.17. do...while Repetition Statement

C.17. do...while Repetition Statement

The do...while repetition statement is similar to the while statement. In the while, the program tests the loop-continuation condition at the beginning of the loop, before executing the loop’s body; if the condition is false, the body never executes. The do...while statement tests the loop-continuation condition after executing the loop’s body; therefore, the body always executes at least once. When a do...while statement terminates, execution continues with the next statement in sequence. Figure C.16 uses a do...while (lines 10–14) to output the numbers 1–10.


 1   // Fig. C.16: DoWhileTest.java
 2   // do...while repetition statement.
 3   
 4   public class DoWhileTest
 5   {
 6      public static void main( String[] args )
 7      {
 8         int counter = 1; // initialize counter
 9      
10         do                                          
11         {                                           
12            System.out.printf( "%d  ", counter );    
13            ++counter;                               
14         } while ( counter <= 10 ); // end do...while
15   
16         System.out.println(); // outputs a newline
17      } // end main
18   } // end class DoWhileTest


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial