Complete Java masterclass for beginners -12 (while loop)

Java While Loop

The Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop.

Syntax:

while(condition){
//code to be executed
       
}

Example:

public class WhileExample {

    public static void main(String[] args) {

        int i=1;

        while(i<=10){

            System.out.println(i);

            i++;

        }

    }

} 

Output:


Java Infinitive While Loop

If you pass true in the while loop, it will be infinitive while loop.

Syntax:

while(true){

//code to be executed  

        

Example:

public class WhileExample2 {

    public static void main(String[] args) {

        while(true){

            System.out.println("infinitive while loop");

        }

    }

}  

Output:



Now, you need to press ctrl+c to exit from the program.

Comments

Popular posts from this blog

Complete Java masterclass for beginners -10 (switch statement)

Complete Java masterclass for beginners -11 (for loop)

Complete Java masterclass for beginners -06 (Unicode System)