DEV Community

Cover image for Day 8: Understanding the `while` Loop in Java
Karthick Narayanan
Karthick Narayanan

Posted on

Day 8: Understanding the `while` Loop in Java

What is a while Loop?

A while loop is used when:

  • The number of iterations is not fixed
  • We want to repeat code until a condition becomes false

Syntax:

while (condition) {
    // code to be executed
}
Enter fullscreen mode Exit fullscreen mode

How it works:

  1. Condition is checked first
  2. If condition is true → loop executes
  3. After execution, condition is checked again
  4. If condition becomes false → loop stops

Order of Execution in a while Loop

  1. Initialization
    Loop control variable is initialized first.

  2. Condition Check
    The condition inside while() is checked.

  3. Loop Body Execution
    If the condition is true, the code inside the loop executes.

  4. Increment / Decrement
    Loop control variable is updated.

  5. Repeat
    Control goes back to step 2.

  6. Termination
    When the condition becomes false, the loop stops.

Important Point to Note:
while is an entry-controlled loop, so if the condition is false initially, the loop will not execute even once.


Key Points

  • while loop checks condition before execution
  • Used when the number of iterations is not fixed
  • An incorrect condition or missing increment leads to infinite loop
  • Mostly used for condition-based repetition
  • Condition is checked before execution
  • Used when iteration count is not known beforehand

Task 1: Print Numbers from 1 to 20

int i = 1;

while (i <= 20) {
    System.out.println(i);
    i++;
}
Enter fullscreen mode Exit fullscreen mode

Explanation:
The loop starts with i = 1. The while condition checks whether i is less than or equal to 20. If true, the current value of i is printed. After printing, i is increased by 1. This process continues until i becomes greater than 20, at which point the loop stops.


Task 2: Print All Odd Numbers from 1 to 50

int i = 1;

while (i <= 50) {
    if (i % 2 != 0) {
        System.out.println(i);
    }
    i++;
}
Enter fullscreen mode Exit fullscreen mode

Explanation:
The loop runs from 1 to 50. Inside the loop, the condition i % 2 != 0 checks whether the number is odd. If the condition is true, the number is printed. The variable i is incremented after each iteration, and the loop continues until i exceeds 50.


Task 3: Find the Sum of Even Numbers from 1 to 100

int i = 1;
int sum = 0;

while (i <= 100) {
    if (i % 2 == 0) {
        sum = sum + i;
    }
    i++;
}

System.out.println("Sum of even numbers: " + sum);
Enter fullscreen mode Exit fullscreen mode

Explanation:
The variable i starts from 1 and runs until 100. The variable sum is used to store the total. Inside the loop, only even numbers are added to sum. After the loop ends, the final value of sum is printed, which represents the sum of all even numbers from 1 to 100.


Task 4: Print the 7th Multiplication Table

int i = 1;

while (i <= 10) {
    System.out.println("7 x " + i + " = " + (7 * i));
    i++;
}
Enter fullscreen mode Exit fullscreen mode

Explanation:
The loop runs from 1 to 10. In each iteration, the value of i is multiplied by 7 and printed in table format. After printing each line, i is incremented by 1. The loop stops once i becomes greater than 10.


Task 5: Count How Many Numbers Are There from 1 to 100

int i = 1;
int count = 0;

while (i <= 100) {
    count++;
    i++;
}

System.out.println("Total count: " + count);
Enter fullscreen mode Exit fullscreen mode

Explanation:
The loop runs from 1 to 100. Each time the loop executes, the count variable is increased by 1. After the loop finishes, the value of count represents how many numbers are present between 1 and 100, which is then printed.


Common Mistakes to Avoid

  • Forgetting to increment/decrement loop variable → infinite loop
  • Writing wrong condition (< instead of <=)
  • Not initializing variables properly

When to Use a while Loop?

Use the while loop when:

  • You don’t know how many times loop should run
  • Repetition depends on a condition rather than a fixed count
  • Reading inputs, validation, counting problems

Top comments (0)