Search Header Logo
Loop Analysis

Loop Analysis

Assessment

Presentation

Computers

12th Grade

Practice Problem

Hard

Created by

Matthew Fahrenbacher

FREE Resource

7 Slides • 13 Questions

1

media

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

media image

4

Fill in the Blanks

media image

5

Fill in the Blanks

media image

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

media image

8

Multiple Choice

Question image

How many stars are printed by this code?

1

6

2

5

3

4

4

3

5

An infinite number

9

Fill in the Blanks

media image

10

Fill in the Blanks

media image

11

Multiple Choice

Question image

What does this code print?

1

A rectangle of 8 rows with 5 stars per row.

2

A rectangle of 8 rows with 4 stars per row.

3

A rectangle of 6 rows with 5 stars per row.

4

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

media image

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

media image

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

Question image

Which Java expression below will correctly represent the number of stars printed by the given code segment?

1

(n+1)/2

2

n/2

3

(n-1)/2

4

n

5

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

media image

20

Fill in the Blanks

media image
media

Loop Analysis (a Mr. F is absent experience)

Show answer

Auto Play

Slide 1 / 20

SLIDE