codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

15 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java While Loop
☕Java Programming

Java While Loop

Updated 2026-05-12
30 min read

Java While Loop

In this tutorial, you'll learn about two types of loops in Java: the while loop and the do-while loop. These loops are essential for executing a block of code repeatedly as long as a specified condition is true. Understanding how to use these loops will help you write more efficient and effective Java programs.

Introduction

Loops are fundamental constructs in programming that allow you to repeat a set of instructions until a specific condition is met. The while loop and the do-while loop are both used for this purpose, but they have subtle differences in how they operate. In this tutorial, we'll explore these loops with examples and best practices.

The While Loop

The while loop repeatedly executes a block of code as long as a specified condition is true. It checks the condition before executing the loop body.

Syntax

Java
1while (condition) {
2 // Code to be executed
3};
  • Condition: This is an expression that evaluates to a boolean value (true or false). The loop continues to execute as long as this condition remains true.
  • Code Block: This is the block of code that will be executed repeatedly.

Example 1: Basic While Loop

Let's look at a simple example where we use a while loop to count from 1 to 5.

WhileLoopExample.java
1public class WhileLoopExample {
2 public static void main(String[] args) {
3 int i = 1;
4 while (i <= 5) {
5 System.out.println("Count: " + i);
6 i++;
7 }
8 }
9}
Output
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Explanation

In this example, the variable i is initialized to 1. The while loop checks if i is less than or equal to 5. If the condition is true, it prints the current value of i and increments i by 1. This process repeats until i becomes greater than 5.

Common Mistakes

  • Infinite Loop: One common mistake is creating an infinite loop where the condition never becomes false. For example:
InfiniteLoop.java
1public class InfiniteLoop {
2 public static void main(String[] args) {
3 int i = 1;
4 while (i <= 5) {
5 System.out.println("Count: " + i);
6 }
7 }
8}

To avoid infinite loops, ensure that the loop variable is updated correctly within the loop body.

The Do-While Loop

The do-while loop is similar to the while loop, but it executes the loop body at least once before checking the condition. This means that even if the condition is false initially, the loop will execute at least one time.

Syntax

Java
1do {
2 // Code to be executed
3} while (condition);
  • Code Block: The block of code inside the do statement is executed first.
  • Condition: This is an expression that evaluates to a boolean value. The loop continues to execute as long as this condition remains true.

Example 2: Basic Do-While Loop

Here's an example where we use a do-while loop to count from 1 to 5.

DoWhileLoopExample.java
1public class DoWhileLoopExample {
2 public static void main(String[] args) {
3 int i = 1;
4 do {
5 System.out.println("Count: " + i);
6 i++;
7 } while (i <= 5);
8 }
9}
Output
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Explanation

In this example, the variable i is initialized to 1. The loop body executes first, printing "Count: 1". Then it checks if i is less than or equal to 5. If the condition is true, it continues to execute the loop body and increments i. This process repeats until i becomes greater than 5.

Key Differences

  • Execution Order: The do-while loop always executes at least once because it checks the condition after executing the loop body.
  • Use Case: Use a do-while loop when you want to ensure that the code block is executed at least once, regardless of the initial condition.

Practical Example: Summing Numbers

Let's create a practical example where we use both while and do-while loops to sum numbers from 1 to 10.

SumNumbers.java
1public class SumNumbers {
2 public static void main(String[] args) {
3 int number = 1;
4 int sumWhile = 0;
5
6 // Using while loop
7 while (number <= 10) {
8 sumWhile += number;
9 number++;
10 }
11 System.out.println("Sum using while loop: " + sumWhile);
12
13 // Reset variables for do-while loop
14 number = 1;
15 int sumDoWhile = 0;
16
17 // Using do-while loop
18 do {
19 sumDoWhile += number;
20 number++;
21 } while (number <= 10);
22 System.out.println("Sum using do-while loop: " + sumDoWhile);
23 }
24}
Output
Sum using while loop: 55
Sum using do-while loop: 55

Explanation

In this example, we first use a while loop to sum numbers from 1 to 10. Then we reset the variables and use a do-while loop to perform the same task. Both loops produce the same result, demonstrating their similarity in functionality.

Summary

Loop TypeSyntaxExecution Order
While Loopwhile (condition) { ... }Checks condition before execution
Do-While Loopdo { ... } while (condition);Executes at least once, checks condition after

Key Takeaways

  • While Loop: Repeats as long as the condition is true. It may not execute at all if the condition is false initially.
  • Do-While Loop: Executes at least once and repeats as long as the condition is true after the first execution.

What's Next?

Now that you've learned about while loops and do-while loops, you're ready to explore another type of loop in Java: the for loop. The for loop is often used when the number of iterations is known beforehand. In the next tutorial, we'll dive into how to use for loops effectively in your Java programs.

Stay tuned for more Java tutorials!


PreviousJava SwitchNext Java For Loop

Recommended Gear

Java SwitchJava For Loop