1️⃣ What Is a Loop?
A loop repeats code multiple times.
Instead of writing:
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
You can write:
for (int i = 0; i < 3; i++) {
System.out.println("Hello");
}
Big idea: Loops help you avoid repeating code by hand.
2️⃣ Why Do We Use Loops?
- Repeating actions
- Processing arrays and lists
- Validating user input
- Counting / totals
- Patterns (shapes, grids)
- Game logic
3️⃣ The Three Main Java Loops
| Loop Type | When To Use It |
|---|---|
| for | When you know how many times you want to repeat |
| while | When you repeat until something changes |
| do-while | When the loop must run at least once |
4️⃣ The for Loop
Structure
for (initialization; condition; update) {
// code repeats
}
Example
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
What each part means
| Part | Meaning |
|---|---|
| int i = 0 | Start a counter at 0 (runs once) |
| i < 5 | Keep looping while this is true |
| i++ | Update the counter each time (increase by 1) |
Counting backwards
for (int i = 5; i > 0; i--) {
System.out.println(i);
}
Skipping numbers
for (int i = 0; i <= 10; i += 2) {
System.out.println(i);
}
5️⃣ The while Loop
Structure
while (condition) {
// code repeats
}
Example
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
Key rule: Something inside the loop must change, or it may run forever.
6️⃣ The do-while Loop
Structure
do {
// code runs
} while (condition);
Example
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
Difference: A do-while loop runs at least once before checking the condition.
7️⃣ Infinite Loops ⚠️
An infinite loop never stops.
Example 1: Always true condition
while (true) {
System.out.println("Forever...");
}
Example 2: Forgetting to update
int i = 0;
while (i < 5) {
System.out.println(i);
// i++ is missing!
}
Warning: Infinite loops can freeze your program.
8️⃣ Loop Control: break and continue
break (stop the loop)
for (int i = 0; i < 10; i++) {
if (i == 5) break;
System.out.println(i);
}
continue (skip one iteration)
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
System.out.println(i);
}
9️⃣ Nested Loops
A nested loop is a loop inside another loop. Great for grids, tables, and 2D arrays.
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 4; col++) {
System.out.print("* ");
}
System.out.println();
}
Output:
* * * *
* * * *
* * * *
1️⃣0️⃣ Loops With Arrays
Standard for loop
int[] numbers = {10, 20, 30, 40};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
Enhanced for (for-each)
for (int num : numbers) {
System.out.println(num);
}
Use for-each when you just need to read values and don't need the index.
1️⃣1️⃣ Common Loop Mistakes
❌ Off-by-one error
// WRONG (crashes with ArrayIndexOutOfBoundsException)
for (int i = 0; i <= numbers.length; i++) {
System.out.println(numbers[i]);
}
// RIGHT
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
❌ Forgetting to update
int x = 0;
while (x < 10) {
System.out.println(x);
// x++ is missing
}
❌ Condition that never runs
// This loop runs 0 times because 10 is not less than 0
for (int i = 10; i < 0; i++) {
System.out.println(i);
}
1️⃣2️⃣ Real-World Example: Guessing Game
import java.util.Scanner;
public class GuessingGameDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int secret = 7;
int guess = 0;
while (guess != secret) {
System.out.print("Enter guess: ");
guess = input.nextInt();
}
System.out.println("Correct!");
input.close();
}
}
Why this is a
while loop: You don’t know how many guesses it will take.
1️⃣3️⃣ When To Choose Each Loop
| Situation | Best Loop |
|---|---|
| Count 1 to 100 | for |
| Ask user until correct | while |
| Menu must show at least once | do-while |
| Process an array | for or for-each |
1️⃣4️⃣ AP CSA Exam Tips
- Practice loop tracing (what prints? how many times?)
- Watch < vs <=
- Know nested loop outputs
- Understand variable scope inside loops
- Recognize infinite loops quickly
- Use array.length correctly
1️⃣5️⃣ Practice Problems
- Print numbers 1–50
- Print even numbers 1–100
- Print a multiplication table (1–12)
- Count how many vowels in a string
- Reverse a number
- Sum numbers from 1–100
- Print a triangle pattern using
*
Big Idea Summary: A loop is controlled repetition of code until a condition changes.