F452 - 3.2.2 The Structure of Procedural Programs

?

3.2.2 Structure of Procedural Programs

These are short definitions of key termsfor structures of procedural programs.

(http://s23.postimg.org/if8ynkgor/Screen_Shot_2015_04_16_at_08_19_28.png)

1 of 7

3.2.2 Structure of Procedural Programs (2)

  • The definitions highlighted in red in the previous card are the three basic programming constructs.
  • These allow us to control how the program will execute the statements in it.
  • ITERATION is useful because otherwise we would have to write the instructions out the correct number of times.
  • There are two types of control loop, condition-controlled and count-controlled.
    • condition-controlled loop keeps going until a certain condition is met.
    • counter-controlled loop repeats until it has run a certain number of times.
  • There are different kinds within each control loop
  • Within the condition-controlled loop there is a WHILE and a REPEAT UNTIL loop...

(http://s9.postimg.org/fmkeapozz/Screen_Shot_2015_04_16_at_08_24_36.png)

  • In both cases, it repeats if the condition is TRUE and exits if the condition is FALSE.
2 of 7

3.2.2 Structure of Procedural Programs (3)

By changing the conditions and/or some of the data in the program, it is possible to convert a WHILE loop into a REPEAT UNTIL loop that performs the same function.

Infinite loops can be caused by an error in the logic of an algorithm.
They repeat endlessly when a condition in a WHILE loop can never be false.
A similiar situation will occur in a REPEAT UNTIL condition if it can never be true.

Counter-controlled loops are often referred to as FOR loops.
For loops use a variable to determine whether the instructions should be repeated.
Typically used when the number of iterations is known before entering the loop.

Nesting constructs are where there are loops within loops.
They give programmers  the flexibility to write complex and powerful algorithms.
Care must be taken as beginnings/ends cannot overlap each other.
Indenting instructions in constructs is a good way of ensuring this.

subroutine is a portion of code within a larger program.
Also called a procedure, method, function, or routine, it performs a specific task.
When a subroutine is defined, it is given an identifier (name) and some instructions.
Two types of subroutines are procedures and functions...

3 of 7

3.2.2 Structure of Procedural Programs (4)

(http://s1.postimg.org/f5kn97zbz/Screen_Shot_2015_04_16_at_08_34_37.png)

The parameters of a procedure are a special kind of variable, used to refer one of the pieces of data provided as input to the subroutine.
These pieces of data are called arguments

An ordered list of parameters is usually included in the definition of a subroutine.

Each time the subroutine is called, its arguments for that call can be assigned to the corresponding parameters.

Instructions are then executed using the actual values substituting parameters.

This is referred to as 'passing parameters' to the procedure.

4 of 7

3.2.2 Structure of Procedural Programs (5)

Recursion is when a subroutine calls itself.

The subroutine is executed as normal until it gets to the line where it calls itself.
If a new subroutine is started every time the subroutine is called, the process would go on indefinitely.

Therefore a recursive subroutine usually has parameters.

There is also a case where based on the value of the parameters, the subroutine can be executed and exit without needing to call itself.

This is known as the stopping condition.
- Once this has been reached, the subroutine is executed completely and then control is passed back to othe previous call that can now be executed.

Tracing the execution of a recursive call means following the execution step by step.
As you follow it, you note...
- Which lines are executed
- What happens when each line is executed
- Where recursive calls are made

5 of 7

3.2.2 Structure of Procedural Programs (6)

This is best illustrated as an algorithmic diagram...

(http://s10.postimg.org/7no2oiyhl/Screen_Shot_2015_04_16_at_08_49_47.png)

6 of 7

3.2.2 Structure of Procedural Programs (7)

Comparing iterative and recursive solutions to a problem...

(http://s17.postimg.org/415zrhz7j/Screen_Shot_2015_04_16_at_08_52_46.png)

7 of 7

Comments

No comments have yet been made

Similar Computing resources:

See all Computing resources »See all F452 resources »