Java uses the brackets ({}
) to indicate which lines of code are part of a for loop and which are not.
Take this code for example:
String[] languages = {"Python", "Java", "JavaScript"};
for (String language : languages) {
System.out.println("I love " + language);
System.out.println("I want to get better at it");
}
Both System.out.println
statements are part of the loop since they are written inside the brackets.
This means that both messages will be printed for every language
in the languages
list. The output of that code would be:
I love Python
I want to get better at it
I love Java
I want to get better at it
I love JavaScript
I want to get better at it
As for this code:
String[] languages = {"Python", "Java", "JavaScript"};
for (String language : languages) {
System.out.println("I love " + language);
}
System.out.println("I want to get better at them");
The System.out.println("I want to get better at them");
is not part of the loop.
This means that only System.out.println("I love " + language);
will be executed for every language
in the languages
list.
Then, when the for loop is over, the System.out.println("I want to get better at them");
will be executed only once.
The output of this program would be:
I love Python
I love Java
I love JavaScript
I want to get better at them
Execution flow
Lastly, note that the execution of a program always begins on the first line. The code is then executed one line at a time from top to bottom. This is known as execution flow and is the order a program in Java executes code.
For example, this program:
String[] fruits = {"banana", "apple", "kiwi"};
System.out.println("I'm hungry!");
for (String fruit : fruits) {
System.out.println("I'm gonna eat:");
System.out.println(fruit);
}
System.out.println("I love my life!");
prints:
I'm hungry!
I'm gonna eat:
banana
I'm gonna eat:
apple
I'm gonna eat:
kiwi
I love my life!
Assignment:
Add a for loop inside our code in the editor such that the final program would print:
Today we'll learn about:
Python
Java
JavaScript
Let's code!
To achieve the desired output, we need to add a for loop that iterates over the array of languages and prints each language. Here is the detailed solution:
public class Main {
public static void main(String[] args) {
// Define an array of languages
String[] languages = {"Python", "Java", "JavaScript"};
// Print the initial statement
System.out.println("Today we'll learn about:");
// For loop to iterate over each language in the array
for (String language : languages) {
// Print each language
System.out.println(language);
}
// Print the final statement
System.out.println("Let's code!");
}
}
Let's break down the code step-by-step:
languages
that contains the elements "Python", "Java", and "JavaScript".System.out.println
to print "Today we'll learn about:". This statement is outside the loop, so it will be printed only once.languages
array. The loop variable language
will take the value of each element in the array one by one.System.out.println
to print the current value of the language
variable. This will print each language on a new line.System.out.println
to print "Let's code!". This statement is also outside the loop, so it will be printed only once.The output of the program will be:
Today we'll learn about:
Python
Java
JavaScript
Let's code!
In this lesson, we learned about the execution flow of a for loop in Java. We saw how to use brackets to define the scope of the loop and how to iterate over an array using a for-each loop. Understanding the execution flow is crucial for writing correct and efficient code. By practicing with different examples, you can master the use of loops in Java and apply them to solve various programming problems.