0% found this document useful (0 votes)
45 views

Do While Loop in Java

A do-while loop will execute the code block at least once, and then continue to loop as long as the boolean expression evaluates to true. It checks the boolean expression at the end of each iteration, differing from a regular while loop which checks at the beginning. The example code prints the values of x from 10 to 19 using a do-while loop, demonstrating that it will run the code block initially before evaluating the boolean on subsequent iterations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Do While Loop in Java

A do-while loop will execute the code block at least once, and then continue to loop as long as the boolean expression evaluates to true. It checks the boolean expression at the end of each iteration, differing from a regular while loop which checks at the beginning. The example code prints the values of x from 10 to 19 using a do-while loop, demonstrating that it will run the code block initially before evaluating the boolean on subsequent iterations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

do while loop in java

A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one
time.

Syntax

Following is the syntax of a do...while loop −

do {

// Statements

}while(Boolean_expression);

Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once
before the Boolean is tested.
If the Boolean expression is true, the control jumps back up to do statement, and the statements in the loop
execute again. This process repeats until the Boolean expression is false.

Flow Diagram

Example

Live Demo
public class Test {

public static void main(String args[]) {

int x = 10;

do {

System.out.print("value of x : " + x );

x++;

System.out.print("\n");

}while( x < 20 );

This will produce the following result −

Output

value of x : 10

value of x : 11

value of x : 12

value of x : 13

value of x : 14

value of x : 15

value of x : 16

value of x : 17

value of x : 18

value of x : 19

You might also like