0% found this document useful (0 votes)
44 views2 pages

Java Do While Loop

The Java do-while loop iterates a block of code at least once and checks the condition at the end of each iteration, unlike a regular while loop which checks the condition at the start. It is useful when the number of iterations isn't fixed or the code needs to run at least once. The document provides examples of a regular do-while loop that iterates 10 times and an infinite do-while loop that runs continuously until manually stopped.

Uploaded by

mehul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
44 views2 pages

Java Do While Loop

The Java do-while loop iterates a block of code at least once and checks the condition at the end of each iteration, unlike a regular while loop which checks the condition at the start. It is useful when the number of iterations isn't fixed or the code needs to run at least once. The document provides examples of a regular do-while loop that iterates 10 times and an infinite do-while loop that runs continuously until manually stopped.

Uploaded by

mehul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

Java do-while Loop

The Java do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and
you must have to execute the loop at least once, it is recommended to use do-while loop.

The Java do-while loop is executed at least once because condition is checked after loop body.

Syntax:

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

Example:

public class DoWhileExample {


public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10);
}
}

Test it Now

Output:

1
2
3
4
5
6
7
8
9
10

Java Infinitive do-while Loop


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

Syntax:

do{
//code to be executed
}while(true);

Example:

public class DoWhileExample2 {


public static void main(String[] args) {
do{
System.out.println("infinitive do while loop");
}while(true);
}
}

Output:

infinitive do while loop


infinitive do while loop
infinitive do while loop
ctrl+c

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

You might also like