

Loop Analysis
Presentation
•
Computers
•
12th Grade
•
Practice Problem
•
Hard
Matthew Fahrenbacher
FREE Resource
7 Slides • 13 Questions
1
Loop Analysis (a Mr. F is absent experience)
2
Analyzing Loops Includes
Determining the execution count of different parts of a loop
An execution count is how many times a section of code is repeated.
Determining when (and if) a loop terminates
3
Fill in the Blanks
4
Fill in the Blanks
5
Fill in the Blanks
6
Why is the last answer 5?
for (int i = 3; i < 7; i++) {
++System.out.print("*");
}
The table outlines the order of commands in the loop.
1st: | Make i = 3 | Is i < 7? | Print * | Increase i by 1 |
2nd: | (i is now 4) | is i < 7? | Print * | Increase i by 1 |
3nd: | (i is now 5) | is i < 7? | Print * | Increase i by 1 |
4th: | (i is now 6) | is i < 7? | Print * | Increase i by 1 |
5th: | (i is now 7) | is i < 7? | STOP! | |
Notice that the question "is i < 7" is asked one more time than the number of stars - that extra time is when it determines the loop should end.
7
Fill in the Blanks
8
Multiple Choice
How many stars are printed by this code?
6
5
4
3
An infinite number
9
Fill in the Blanks
10
Fill in the Blanks
11
Multiple Choice
What does this code print?
A rectangle of 8 rows with 5 stars per row.
A rectangle of 8 rows with 4 stars per row.
A rectangle of 6 rows with 5 stars per row.
A rectangle of 6 rows with 4 stars per row.
12
Why?
A rectangle of 6 rows with 5 stars per row.
for (int i = 3; i <= 8; i++) {
++for (int y = 2; y < 7; y++) {
++++System.out.print("*");
++}
++System.out.println();
}
++
Outside loop is the number of rows:
i goes through these values: 3-4-5-6-7-8, or a total of 6
Inside loop is the number of stars per row:
y goes through these values: 2-3-4-5-6, or a total of 5
*****
*****
*****
*****
*****
13
Fill in the Blanks
14
Why 18?
for (int i = 3; i <= 6; i++) {
++for (int j = 9; j > i; j--) {
++++System.out.print("*");
++}
++System.out.println();
}++++
When i is 3... | Inside Loop: 9 to 4 | 9-8-7-6-5-4 means 6 stars |
When i is 4... | Inside Loop: 9 to 5 | 9-8-7-6-5 means 5 stars |
When i is 5... | Inside Loop: 9 to 6 | 9-8-7-6 means 4 stars |
When i is 6... | Inside Loop: 9 to 7 | 9-8-7 means 3 stars |
6 + 5 + 4 + 3 = 18
15
Fill in the Blanks
16
Why 10?
int i = 0;
while (i <= 4) {
++for (int j = i+1; j < 5; j++) {
++++System.out.println("Hi!");
++}
++i++;
}
When i is 0... | Inside Loop: 1 to 4 | 1-2-3-4 means 4 Hi!'s |
When i is 1... | Inside Loop: 2 to 4 | 2-3-4 means 3 Hi!'s |
When i is 2... | Inside Loop: 3 to 4 | 3-4 means 2 Hi!'s |
When i is 3... | Inside Loop: 4 to 4 | 4 means 1 Hi!'s |
When i is 4... | Inside loop starts at 5 - loop condition is false immediately! | |
4 + 3 + 2 + 1 = 10
17
Multiple Choice
Which Java expression below will correctly represent the number of stars printed by the given code segment?
(n+1)/2
n/2
(n-1)/2
n
n-1
18
Why (n+1)/2?
int n = ...; // n>=0
for(int i = 0; i < n; i++) {
++if(i % 2 == 0) {
+++System.out.print("*");
++}
}
The code prints stars for all the even numbers from 0 to n (not including n). And abot half the values between 0 and n are even.
- if n is 5, it should print stars for the numbers 0, 2, and 4 (so 3 stars)
- if n is 6, it should also print stars for the numbers 0, 2 and 4 (so still 3 stars)
(5 + 1) / 2 evaluates to 3
(6 + 1) /2 also evaluates to 3 because of integer division!
For problems like this, it's a good idea to test values of n (especially even and odd values)
19
Fill in the Blanks
20
Fill in the Blanks
Loop Analysis (a Mr. F is absent experience)
Show answer
Auto Play
Slide 1 / 20
SLIDE
Similar Resources on Wayground
15 questions
KD. 3.1 INFORMATIKA KELAS 9 PENGENALAN BROWSER
Presentation
•
12th Grade
14 questions
Divisão com números decimais
Presentation
•
12th Grade
15 questions
Bài 11. Tin 12
Presentation
•
12th Grade
16 questions
Escucha activa
Presentation
•
12th Grade
16 questions
confuguracion de página en word
Presentation
•
12th Grade
15 questions
Snake Python
Presentation
•
KG
16 questions
1°A TERESA MARTIN 04.02.2021
Presentation
•
12th Grade
15 questions
PAST PERFECT
Presentation
•
12th Grade
Popular Resources on Wayground
11 questions
Hallway & Bathroom Expectations
Quiz
•
6th - 8th Grade
10 questions
HCS SCI 03 Summer School Assessment 2
Quiz
•
3rd Grade
11 questions
Home Scope
Quiz
•
7th - 8th Grade
12 questions
2026 TAP Technology in the Classroom
Presentation
•
Professional Development
15 questions
HCS SCI 05 Summer School Assessment 2 Review
Quiz
•
5th Grade
15 questions
HCS SCI 04 Summer School Review 2
Quiz
•
4th Grade
59 questions
Geometry Unit 3 Review
Quiz
•
9th - 12th Grade
14 questions
FAST ELA READING SMAPLE TEST MATERIALS
Passage
•
3rd Grade