Welcome to our lesson on Java's for
loop. We use loops to repeat some action multiple times without the need to write the code for every iteration. A for
loop can be likened to a set task — say, visiting each planet in our solar system one by one. By the end of this journey, you will understand and be able to use the basic for
loop and the enhanced for
loop in Java.
Imagine lining up your favorite planet toys in order. You take one, put it in line, and repeat until you run out of toys. This is precisely how the basic for
loop works in Java!
Here's the syntax:
Java1for (initialization; condition; post-iteration actions) { 2 // Some task to do 3}
This loop does the following:
initialization
condition
is true, we keep executing the task
inside the loop bodypost-iteration actions
that change the state in some wayNow, let's demonstrate this with code that prints numbers 1 through 5:
Java1for (int i = 1; i <= 5; i++) { 2 // The following command will print the number i 3 System.out.println(i); 4} 5// Prints: 6// 1 7// 2 8// 3 9// 4 10// 5
Here, we defined an int
variable i
, assigned it to 1
first, and then repeated System.out.println(i);
while i <= 5
, incrementing i
by 1
after every iteration. By the way, i++
is a short form for i += 1
, which is i = i + 1
- so it's just adding 1
to the current value of i
, but in a short way! This operation is called increment.
Now, remember flipping pancakes? You pour one pancake, let it cook, then pour the next one, continuing until the batter is finished. Guess what? The enhanced for
loop operates in a similar fashion!
Here's an example of printing all elements of an array:
Java1int[] numbers = {5, 4, 3, 2, 1}; 2 3// The loop below will take each element num from numbers and print it 4for (int num: numbers) { 5 System.out.println(num); 6} 7// Prints: 8// 5 9// 4 10// 3 11// 2 12// 1
Quite the pancake flipper, isn't it?
You'd typically use a basic for
loop when you need to perform a task a specific number of times. In contrast, an enhanced for
loop is your go-to when you have a collection (like an array, list, map, or set) and need to perform an action for each item in that collection.
Let's illustrate with both loops printing numbers from an array:
Basic for loop:
Java1// This loop will print all numbers from the numbers array using its indices 2int[] numbers = {1, 2, 3, 4, 5}; 3 4// `i` goes through all array indices - 0, 1, 2, ..., numbers.length - 1 5for (int i = 0; i < numbers.length; i++) { 6 System.out.println(numbers[i]); 7} 8// Prints: 9// 1 10// 2 11// 3 12// 4 13// 5
Enhanced for loop:
Java1// This loop will print all numbers from the numbers array, directly taking each of them 2int[] numbers = {1, 2, 3, 4, 5}; 3 4for (int num: numbers) { 5 System.out.println(num); 6} 7// Prints: 8// 1 9// 2 10// 3 11// 4 12// 5
Congratulations, Space Explorer! You've mastered the Java for
loop! You can now loop over arrays just like visiting space planets!
Now, it's practice time! This will solidify your understanding and prepare you for the next lesson on nested loops. Let's continue our voyage in the programming universe! Happy coding!