When you find yourself in a situation where you know exactly how many times you want something to repeat, use a for loop!
Usually it is a simple counter-controlled loop which executes the code in the loop body a set number of times.
THREE PARTS OF A FOR LOOP
A for-loop combines all 3 parts of writing a loop in one line to initialize, test, and change the loop control variable.
The 3 parts are separated by semicolons (;).
Each of the three parts for a for loop declaration is optional, but the semicolons are not optional.
Personally, I have never tried writing a for loop without all three parts...
You can have a while loop that acts like a for loop and repeats a certain number of times.
However, the for loop is almost like a shortcut that has all three steps that you need in one line of code:
This short video (2:31) compares a while loop and a for loop line by line:
For a for loop, the initialization is only executed once 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 change to the loop control variable is done at the end of each execution of the body of the loop.
When the loop condition is false, execution will continue at the next statement after the body of the loop.
Here is a control flow diagram for a for loop:
The variable i (for index) is often used as a counter in for loops.
A common pattern in for loops is to count from 0 up to a number using <
It is also common to count from 1 to a number up to and including the number using <=
You don't always have to increment (add 1) or decrement (subtract 1) from the loop control variable.
You could use +=, -=, *=, /=, %= to change the loop control variable as well making for loops in Java very versatile.
For example, in the following for loop I changed i++ to i += 2 which increases the loop control variable by two for each iteration:
DECREMENTING LOOPS
You can also count backwards in a loop starting from the last number and decrementing down to 0 or 1.
All three parts of the loop must change to count backwards.
for example, for (int i = 5; i > 0; i--) will count down from 5 to 1.
Here is an example of decrementing a for loop to print out a pop song:
Output in the console:
The method .printPopSong() prints the words to the song.
It initializes the value of the variable i with the value 5 and checks if i is greater than 0.
Since 5 is greater than 0, the body of the loop executes.
Before the condition is checked again, i is decreased (decremented) by 1.
When the value stored in i is 0, the loop stops executing.
SUMMARY
There are three parts in a for loop header: the initialization, the test condition (a Boolean expression), and an increment or decrement statement to change the loop control variable.
In a for loop, the initialization statement is only executed once before the evaluation of the text Boolean expression. The variable being initialized is referred to as a loop control variable.
In each iteration of a for loop, the increment or decrement statement is executed after the entire loop body is executed and before the Boolean expression is evaluated again.
A for loop can be rewritten into an equivalent while loop and vice versa.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.