While loop java

Till condition is true, the code in the loop will run, over and over again in the while loop in java programming.

Flow Diagram for while loop

flowchart for while loop java

Java while loop syntax:

while (condition) {
    //code block for need execute
} 

Example:

public class Solution {
    public static void main(String[] args) {
        int i = 0;
        while (i<10) {
            System.out.print(i+" ");
            i++;
        }
    }   
}

Output:

0 1 2 3 4 5 6 7 8 9 

Do While Loop

The do while loop main deference than while loop, the code block will execute, before checking if the condition is true.

Flow Diagram for do while loop

flowchart for do while loop java

Java do while loop syntax:

do
while (condition) {
    //code block for need execute
} 

Example:

public class Solution {
    public static void main(String[] args) {
        int i = 0;
        do {
            System.out.print(i+" ");
            i++;
        }
        while (i<10);
    }   
}

Output:

0 1 2 3 4 5 6 7 8 9 

Java is the most popular programming language and widely-used programming language for many years. You can free learning java programming by following our Java articles sequentially.

 89 total views,  2 views today

Leave a Comment

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

wpChatIcon