"while" Statements

This section describes 'while' statement, which is a looping statement that executes the contained statement repeatedly while a given condition is true.

What Is "while" Statement? - A "while" statement is a looping statement that executes the contained statement repeatedly while a given condition is true.

Here is the syntax for a "while" statement.

while (looping_condition) contained_statement

When a "while" statement is executed, the flow of control can be described as:

Step 1. Evaluate "looping_condition". If result is "false", end the "while" statement.

Step 2. Run "contained_statement".

Step 3. Go back to Step 1.

The contained statement can be empty, a simple single statement, or a complex statement block. Here is a sample program that shows you how to use "while" statements:

/* WhileStatementTest.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
class WhileStatementTest {
   private static int rank = 5;
   private static int downgrade() {
      rank--;
      System.out.println("   Rank downgraded to: "+rank);
      return rank;
   }

   public static void main(String[] arg) {
      java.io.PrintStream out = System.out;

      out.println("\"while\" statement with no contained statement:");
      while (downgrade() > 0);

      int max = 20;
      int sum = 0;
      int i = 0;
      out.println("\"while\" statement with a contained statement:");
      while (i < max) sum += ++i;
      out.println("   Sum of 1 to 20: "+sum);

      boolean isPrime;
      i = 3;
      out.println("\"while\" statement with a contained statement block:");
      while (i < max) {
         isPrime = true;
         int j = 2;
         while (j < i) {
            isPrime = i%j > 0;
            if (!isPrime) break;
            j++;
         }
         if (isPrime) out.println("   "+i+" is a prime number.");
         i++;
      }
   }
}

If you compile and run the above program, you will see:

herong> java WhileStatementTest.java

"while" statement with no contained statement:
   Rank downgraded to: 4
   Rank downgraded to: 3
   Rank downgraded to: 2
   Rank downgraded to: 1
   Rank downgraded to: 0

"while" statement with a contained statement:
   Sum of 1 to 20: 210

"while" statement with a contained statement block:
   3 is a prime number.
   5 is a prime number.
   7 is a prime number.
   11 is a prime number.
   13 is a prime number.
   17 is a prime number.
   19 is a prime number.

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

Control Flow Statements

 What Is Control Flow Statement

 "if-then" Statements

 "if-then-else" Statements

 Nested "if-then-else" Statements

 "switch" Statements

 Fall-Through Behavior of "switch" Statements

 Basic "for" Statements

 Enhanced "for" Statements

"while" Statements

 "do" Statements

 "break" Statements

 Labeled "break" Statements

 "continue" Statements

 Labeled "continue" Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB