Chapter 11 | Programming

11.1 Programming Basics

Input Processing & Output

Variables & Constants

Variables

A variable is a storage location with an identifier. The value of the variable can change.

DECLARE <identifier> : <data type>

Variables can be expressed in identifier tables, which include:

Constants:

A constant is a named storage location. The value of the constant can not change.

CONSTANT <identifier> = <value>

11.2 Constructs

Logic statements

IF THEN ELSE ENDIF

Provides a selection of 2 possible pathways

IF <condition> THEN

<statement(s)>

ELSE

<statement(s)>

ENDIF

A condition consists of at least one logic proposition. Logic propositions use comparison & relational operators.

Operator Comparison
= Equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
<> Unequal to
CASE Statement

Selection of multiple pathways

Each condition can be:

CASE OF <expression>

<value1>: <statement(s)>

<value2>, <value3>: <statements(s)>

<value4> to <value5>: <statement(s)>

OTHERWISE <statement(s)> //optional; used for error trapping

ENDCASE

Loops

Count-controlled (FOR) loop

Iterates a set number of times

FOR <control variable> ← s TO e STEP i // STEP is optional

<statement(s)>

ENDFOR

Post-conditional (REPEAT...UNTIL) loop

A post-conditional loop must run at least once. The condition is evaluated at the end.Aas long as the condition evaluates to false, the loop runs again. The loop will terminate once the condition evaluates to true.

REPEAT

<statement(s)>

UNTIL <condition>

Pre-conditional (WHILE) loop

A pre-conditional loop doesn’t always run. This evaluates the condition at the start. The loop will execute the statement as long as the condition evaluates to true, and will terminate once the condition evaluates to false.

WHILE <condition>

<statement(s)>

ENDWHILE

11.3 Structured Programming

Modular programming

Modular programming: The process of subdividing a computer program into separate sub-programs.

Modules: Seperate software component

Benefits:

A subroutine is a set of instructions that performs a specific task as part of a larger program. Defined away from the program, and called with an identifier when needed. 2 types; procedures & functions.

Procedures

A subroutine that doesn’t return a value

PROCEDURE <identifier> (<parameter1>: <dataType> …)

<statement(s)>

ENDPROCEDURE

CALL <identifier>

May be given data - using variables called parameters passed in as values called arguments

Functions

A subroutine that returns a value

FUNCTION <identifier> (<parameter1>: <dataType> …) RETURNS dataType

<statement(s)>

ENDFUNCTION

call ← identifier()

Example functions:

Passing parameters