1 of 17

Unit 6

Algorithms

2 of 17

To do: March 22 - Please write in your IN.

  • Nested loops are when a loop is located within another loop.
  • Each time the outer loop runs one time, the inner loop runs to completion.

3 of 17

Loop example #1

REPEAT 3 TIMES

{

snap once

clap once

}

4 of 17

Nested loop example #1

REPEAT 4 TIMES

{

REPEAT 3 TIMES

{

snap once

clap once

}

Step once to the right

}

5 of 17

Loop example #2

REPEAT 4 TIMES

{

DISPLAY("a")

}

6 of 17

Loop example #2

REPEAT 4 TIMES

{

DISPLAY("a")

}

OUTPUT

a a a a

7 of 17

Nested loop example #2

REPEAT 2 TIMES

{

REPEAT 4 TIMES

{

DISPLAY("a")

}

DISPLAY("b")

}

8 of 17

Nested loop example #2

REPEAT 2 TIMES

{

REPEAT 4 TIMES

{

DISPLAY("a")

}

DISPLAY("b")

}

OUTPUT

a a a a b a a a a b

9 of 17

Nested loop example #3 - How many a's will be printed?

REPEAT 5 TIMES

{

REPEAT 3 TIMES

{

DISPLAY("a")

}

}

10 of 17

Nested loop example #3 - How many a's will be printed? 15

REPEAT 5 TIMES

{

REPEAT 3 TIMES

{

DISPLAY("a")

}

}

11 of 17

Nested loops in the real world: clocks!

12 of 17

Nested loops MC question

In the following code segment, assume that x and y have been assigned integer values.

sum <- 0

REPEAT x TIMES{

REPEAT y TIMES{

sum <- sum + 1

}

}

At the end of which of the following code segments is the value of sum the same as the value of sum at the end of the preceding code segment? SELECT TWO ANSWERS.

A.

B.

C.

D.

13 of 17

Nested loops MC question

In the following code segment, assume that x and y have been assigned integer values.

sum <- 0

REPEAT x TIMES{

REPEAT y TIMES{

sum <- sum + 1

}

}

At the end of which of the following code segments is the value of sum the same as the value of sum at the end of the preceding code segment? SELECT TWO ANSWERS.

A.

B.

C.

D.

14 of 17

Go over homework:

  • Submit class work
    • Lists Warm-up #1-3, include 2 key words (problem and algorithm)
  • Algorithms U6L01 worksheet - submit image/pdf

15 of 17

Algorithms solve Problems

Problem: a general description of a task that can (or sometimes cannot) be solved with an algorithm

Algorithm: a finite set of instructions that accomplish a task.

  • There are usually many algorithms to solve the same problem.
  • And there are many ways to write or express an algorithm including natural language, pseudocode, diagrams, and a programming language.
  • All algorithms can be created by combining steps in three different ways.

16 of 17

Battleship Game - TWO rounds

17 of 17

Homework

  • Submit image/pdf of Battleship Game worksheet (round 1 and 2)
  • AP classroom - Iteration Practice #1-6
    • Complete online
    • Show work for each problem in IN and submit image/pdf