Java Loops - The Coding Shala

Home >> Learn Java >> Java Loops

Java loops

In Java, loops are used to execute the same set of code again and again until termination. There are three types of loops or iteration statements are for, while, and do-while.
Java Loops - The Coding Shala

Java while loop

The basic syntax of Java while loop is as follows: 

while(condition){
// code inside while body
 }

The code inside the body of while loop will be executed as long as the condition is true. The condition can be any boolean expression. 

The following Java program explains Java's while loop: 


class WhileExample{
    public static void main(String[] args){
        int num = 0;
        while(num <= 5){
            System.out.println("Value of num is: " + num);
            num++;
        }
    }
}


<<<<<Output of this program is >>>>>
Value of num is: 0
Value of num is: 1
Value of num is: 2
Value of num is: 3
Value of num is: 4
Value of num is: 5

Java do-while loop

The basic syntax of Java's do-while loop is as follows: 

do {
  //body of loop
} while(condition);

The do-while loop always executes its body part at least once, because its condition is checked at the bottom of the loop. 

The following Java program explains Java's do-while loop: 

class DoWhileExapmle{
    public static void main(String args[]){
        int num = 5;
        do {
            System.out.println("Value of num is: " + num);
        } while(num<=4);
    }
}



<<<<Output of this program is >>>>>
Value of num is: 5

Java for loop

The basic syntax of Java's for loop is as follows: 

for(initialization; condition; iteration) {
// body of loop
}

Here, when the for loop starts the initialization expression is sets the value of the loop control variable. The condition must be a boolean expression and loop variables check whether the condition is true or not. If it is false, the loop terminates. Next, the iteration part is used to increments and decrements the loop control variables. 

The following java program explains the for loop: 

class ForExample{
    public static void main(String[] args){
        for(int i=0; i<10; i++){
             System.out.println("Value of i is: " + i);
        }
    }
}


<<<<<Output of this program is >>>>
Value of i is: 0
Value of i is: 1
Value of i is: 2
Value of i is: 3
Value of i is: 4
Value of i is: 5
Value of i is: 6
Value of i is: 7
Value of i is: 8
Value of i is: 9

Note: we can declare the loop control variables inside the for a loop. We can also use more than one statement inside for loop using a comma(,) and semicolon(;) is used as separated.

Java For-Each loop

Java for-each loop is the enhanced version of for loop. The basic syntax of the for-each loop is as follows: 
for(type var_name : collection ) {
//block code
}

Here, var_name is the name of the iteration variable that will receive the elements from a collection from start to end one at a time of specified type.

The following Java program explains the for-each loop: 

class ForEachExample{
    public static void main(String args[]){
        int arr[] = new int[5];
        for(int i=0; i<5; i++){ //normal java loop
            arr[i] = i;
        }
        
        for(int x : arr ){//for-each loop
            System.out.println("Value is: "+ x);
        }
    }
}



<<<<<Output of this program is>>>>>
Value is: 0
Value is: 1
Value is: 2
Value is: 3
Value is: 4



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to Create a Simple Calculator - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala