Java-For Loop with Multiple Conditions

For Loop with Multiple Conditions

The for loop has several capabilities that are not found in other loop constructs. For example, more than one variable can be initialized at a time in the for statement using comma. The test condition may have any compound relation. For example, in the following program, the expression1  has two parts i = 0 and j = 0 separated by comma and the loop uses compound condition. The loop is executed as long as both conditions i<5 and j<5 are true.

Program

1

Program Source

class Javaapp {

    public static void main(String[] args) {
        
        for(int i=0,j=0; (i<5&j<5); i++,j++)
        {
            System.out.println("Value of i = "+i);
            System.out.println("Value of j = "+j);
        }  
    }
}

Leave a Comment