
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What are the differences between while loop and do-while loop in Java?
The while loop in java executes one or more statements after testing the loop continuation condition at the start of each iteration. The do-while loop, however, tests the loop continuation condition after the first iteration has completed. Therefore, the do-while loop guarantees one execution of the loop logic whereas the while does not.
Example
public class WhileAndDoWhileLoop { public static void main(String args[]) { int i=5; System.out.println("Test while Loop:"); while(i < 5) { System.out.println("Iteration: "+ ++i); } System.out.println("Test do-while Loop:"); i=5; do { System.out.println("Iteration: "+ ++i); } while(i < 5); } }
In the above example, The while loop statement will not execute at all. However, one iteration of the do-while loop will execute.
Output
Test while Loop: Test do-while Loop: Iteration: 6
- Related Articles
- Difference Between while and do-while Loop
- do…while loop vs. while loop in C/C++
- Java do-while loop example
- Java infinite do-while loop
- Java while loop
- Do-while loop in Arduino
- Difference between for loop and while loop in Python
- The do…while loop in Javascript
- How to use ‘do while loop’ in Java?
- Infinite while loop in Java
- What is do...while loop statement in JavaScript?
- Difference Between for and while loop
- Java while loop with Examples
- The while loop in Javascript
- How to use ‘while loop’ in Java?

Advertisements