The function of structural components of programs - EdexcelNesting

Programs are designed and implemented using common building blocks, known as programming constructs. These constructs are sequence, selection and iteration and they form the basis for all programs.

Part ofComputer ScienceApplication of computational thinking

Nesting

is when one programming construct is included within another. Nesting enables powerful yet simple programming. It reduces the amount of code needed, while making it simple for a programmer to and edit.

Nested selection

When using , the number of possible paths at a decision point can be increased by including one selection within another.

This program uses nested selection:

SEND ‘How old are you?‘ TO DISPLAY RECEIVE age FROM (INTEGER) KEYBOARD IF age > 16 THEN SEND ‘You are old enough to drive a car and ride a moped’ TO DISPLAY ELSE  IF age = 16 THEN SEND ‘You are old enough to ride a moped!’ TO DISPLAY ELSE SEND ‘Come back when you are older!’ TO DISPLAY END IF END IF

Here, there are two conditions that may be tested, resulting in three possible paths to follow. The second condition is only tested if the result of the first test is FALSE.

Nested selection can be built up over and over to include many possibilities and paths for a program to follow. It enables complex decisions to be made.

Nested iteration

can also be nested. This program uses two count-controlled - one within another - to print out the times table for all numbers from one to ten:

FOR x FROM 1 TO 10 DO  FOR y FROM 1 TO 10 DO SET result TO y * x SEND y & " * " & x & " = " & result TO DISPLAY END FOR END FOR

For every iteration of x, y is iterated ten times.

This program uses condition-controlled WHILE loops, one within another, to print out the times table for all numbers from one to ten:

SET x TO 1 SET y TO 1 WHILE x < 11 DO  WHILE y < 11 DO SET result TO y * x SEND y & " * " & x & " = " & result TO DISPLAY y = y + 1 END WHILE SET y TO 1 SET x TO x + 1 END WHILE

For every iteration of x, y is iterated ten times.