Control Flow Lesson 13 of 21
Java for Loops and Nested Loops
Count with for loops, step by different amounts, compute sums and factorials, build multiplication tables and star patterns with nested loops.
The for loop is perfect when you know how many times to repeat: 10 times, once for each student, once for each day of the week. It puts the counter, the condition and the step all in one line.
Syntax
for (initialization; condition; update) {
// code to repeat
}
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Student #" + i);
}
}
}
Output
Student #1 Student #2 Student #3 Student #4 Student #5
int i = 1runs once, at the start.i <= 5is checked before every repetition.i++runs after every repetition.
Counting in other ways
public class Main {
public static void main(String[] args) {
for (int i = 10; i >= 1; i--) System.out.print(i + " ");
System.out.println("Liftoff! 🚀");
for (int i = 0; i <= 100; i += 25) System.out.print(i + " ");
System.out.println();
}
}
Output
10 9 8 7 6 5 4 3 2 1 Liftoff! 🚀 0 25 50 75 100
Sums and factorials
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println("1 + 2 + ... + 100 = " + sum);
long factorial = 1;
int n = 10;
for (int i = 2; i <= n; i++) {
factorial *= i;
}
System.out.println(n + "! = " + factorial);
}
}
Output
1 + 2 + ... + 100 = 5050 10! = 3628800
We used long for the factorial because factorials grow very fast. 13! is already too big for an int.
Scope of the counter
A variable declared in the for line exists only inside the loop. Using i after the loop gives "cannot find symbol". If you need the value afterward, declare it before the loop.
Nested loops: multiplication table
public class Main {
public static void main(String[] args) {
for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= 5; col++) {
System.out.printf("%4d", row * col);
}
System.out.println();
}
}
}
Output
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
For each row, the inner loop runs completely. %4d gives each number a width of 4 so the columns line up.
Star patterns
Patterns are a classic way to practice nested loops:
public class Main {
public static void main(String[] args) {
int n = 4;
// Right triangle
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) System.out.print("*");
System.out.println();
}
// Pyramid
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.println();
}
}
}
Output
* ** *** **** * *** ***** *******
For the pyramid, each row needs n - i spaces and 2i - 1 stars. Figuring out formulas like these is great practice for logical thinking.
Prime number check
public class Main {
public static void main(String[] args) {
System.out.print("Primes up to 50: ");
for (int n = 2; n <= 50; n++) {
boolean isPrime = true;
for (int d = 2; d * d <= n; d++) {
if (n % d == 0) {
isPrime = false;
break;
}
}
if (isPrime) System.out.print(n + " ");
}
System.out.println();
}
}
Output
Primes up to 50: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
We only test divisors up to the square root (d * d <= n), which makes the check much faster.
The enhanced for loop
To go through every item of an array, Java has a shorter "for-each" loop: for (String name : names). You'll use it in the Arrays lesson.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04