2️⃣

8.2 For-Loops

Chapter Table of Contents

Ch. 8 Loops

Section Table of Contents

 
Analyze the code below, which utilizes a basic for loop.
int n = 10; // prints "Hello World" n times for (int count = 0; count < n; count++) { System.out.println("Hello World"); }
 
Output:
Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World
Let’s focus on the highlighted portion of the code. All for loops take in three inputs separated by semicolons.

Initialization

The first input initializes the for loop’s control variable. Like with any variable, you can name it however you wish. Typically, people will call it i, but for the purposes of readability it is called count in the example above. You can also assign any value to the control variable (as long as it works as intended, of course). In this case, count is set to 0 at the beginning of the for loop.
 

Condition

The second input indicates a boolean expression. As long as the expression evaluates to true, the code within the for loop will be executed. In this case, I have specified that as long as count is less than n, the code inside of the for loop will be executed. Note that this condition is checked at the beginning of each iteration (each cycle) of the for loop.
 

Action

The last input indicates the action that takes place after each iteration of the loop. Usually, the action changes the control variable so that at some point the condition is false and the loop terminates. In this case, the action is count++, which means that after each iteration, the count variable is incremented by 1. Thus, it will eventually equal the value of n, which means the condition count < n will be false and the loop ends.
 

Putting It All Together

The code that is inside of the for loop’s curly braces runs during each iteration. In this case, there is only 1 statement, which prints “Hello World” to the console. Let’s look at what happens to the control variable and the current output to the console during each iteration:
 
 
Notice that when count == 10count < n is false, so the for loop terminates after printing “Hello World” 10 times.
 
💡
Tip: When analyzing code involving loops, it often helps to make note of what changes after each iteration.
 

Examples

Here’s a slightly more complicated piece of code involving a for loop. What do you think the output will be?
import java.util.Scanner; Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int num = input.nextInt(); for (int i = 0; i < num; i++) { System.out.println(num - i); } System.out.println();
 
If you said it prints numbers in descending order, you’re right.
Sample output:
Enter an integer: 10 10 9 8 7 6 5 4 3 2 1
 
Alternatively, you could also use the decrement operator in the for loop and it would work the same.
for (int i = num; i > 0; i--) { System.out.println(i); }
⚠️
Note: Remember that all variables have a scope, or region of the code, that they belong to. The control variable’s scope is only inside of that for loop’s curly braces. If I tried to print the value of i outside of the for loop, it would give me an error.
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.