Java Tutorial

What is Java


Control Statements


Java Object Class


Java Inheritance


Java Polymorphism


Java Abstraction


Java Encapsulation


Java Array

Java OOPs Misc


Do-while Loop in java

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.
The Java programming language also provides a do-while statement, which can be expressed as follows:

Syntax:


do {
statement(s)
} while (expression);


Example:

class Java64bit {
public static void main(String[] args){
int count = 1;
do {
System.out.println("Count is: " + count);
count++;
} while (count < 3);
}
}


Output:Count is: 1 Count is: 2

Java Infinitive do-while Loop

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

Example:

package com.ashu;
class Java64bitDemo {
public static void main(String[] ab) throws InterruptedException {
do {
System.out.println("Start Processing inside do while loop");
// look for a file at specific directory
// if found, then process it, such as inserting rows into database
System.out.println("End Processing of do while loop");
Thread.sleep(5 * 1000);
} while (true);
}
}


Output:Start Processing inside do while loop, End Processing of do while loop

Advertisement

Copyright ©2017 Design& development