0Pricing
Java Academy · Lesson

While Loop Basics

Understand while basics: init, condition, update, patterns, and avoiding infinite loops.

While Loop Basics is a free Java Academy lesson on CoddyKit — lesson 1 of 2. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 2 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Loop Concept

What is a loop? A loop repeats a block while a condition is true. Use it to avoid writing the same line many times.

  • Start with a clear goal
  • Decide what to repeat
  • Stop when the task is done

While Syntax

While syntax

int count = 1;
while (count <= 3) {
System.out.println("Loop " + count);
count = count + 1;
}

Condition is checked before each iteration.

Init/Cond/Update

Three parts

  • Initialization: set a start value
  • Condition: when to continue
  • Update: move toward stopping
int i = 0;
while (i < 5) {
// work
i = i + 1;
}

Loop Patterns

Patterns

  • Counting up or down
  • Accumulating a sum
  • Searching until found (simulate with flags)
int sum = 0;
int n = 5;
while (n > 0) {
sum = sum + n;
n = n - 1;
}

Avoid Infinite

Avoid infinite loops

  • Make sure the update changes the variable in the condition
  • Double check boundary logic (< vs <=)
  • Use small prints to trace progress
int x = 3;
while (x > 0) {
x = x - 1; // update present
}

While Demo

Run it: See a countdown sum and a simple repeating message. No console input required.

public class Main {
  public static void main(String[] args) {
    int n = 5;
    int sum = 0;

    while (n > 0) {
      sum = sum + n;
      n = n - 1;
    }

    System.out.println("Sum 1..5 = " + sum);

    int i = 1;
    while (i <= 3) {
      System.out.println("Ping " + i);
      i = i + 1;
    }
  }
}

Countdown Check

Quick check: Which loop prints 3, 2, 1 on separate lines and then stops?

Recap & Next

Recap: You practiced while syntax, the init-condition-update trio, common patterns, and how to avoid infinite loops.

Next: Learn do-while to run the body at least once.

Frequently asked questions

Is the “While Loop Basics” lesson free?

Yes — the full text of “While Loop Basics” is free to read here on the web, and the Java Academy course includes 2 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “While Loop Basics”?

Understand while basics: init, condition, update, patterns, and avoiding infinite loops. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 2, so you can start here or from the beginning and move at your own pace.

How long does the “While Loop Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. While Loop Basics
  2. Do-While & When to Use It
← Back to Java Academy