📓2.8: for Loops
Table of Contents
📖 This page is a condensed version of CSAwesome Topic 2.8
For Loops
Another type of loop in Java is a FOR loop. This is usually used when you know how many times you want the loop to execute. It is often a simple counter-controlled loop intended to run the loop body a set number of times.
A for loop is basically a specific instance of while loops, for counter-controlled repetition.
Three Parts of a For Loop
🔁 A for loop combines all 3 parts of writing a loop in one line to initialize, test condition, and change the loop control variable. The 3 parts are separated by semicolons (;):
// LOOP HEADER
for (initialize; test; change)
{
// LOOP BODY
}
The
forloop is like a shortcut way to write a counter-controlledwhileloop, with all 3 steps organized in one line.

📺 Watch the following video which compares a while loop and for loop, line by line.
Here is a control flow diagram for a for loop:

- The code in the initialization area is executed only one time before the loop begins
- The test condition is checked each time through the loop and the loop continues as long as the condition is
true- The loop control variable change is done at the end of each execution of the body of the loop, just like a
whileloop.- When the loop condition becomes
false, execution will continue at the next statement after the body of the loop.
Incrementing Loops
Two common patterns in for-loops are to count from 0 up to an number (using <) or count from 1 to a number including the number (using <=). Remember that if you start at 0 use <, and if you start at 1, use <=.
The two loops below using these two patterns both run 10 times:
// These loops both run 10 times
// If you start at 0, use <
for(int i = 0; i < 10; i++) {
System.out.println(i);
}
// If you start at 1, use <=
for(int i = 1; i <= 10; i++) {
System.out.println(i);
}
The variable
i(stands for index) is often used as a counter infor-loops.
Decrementing Loops
You can also count backwards in a loop starting from the last number and decrementing the loop counter down to 0 or 1. All 3 parts of the loop must change to count backwards including the test of when to stop.
For example,
for (int i=5; i > 0; i--)counts from 5 down to 1.
💬 DISCUSS: What do you think will happen when you run the code below? How would it change if you changed line 11 to initialize i’s value to 3?
String line1 = " bottles of pop on the wall";
String line2 = " bottles of pop";
String line3 = "Take one down and pass it around";
// loop 5 times (5, 4, 3, 2, 1)
for (int i = 5; i > 0; i--) {
System.out.println(i + line1);
System.out.println(i + line2);
System.out.println(line3);
System.out.println((i - 1) + line1);
System.out.println();
}
🐢 Coding Practice: Turtle Loops
Summary
- (AP 2.8.A.1) A
forloop is a type of iterative statement.- There are three parts in a for loop header: the initialization (of the loop control variable or counter), the boolean condition (testing the loop variable), and the update (to change the loop variable).
- (AP 2.8.A.2) In a
forloop, the initialization statement is only executed once before the first Boolean expression evaluation. The variable being initialized is referred to as a loop control variable. - (AP 2.8.A.2) The
forloop Boolean expression is evaluated immediately after the loop control variable is initialized and then followed by each execution of the increment (or update) statement until it is false. - (AP 2.8.A.2) In each iteration of the
forloop, the update is executed after the entire loop body is executed and before the Boolean expression is evaluated again. - (AP 2.8.A.3) A
forloop can be rewritten into an equivalentwhileloop (and vice versa).
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.