← Back to all posts

10 Java Fundamentals

For Loop in Java

By Dummy for Dummies


Introduction

Remember what we learned in English? "Since" is used for a specific point in time. "For" is used for a period of time.

Coding works the same way. While Loop is built around a specific point, like "did the child get candy?" For Loop is built around a set period, like "run this 10 times" or "run this 5 times." That's the core difference, and why each one fits different jobs.


For Loop

For Loop has a few more things though. It's like a time bomb, so you have to set a few things before it becomes functional: when to start, when to stop, and how to count from start to stop.

Java
for (counter ; condition ; update) {
    // write the code here to repeat
}

You set a counter, then set a condition, then it'll update, then the loop code will run.

Note: The update happens after the loop body runs, right before the next check.

Java
public class ForLoopDemo {
    public static void main(String[] args) {
        // Print numbers 1 to 5
        for (int i = 1; i < 6; i++) {
            System.out.println("Number: " + i);
        }
        System.out.println("Loop exited!");
    }
}
Console Output
Number: 1 Number: 2 Number: 3 Number: 4 Number: 5 Loop exited!

Reverse For Loop

For loop is not always ascending like from 0 to 10 or 0 to 5. You can actually code a for loop that starts from 10 and comes down to 0, just like a time bomb.

Java
public class ReverseForLoop {
    public static void main(String[] args) {
        // Print numbers 5 to 1
        for (int i = 5; i > 0; i--) {
            System.out.println("Number: " + i);
        }
        System.out.println("Loop exited!");
    }
}
Console Output
Number: 5 Number: 4 Number: 3 Number: 2 Number: 1 Loop exited!

Points to Remember

Java
public class EvenNumbers {
    public static void main(String[] args) {
        // Print even numbers from 2 to 10
        for (int i = 2; i <= 10; i += 2) {
            System.out.println("Even: " + i);
        }
        System.out.println("Done printing evens!");
    }
}
Console Output
Even: 2 Even: 4 Even: 6 Even: 8 Even: 10 Done printing evens!

It prints up to 10, because the condition is i <= 10. Therefore, as long as i is less than or equal to 10, the loop will run.


Exercise

Let's do an exercise for what we studied in this post. Study this code, break it down, analyze it and identify the concepts used here. It would be even better if you write down your observations and make a mini report on it.

Java
import java.util.Scanner;

public class ForLoopExercise {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("  For Loop Practice  ");
        System.out.print("Enter a small positive integer (n): ");
        int n = sc.nextInt();
        System.out.print("Enter a number for multiplication table (m): ");
        int m = sc.nextInt();

        // 1) Count from 1 to n
        System.out.println("\n1) COUNT FROM 1 TO N");
        for (int i = 1; i <= n; i++) {
            System.out.println("Number: " + i);
        }

        // 2) Even numbers from 2 to n (step update)
        System.out.println("\n2) EVEN NUMBERS FROM 2 TO N (i += 2)");
        for (int i = 2; i <= n; i += 2) {
            System.out.println("Even: " + i);
        }

        // 3) Reverse count from n to 1
        System.out.println("\n3) REVERSE COUNT FROM N TO 1");
        for (int i = n; i >= 1; i--) {
            System.out.println("Rev: " + i);
        }

        // 4) Sum and average of 1..n
        System.out.println("\n4) SUM AND AVERAGE OF 1..N");
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        System.out.println("Sum: " + sum);
        System.out.printf("Average: %.2f\n", (n > 0) ? (double) sum / n : 0.0);

        // 5) Multiplication table for m (1..10)
        System.out.println("\n5) MULTIPLICATION TABLE OF " + m);
        for (int i = 1; i <= 10; i++) {
            System.out.println(m + " x " + i + " = " + (m * i));
        }

        sc.close();
    }
}
Console Output
For Loop Practice Enter a small positive integer (n): 5 Enter a number for multiplication table (m): 3 1) COUNT FROM 1 TO N Number: 1 Number: 2 Number: 3 Number: 4 Number: 5 2) EVEN NUMBERS FROM 2 TO N (i += 2) Even: 2 Even: 4 3) REVERSE COUNT FROM N TO 1 Rev: 5 Rev: 4 Rev: 3 Rev: 2 Rev: 1 4) SUM AND AVERAGE OF 1..N Sum: 15 Average: 3.00 5) MULTIPLICATION TABLE OF 3 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30

Mini Project: For Loop Multiplication Table

Your project is to create a simple Java program that prints the multiplication table of any number using a for loop. The program should ask the user to enter a number. Then it should use a for loop to print the table of that number from 1 to 10.

Example: If the user enters 6, the output should be:

While writing the program, make sure you use:


Example Output

Console Output
Enter a number for the multiplication table: 6 Multiplication table of 6: 6 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36 6 x 7 = 42 6 x 8 = 48 6 x 9 = 54 6 x 10 = 60

Closing

That's it for For Loop in Java. With For Loop you have control over where to start, when to stop, and how to count, whether you're counting up, counting down, or skipping numbers.

← Previous 09 - While Loop in Java Next → 11 - Nested Loops in Java