Java for loops

java-Logo
Java Tutorials
Java Environment
Environment Setup
Declaration & Assignments
Flow Control
Miscellaneous
Java OOPs

Java for loops is very similar to Java while loops in that it continues to process a block of code until a statement becomes false, and everything is defined in a single line.

The basic format of for loop statement is:

Syntax:
for ( init; condition; increment )
{
   statement(s);
}

Figure - Flowchart of for loop:

java-for-loop

Example of a Java Program to Demonstrate for loop

 Example:
public class Sample {

    public static void main(String args[]) {
        /* local variable Initialization */        int n = 1, times = 5;

        /* for loops execution */        for (n = 1; n <= times; n = n + 1) {
            System.out.println("Java for loops:" + n);
        }
    }
}
Program Output:

java-for-loop


Scroll Back to Top