Java Tutorials - Herong's Tutorial Examples - v8.22, by Herong Yang
Basic "for" Statements
This section describes basic 'for' statement, which is a looping statement that executes the contained statement repeatedly with an initial logic, a looping condition, and an update logic.
"for" statements have 2 forms: basic "for" statements and enhanced "for" statements. Let's look at basic "for" statements in this tutorial first.
What Is Basic "for" Statement? - A basic "for" statement is a looping statement that executes the contained statement repeatedly with an initial logic, a looping condition, and an update logic.
Here is the syntax for a basic "for" statement.
for (initial_logic; looping_condition; update_logic) contained_statement
When a basic "for" statement is executed, the flow of control can be described as:
Step 1. Run "initial_logic".
Step 2. Evaluate "looping_condition". If result is "false", end the "for" statement.
Step 3. Run "contained_statement".
Step 4. Run "update_logic".
Step 5. 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 basic "for" statements:
/* BasicForStatementTest.java * Copyright (c) HerongYang.com. All Rights Reserved. */ class BasicForStatementTest { public static void main(String[] arg) { java.io.PrintStream out = System.out; int max = 20; int sum = 0; out.println("\"for\" statement with no contained statement:"); for (int i = 0; i < max; sum += ++i); out.println(" Sum of 1 to 20: "+sum); sum = 0; out.println("\"for\" statement with a contained statement:"); for (int i = 0; i < max; i++) sum += i; out.println(" Sum of 0 to 19: "+sum); out.println("\"for\" statement with a contained statement block:"); for (int i = 3; i < max; i++) { boolean isPrime = true; for (int j = 2; j <= i/2; j++) { isPrime = i%j > 0; if (!isPrime) break; } if (isPrime) out.println(" "+i+" is a prime number."); } } }
If you compile and run the above program, you will see:
herong> java BasicForStatementTest.java "for" statement with no contained statement: Sum of 1 to 20: 210 "for" statement with a contained statement: Sum of 0 to 19: 190 "for" 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