Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Do/While Loop

The Do/While Loop

The do/while loop is a variation of the while loop. It executes the code block once before checking the condition. Then, it repeats the loop as long as the condition remains true.

 

Syntax

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

 

The subsequent example using a do/while loop ensures that the loop runs at least once, regardless of the initial truth value of the condition. This is because the code block executes before the condition is checked:

Example

int i = 0;
do
{ System.out.println(i); i++; } while (i < 5);

 

Remember to increment the variable utilized in the condition, or else the loop will continue indefinitely.