π4.5: Loops Analysis
Table of Contents
π This page is a condensed version of CSAwesome Topic 4.5
π Take notes in a Codespace during class, coding along with the instructor.
- Go to GitHub and click on your picture in the TOP RIGHT corner
- Select
Your repositories
- Open
CS2-Unit-4-Notes
- Now on your repository, click and select the
Codespaces
tab - Click
Create Codespace on main
(unless you already have one listed there), wait for the environment to load, then youβre ready to code!
Loop Analysis
In this lesson, you will practice tracing through code with loops and analyzing loops to determine how many times they run.
Tracing Loops
Letβs practice tracing through loops with many variables. Remember to make a tracing table to keep track of all the variables, the iterations, and the output.
Here is a complex loop. See if you can trace the code on paper by making a tracing table to predict what the code will do when you run it. Click on the this Java visualizer link to help you step through the code.
βοΈ Can you trace through this code? Write your tracing table on paper first, then test the code.
Add in output statements
System.out.println("var1: " + var1 + " var2: " + var2);
before the loop and inside the loop at the end to keep track of the variables and run.
int var1 = 3;
int var2 = 2;
while ((var2 != 0) && ((var1 / var2) >= 0))
{
var1 = var1 + 1;
var2 = var2 - 1;
}
β CHECK: Did your trace table look like the following?
Counting Loop Iterations
Loops can be also analyzed to determine how many times they run. This is called run-time analysis or a statement execution count.
βοΈ How many stars are printed out in this loop? How many times does the loop run? Figure it out on paper before you run the code.
for (int i = 3; i < 7; i++)
{
System.out.print("*");
}
If you made a trace table, you would know that the loop runs when i = 3, 4, 5, 6 but finishes as soon as i becomes 7 since that is not less than 7. So, the loop runs 4 times. Or you can use the shortcut formula in the note below.
π‘ The number of times a loop executes can be calculated by: (largestValue - smallestValue + 1)
- If the loop uses
counter <= limit
as the condition,limit
is the largest value. - If the loop uses
counter < limit
,limit-1
is the largest value that allows the loop to run.
In the code above the largest value that allows the loop to run is 6 (which is the largest value < 7) and the smallest value that allows the loop to execute is 3 so this loop executes (6 - 3 + 1 = 4 times).
βοΈ How many stars are printed out in this loop? How many times does the loop run? Figure it out on paper before you run the code.
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 10; col++)
{
System.out.print("*");
}
System.out.println();
}
The number of times a nested for loop body is executed is equal to the number of times the outer loop runs multiplied by the number of times the inner loop runs (OuterLoopRuns * InnerLoopRuns
).
For the example above, the outer loop executes 4 - 0 + 1 = 5 times and the inner 9 - 0 + 1 = 10 times so the total is 5 * 10 = 50.
Loop Analysis Game
π² Try the game below to practice loop analysis. Click on Loops and click on the number of times the loop runs. For an added challenge, try the check boxes for Backwards
and Nested
. We encourage you to work in pairs and see how high a score you can get.
βοΈ Summary
-
A trace table can be used to keep track of the variables and their values throughout each iteration of the loop.
-
We can determine the number of times a code segment will execute with a statement execution count. This is called run-time analysis.
-
The number of times a loop executes can be calculated by
largestValue - smallestValue + 1
where these are the largest and smallest values of the loop counter variable possible in the body of the loop. -
The number of times a nested for-loop runs is the number of times the outer loop runs times the number of times the inner loop runs.
π When class ends, donβt forget to SAVE YOUR WORK!
- Navigate to the
Source Control
menu on the LEFT sidebar - Type a brief commit message in the box, for example:
updated Main.java
- Click the button on the LEFT menu
- Click the button on the LEFT menu
- Finally you can close your Codespace!
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.