Programming Structures

What is an Imperative Language?

Imperative programming languages require programs to define sequences of commands for the computer to perform... basically a list of instructions.

What is a Sequence in an Algorithm?

A sequence is a control structure where the computer executes each instruction in the order they are written until it reaches an instruction to end. Each instruction is on a separate line with the first instruction at the top. You have already been introduced to sequences in algorithms.

In the example below, the pseudo-code algorithm uses the sequential instructions GET and DISPLAY and executes each instruction in sequence, starting at the top:


GET item_price 
GET vat_rate vat_amount = item_price * vat_rate 
final_price = item_price + vat_amount 
DISPLAY final_price 
END


Conditions & Iteration

Conditions and iteration are both examples of Control Operations. These are instructions that DO NOT have to be executed in sequence...

Conditional Operations

These allow the program to execute instructions that are non-sequential.

IF - this is used with THEN, ELSE and ENDIF to check if a condition is TRUE or FALSE. Only if the condition is TRUE will certain instructions be executed. Optionally, if the condition is FALSE then a different set instructions will be executed.

CASE - this is used with CASE OF and ENDCASE to select one instruction from a set of instructions, depending on the value of a variable. It does the same job as having multiple IF/ ELSE statements, but is far more efficient. In the example below, the algorithm uses the conditional control operations IF / THEN / ELSE / ENDIF to determine whether or not overtime needs to be paid.

In the following example, the algorithm uses the conditional control operations CASE OF / CASE / ENDCASE to perform the same task:


GET hours_worked 
GET pay_rate
IF hours_worked ≤ 35 THEN
gross_pay = pay_rate * hours_worked ELSE
gross_pay = (pay_rate * 35) + (1.5 * pay_rate * (hours_worked - 35)) 
ENDIF
DISPLAY gross_pay 
END


Iterative Operations

These repeat a block of instructions until a condition is met. WHILE - this is used with ENDWHILE to repeat a block of instructions until a condition is TRUE. The check is carried out at the start of each loop. The instructions in the loop may never be executed if the condition is FALSE at the beginning.

REPEAT - this is used with UNTIL to repeat a block of instructions until a condition is FALSE. The check is carried out at the end of each loop. The instructions in the loop will always be carried out at least once. FOR - this uses a counter and is used with TO and NEXT to repeat a block of instructions a set number of times. The counter can go up or down in different size STEPS.

In the example below, the algorithm uses the iterative control operation WHILE / ENDWHILE to carry out a block of sequential instructions until a condition is met. The check is carried out at the start of each loop…


GET total_items 
SET total_cost = 0 
SET count = 0
WHILE count < total_items
GET item_price 
total_cost = total_cost + item_price 
count = count + 1 
ENDWHILE
average_price = total_cost / total_items 
DISPLAY average 
END


In the second example the algorithm uses the iterative control operation REPEAT / UNTIL to carry out a block of sequential instructions until a condition is met. The check is carried out at the end of each loop.


GET total_items 
SET total_cost = 0 
SET count = 0
REPEAT
GET item_price 
total_cost = total_cost + item_price 
count = count + 1 
UNTIL 
count = total_items
average_price = total_cost / total_items 
DISPLAY average 
END


In the last example the algorithm uses the iterative control operation FOR / TO / NEXT to carry out a block of sequential instructions a specific number of times. The default STEP value will increment the counter by 1 unless you specify something different.


GET total_items 
SET total_cost = 0
FOR count = 1 TO total_items
GET item_price total_cost = total_cost + item_price 
NEXT
average_price = total_cost / total_items 
DISPLAY average 
END