Skip to content

Java For Loop with Example | Syntax | Break | Enhanced for loop

  • by

Loop condition statement in programming is used to repeat a block of code (statement) until a specific condition. Java for loop is one of the ways to do loop execution in the program, there are many.

A For Loop is break one a given condition is false.

Java For Loop with Example Syntax Break Enhanced for loop

Syntax

Here Java for loop syntax code.

for (initialization; testExpression; update)
{
    // codes(statements) body
}

How for the loop works?

In mind, if you got the question of how java for loop works, then there is 3 stage of it.

  • Initialization – This step executes first and only once in for loop. Declare and initialize variables for loop and ends with a semicolon (;).
  • Test Expression – This is a boolean expression, will execute if it’s true else pass them for a loop.
  • Update – Here is a loop that comes after a complete cycle of body and updates the value of variables (Increase or decrease value, etc).

With an example, you will get more clarity.

for Loop Flowchart Diagram

how java for Loop Flowchart Diagram example

Java for loop example

Here is an example program code in java with the output. This program a print 1 to 10 number in java,

Here is i is an Initialization variable with an assigned value of 1. The loop executes until i <= 10 is true. And “i” value increased by 1 every time as using unary operator (i++)

package eye.hunts;

public class A {
    public static void main(String[] args) {
        //Java for loop code
        for (int i = 1; i <= 10; i++) {
            System.out.println(i);
        }
    }
}

Output: 1
2
3
4
5
6
7
8
9
10

Java for loop array

A for loop is used with the Array is accessing an element(item) one by one. Here is an example of how to iterating over an array used for a loop.

public class A {
    public static void main(String[] args) {
        int ar[] = { 1, 2, 3, 4, 5};
        int i, x;

        // iterating over an array
        for (i = 0; i < ar.length; i++) {

            // accessing each element of array
            x = ar[i];
            System.out.print(x + " ");
        }
    }
}

Output: 1 2 3 4 5

Java for loop list

You have to use a for-each statement for a list of data structure in java.

for (E element : list) {
    . . .
}

QA: What is an Enhanced for loop in java?

A Java for-each Loop loop is an Enhanced and Advanced version of Java for a loop. If you are working with array and collection then it's mainly using for loop condition statements.

QA: What is nested for loop in java?

A Loop inside Loop is called a nested loop. So if there is for loop is exists inside another loop is called a Nested for loop in java.

Here is a simple program example of a Nested Loop. Printing a pattern with Nested for a loop.

public class A {
    public static void main(String[] args) {
        int rows = 5;

        for(int i = 1; i <= rows; ++i)
        {
            for(int j = 1; j <= i; ++j)
            {
                System.out.print("*" + " ");
            }
            System.out.println("");
        }
    }
}

Output:

What is nested for loop in java example

QA: How to Java for loop break (not by condition false)?

You can break and loop by using Break Keyword in Java, let's see this simple example for it. Using an if condition when the value matched then breaks the loop otherwise it will break in a first-time loop cycle.

public class A {
    public static void main(String[] args) {
        //Java for loop code
        for (int i = 20; i <= 30; i++) {
            System.out.println(i);
            if (i == 21) {
                break;
            }
        }
    }
}

Output: 20
21

Do java loop programs for practice, it's required mostly places in different ways. So you must have practical knowledge of it. If any doubt and suggestions, do comment.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Example of Java program code are in Java 11, so it may change on different from 9 or 10 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *