Java Tutorials - Herong's Tutorial Examples - v8.22, by Herong Yang
"do" Statements
This section describes 'do' statement, which is a looping statement that executes the contained statement immediately, then repeat it while a given condition is true.
What Is "do" Statement? - A "do" statement is a looping statement that executes the contained statement immediately, then repeat it while a given condition is true.
Here is the syntax for a "do" statement.
do contained_statement while (looping_condition)
When a "do" statement is executed, the flow of control can be described as:
Step 1. Run "contained_statement".
Step 2. Evaluate "looping_condition". If result is "false", end the "while" statement.
Step 3. Run "contained_statement".
Step 4. Go back to Step 2.
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 "do" statements:
/* DoStatementTest.java
* Copyright (c) HerongYang.com. All Rights Reserved.
*/
class DoStatementTest {
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("\"do\" statement with no contained statement:");
do ; while (downgrade() > 0);
int max = 20;
int sum = 0;
int i = 0;
out.println("\"do\" statement with a contained statement:");
do sum += ++i; while (i < max);
out.println(" Sum of 1 to 20: "+sum);
boolean isPrime;
i = 3;
out.println("\"do\" statement with a contained statement block:");
do {
isPrime = true;
int j = 2;
do {
isPrime = i%j > 0;
if (!isPrime) break;
j++;
} while (j < i);
if (isPrime) out.println(" "+i+" is a prime number.");
i++;
} while (i < max);
}
}
If you compile and run the above program, you will see:
herong> java DoStatementTest.java "do" 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 "do" statement with a contained statement: Sum of 1 to 20: 210 "do" 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
Execution Process, Entry Point, Input and Output
Primitive Data Types and Literals
What Is Control Flow Statement
Nested "if-then-else" Statements
Fall-Through Behavior of "switch" Statements
Bits, Bytes, Bitwise and Shift Operations
Managing Bit Strings in Byte Arrays
Reference Data Types and Variables
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