 # 15.07 Loops in code

Worksheet
Loops
1

Consider the following pseudocode:

a

Find the initial value of a.

b

Find the final value of a.

c

Find the value of \text{OUTPUT}.

\text{START} \\ \text{SET } a = 9 \\ \text{SET sum = 0} \\ \text{WHILE } a \gt 3\\ \quad \text{sum = sum} + a\\ \quad a = a - 1 \\ \text{END WHILE} \\ \text{PRINT sum} \\ \text{END}

2

For each of the following pseudocodes:

i

Find the initial value of a.

ii

Find the \text{OUTPUT} of the code.

a

\text{START} \\ \text{SET } a = 2 \\ \text{SET sum} = 0 \\ \text{WHILE a < 9} \\ \quad \text{sum = sum} + a \\ \quad a = a + 1 \\ \text{END WHILE} \\ \text{PRINT sum} \\ \text{END}

b

\text{START} \\ \text{SET } a = 3 \\ \text{SET sum} = 0 \\ \text{SET } n = 0 \\ \text{WHILE } a \lt 12 \\ \quad \text{sum = sum + a} \\ \quad a = a + 1 \\ \quad n = n + 1 \\ \text{END WHILE} \\ \text{PRINT sum / } n \\ \text{END}

3

Find the final value, n, for the following pseudocode:

\text{START} \\ \text{SET } a = 4 \\ \text{SET } n = 0 \\ \text{WHILE } a \gt 1 \\ \quad \text{sum = sum + a} \\ \quad a = a \, / \, 2 \\ \quad n = n + 1 \\ \text{END WHILE} \\ \text{END}

4

Determine the value that is printed at the end of the following pseudocodes:

a

\text{START} \\ \text{SET } a = 19 \\ \text{WHILE } a \gt 5 \\ \quad a = a - 1 \\ \text{END WHILE} \\ \text{PRINT } a \\ \text{END}

b

\text{START} \\ \text{SET } a = 1 \\ \text{WHILE } a \lt 24\\ \quad \text{PRINT } a \\ \quad a = a * 2 \\ \text{END WHILE} \\ \text{END}

c

\text{START} \\ \text{SET } a = 5 \\ \text{WHILE } a \lt 7 \\ \quad \text{PRINT } a \\ \quad a = a + 1 \\ \text{END WHILE} \\ \text{END}

d

\text{START} \\ \text{SET } a = 2 \\ \text{WHILE } a \lt 10 \\ \quad \text{PRINT } a \\ \quad a = a + 2 \\ \text{END WHILE} \\ \text{END}

e

\text{START} \\ \text{SET } a = 5 \\ \text{WHILE } a \lt 20 \\ \quad \text{PRINT } a \\ \quad a = a * 2 \\ \text{END WHILE} \\ \text{END}

5

Consider the algorithm represented by the flow chart:

Arrange the following lines of pseudocode in the correct order for the algorithm.

6

Consider the algorithm represented by the flow chart:

Arrange the following lines of pseudocode in the correct order for the algorithm.

7

Consider the algorithm for finding the sum of the first n natural numbers represented by the flow chart.

a

Find the output of this algorithm if n=4.

b

Arrange the following lines of pseudocode for adding the first 50 natural numbers in the correct order for the algorithm.

8

The factorial of a non-negative integer n is given by n! = n \times (n-1) \times (n-2) \times .... \times 2 \times 1. For example, 5! = 5 \times 4 \times 3 \times 2 \times 1=120.

Consider the algorithm for finding n! represented by the flow chart:

a

Find the output of this algorithm if n=6.

b

Arrange the following lines of pseudocode in the correct order for finding 50!.

### Outcomes

#### 7.C3.1

Solve problems and create computational representations of mathematical situations by writing and executing efficient code, including code that involves events influenced by a defined count and/or sub-program and other control structures.

#### 7.C3.2

Read and alter existing code, including code that involves events influenced by a defined count and/or sub-program and other control structures, and describe how changes to the code affect the outcomes and the efficiency of the code.