Grade 10 IT Lesson Plan: While Loops in Java
Duration: 100 minutes
Book: Exploring IT by Funworks – Learning Unit 9 (pp. 171–186)
Environment: Java in NetBeans
Level: Beginner-friendly
Lesson Breakdown
Time Activity
0–10 min Warm-up and real-life examples
10–25 min Explain while loop concept + syntax
25–40 min Simple example: Count from 1 to 5
40–60 min Student Practice: Count from 1 to 10
60–75 min Input example: Keep asking until 0
75–90 min Debugging broken while loops
90–100 min Recap quiz + homework task
Student Notes: While Loops in Java
What is a while loop?
A while loop lets a program repeat some code as long as a condition is true.
Java syntax:
while (condition) {
// code to repeat
}
How it works:
- The loop checks the condition before it runs. - If the condition is true, it runs the code inside the
loop. - The loop repeats until the condition becomes false.
Key Points
1. Always start your variable before the loop.
2. Make sure the condition will become false, or the loop will run forever.
3. Update the variable inside the loop.
0–10 min: Warm-Up
Ask students:
- When do we repeat things in real life until something is true?
* Keep guessing until correct
* Keep walking until you reach school
* Keep asking until you get an answer
Link this to programming:
We use a while loop when we don’t know exactly how many times we need to repeat.
10–25 min: Teaching the While Loop
Code Example:
int x = 1;
while (x <= 5) {
[Link](x);
x = x + 1;
}
Explanation:
- int x = 1; → Start x at 1.
- while (x <= 5) → Run loop while x is 5 or less.
- [Link](x); → Print x to the screen.
- x = x + 1; → Increase x by 1.
Output: 1 2 3 4 5
25–40 min: Code Walkthrough
Open NetBeans. Create a class `WhileExample1`. Run the code above.
Ask: What happens if x = x + 1 is missing? Answer: Infinite loop.
Change condition to x < 3. What changes?
40–60 min: Student Practice
Task: Write a program that prints numbers 1 to 10 using a while loop.
int x = 1;
while (x <= 10) {
[Link](x);
x = x + 1;
}
Extensions: Count 5 to 15, count backwards 10 to 1.
60–75 min: Ask Until User Enters 0
Code Example:
import [Link];
public class AskUntilZero {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int number = -1;
while (number != 0) {
[Link]("Enter a number (0 to stop): ");
number = [Link]();
}
[Link]("Done.");
}
}
Explanation: Program keeps asking until input = 0. '!=' means not equal to.
75–90 min: Debugging Practice
Example 1: Infinite loop
int x = 1;
while (x <= 5) {
[Link](x);
// missing x = x + 1;
}
Problem: x never increases.
Fix: add x = x + 1;
Example 2: Loop never runs
int x = 10;
while (x < 5) {
[Link](x);
x--;
}
Problem: Condition false from start.
90–100 min: Recap Quiz
Questions:
1. What does a while loop do?
Answer: Repeats code while condition is true.
2. What happens if condition never false?
Answer: Infinite loop.
3. Fix this code:
int total = 0;
while (total < 100) {
[Link]("Total: " + total);
}
Answer:
int total = 0;
while (total < 100) {
[Link]("Total: " + total);
total = total + 10;
}
Homework (Optional)
Write a Java program that: - Asks the user to enter numbers. - Adds them together until the total is
100 or more. - Then prints the total and says 'Done.' Hint: Use Scanner for input, while loop to keep
asking, and a variable like total to add the numbers.
Summary for Students
- A while loop repeats code while the condition is true.
- You must: * Start the variable before the loop * Update the variable inside the loop * Stop the loop
by making the condition false