# OCR computing Control Structures

These cards give a basic description for the 3 control structures needed for the OCR computing AS level exam F452 with pseudo-code examples.

HideShow resource information

## Sequence

Sequence

• The easiest Control structure to follow, it is where the lines of code are executed once in turn in the order they are written.
• eg:

﻿START

INPUT Num1

INPUT Num2

OUTPUT Num1 + num2

STOP

1 of 9

## Selection

Selection

• This is where lines of code are selected to be run, this is by an IF statement of a SELECT...CASE.
• eg:

INPUT Num1 and Num2

IF Num1 > =Num 2 THEN

OUTPUT Num1 - NUM

ELSE

OUTPUT Num2 - NUM 1

END IF

2 of 9

## Iteration

Iteration

• This is where code may be executed once, more than once or not at all, depending on a condition or a counter. (also called a loop or repetition).
• There are two main types of loops; condition controlled and count controlled.
• The two condition controlled are REPEAT...UNTIL and WHILE Loop.
• The REPEAT...UNTIL loop tests the condition (usually a variable) at the end of each loop, and will exit if true.
• A WHILE loop will test the condition at the Start of the loop and exit if False. This means that the statements within a WHILE loop may never be executed.
• The other loop is a FOR...NEXT loop, which uses a variable as a counter to keep track of the iteration.
• it is common for the counters to be called i, j or k in a FOR...NEXT Loop.
3 of 9

Iteration (Contd.)

• Recursion is another example of iteration, and is where a Function calls itself whilst it is running.
• A stopping condition is used so the function will stop calling itself completely and give an answer.
• Recursion takes up more resources, but the algorithm is usually easier to understand as it is more like how a human will describ a process.
• A recursive algorithm will usually have fewer lines of code than the equivalent algorithm in a loop.
4 of 9

Iteration Examples

REPEAT...UNTIL

INPUT Num1 and Num2

Num3 = Num1

REPEAT

Num3 = Num3 - Num2

UNTIL NUM3 <=0

5 of 9

Iteration Examples (Contd.)

FOR...NEXT

INPUT String1 and Character1

FOR i = 1 TO Length of string

IF MID(String,i,1) = Character1 THEN

OUTPUT "Found"

END IF

NEXT

6 of 9

## Iteration Examples (Contd.)

Iteration Examples (Contd.)

Recursion

The Algorithm below is used to calculate the factorial of a number, n, that is inputted. When the function is first called n is given to the first iteration.

If n was initially 3 then it will be tested for 0 or 1 (FALSE) so line 05 will be executed and the first iteration of the function "paused", n in this function will be equal to n-1 from the last function (2). As 2 is still not 0 or 1 line 05 will be executed again and factorial will be called again. In this iteration of the function n = 1 so the function will return 1. Now the Recursion will go back to its previous iterations and evaluate line 05 in iteration 2 which will be 2 x 1 (the returned value) this will then be returned to the first iteration so that line 05 will equal 3 x 2 which is 6.

It is important to think of each function called as being completely seperate as each parameter and/or variables are different and can contain different values completely.

7 of 9

Iteration Examples (Contd.)

Recursion

01 Start Factorial (n as integer) as integer

02 If N= 0 OR 1 THEN

03 RETURN 1

04 ELSE

05 RETURN = n*Factorial(n - 1)

06 END FUNCTION Factorial.

8 of 9

Iteration Examples (Contd.)

WHILE

INPUT Num1 and Num 2

Num3 = Num1

WHILE Num3<=0

Num3 = Num3 - Num2

WHILE END

9 of 9