Hi there! Today, we’re going to talk about one of the most important tools in programming: the for loop. This loop allows you to repeat actions multiple times, saving time and making your code cleaner and easier to understand.
What is a for loop?
A for loop is used when you need to repeat the same action multiple times. For example, if you want to print numbers from 1 to 10, instead of writing 10 separate lines of code, you can accomplish this with a for loop.
Syntax of the for Loop
The for loop has the following structure:
for (initialization; condition; update) {
// code to be executed
}
Let’s break it down:
- Initialization: This is where you create a variable, usually called a counter. For example,
int i = 0;. - Condition: This checks whether the loop should continue running. As long as the condition is
true, the loop will keep running. - Update: This changes the counter variable, typically by increasing it (e.g.,
i++).
Example 1: Printing Numbers from 1 to 5
Let’s write a program that prints numbers from 1 to 5:
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Number: " + i);
}
}
}
What’s happening here:
- Initialization:
int i = 1;— the loop starts with the variableiset to 1. - Condition:
i <= 5;— the loop continues as long asiis less than or equal to 5. - Update:
i++— after each iteration,iis increased by 1. The loop stops wheniexceeds 5.
Example 2: Multiplication Table for 3
Now, let’s create a program that prints the multiplication table for 3:
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println("3 x " + i + " = " + (3 * i));
}
}
}
Output:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
...
3 x 10 = 30
Here, the variable i starts at 1 and goes up to 10. During each iteration, the program multiplies 3 by i and prints the result.
Nested Loops
You can also have loops inside other loops. Let’s create a multiplication table for numbers 1 to 3:
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) { // Outer loop
for (int j = 1; j <= 10; j++) { // Inner loop
System.out.println(i + " x " + j + " = " + (i * j));
}
System.out.println(); // Adds a blank line between tables
}
}
}
Example 3: Working with Arrays
The for loop is great for working with arrays. Here’s an example:
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println("Array element: " + numbers[i]);
}
}
}
Here, the loop runs through each element of the array and prints it.
The for-each Loop
Java also has a simpler loop for working with arrays and collections, called the for-each loop:
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int num : numbers) {
System.out.println("Array element: " + num);
}
}
}
What’s happening here:
int numis a temporary variable.: numberstells the loop to go through every element in thenumbersarray.- Each array element is assigned to
numin turn, and we print it.
When to Use a for Loop?
Use a for loop when:
- You know the exact number of times you need to repeat something (e.g., printing numbers from 1 to 10).
- You’re working with arrays or lists.
- You want to optimize your code and avoid writing the same action multiple times.
Homework
Try writing programs that:
- Print all even numbers from 1 to 20.
- Calculate the sum of numbers from 1 to 100.
Summing It Up
The for loop is a powerful tool for automating repetitive tasks. It makes your code more readable, efficient, and easy to modify. Once you master the for loop, you’ll be able to tackle a wide range of programming challenges with ease!
