2.3.1 - 2.3.6 Programming Basics, Selection, Built-In Functions & Structured Programming 2
2.3.1 - 2.3.6 Programming Basics, Selection, Built-In Functions & Structured Programming 2
can be easier to understand by other programmers (who are either part of the programming team, or who will be
is split into smaller blocks which are easier to debug individually and update;
is less prone to errors – because the meaningful variable names and comments make it self documenting and
easier to read.
Programming
Variable
A variable is a value that can change during the execution of a program.
Constant
A constant is a value that is set when the program initializes and does not change during the program’s execution.
Identifier
Identifier is the name given to a variable, constant or subroutine.
Assigning a variable or constant an identifier name means that it can be easily referenced from any part of the
program within its scope and it helps to make the program more readable.
Reserved word/keyword
Reserved words (occasionally called keywords) are one type of grammatical construct in programming languages.
These words have special meaning within the language and are predefined in the language’s formal specifications.
Programming
Declaration
A declaration is a statement in a program that gives the compiler or the interpreter information about a variable or
constant that is to be used within a program.
A declaration ensures that sufficient memory is reserved in which to store the values and also states the variables’
data type. Reserved words/keywords cannot be used as identifier names as this generates a compiler error and
comes under the category of syntax error.
Powers
Programming
Division
A result of a division such as 17 ÷ 4 can be expressed either as a real (4.25) or as two integers (4 remainder 1).
The integer method, in most programming languages, uses the operators DIV and MOD.
Programming
Relational operators (=, <, <=, >, >= and <>)
Relational operators are used in the format: [Expression] [Operator] [Expression] and will always return a Boolean
(True or False) value.
Relational operators are typically used with the “IF” selection and also within conditional loops (REPEAT-UNTIL or
WHILE-WEND).
In the following table, the variables “a” and “name$” have the following assignments:
a=3+5
name$=“JAMES”
Programming
In the following table, the variables “a” and “name$” have the following assignments:
a=3+5
name$=“JAMES”
Programming
Boolean operators AND, OR and NOT
AND and OR
The AND & OR operators always return a Boolean result and are used in the format:
[Boolean] [Operator] [Boolean]
The following ‘truth’ table summarizes the result of the Boolean operations:
Values Results
Programming
NOT
The NOT operator reverses the result of the Boolean expression and is used in the format:
NOT [Boolean]
The following truth table summarizes the NOT operation:
Programming
Examples of Boolean ‘logic’
Consider the following algorithm, which is used to monitor a printer and display its output via a LCD display in the front panel:
IF NOT(PaperTrayEmpty) AND (FilesWaiting> 0) THEN
OUTPUT “PRINTING…”
ELSE
OUTPUT “PLEASE ADD PAPER”
END IF
If the values of the variables are:
PaperTrayEmpty = False
FilesWaiting = 2
Then the output will be “PRINTING…”
The following table shows why:
Programming
in whichPaperTrayEmpty = False.
To avoid this incorrect message, the algorithm should be rewritten using a nested IF, as shown on the next page:
IF PaperTrayEmpty THEN
OUTPUT “PLEASE ADD PAPER”
ELSE
IF FilesWaiting> 0 THEN
OUTPUT “PRINTING…”
ELSE
OUTPUT “STATUS OK”
END IF
Programming
Meaningful identifier names
Identifiers are used to give names to constants and variables. They are also used to name procedures, functions, and
the main program.
Naming conventions
Most of the identifier names must conform to the following rules (different programming languages may have
slightly different rules):
1. they must be unique;
2. spaces must not be used;
3. they must begin with a letter of the alphabet;
4. the rest of the identifier must not contain punctuation – it may only consist of a mixture of letters and
5. digits (A–Z, a–z and 0–9) and the underscore character ‘_’;
6. they must not be a ‘reserved’ word – eg Print, Repeat, For, Dim, Loop, etc.
Programming
Recommended naming policies
Do not use spaces within identifier names – even with programming languages where they are permitted. Instead,
use the underscore character ‘_’ or, better yet, type names in lowercase except the first letter of each word, which
should be typed in uppercase.
Examples of good identifier names:
FirstName LastName PostCode TelephoneNumber WeightAtBirth TestScore AverageHeight
Further clarity can be given to identifier names by including a prefix that identifies the data type. The above
identifiers would be clearer if given the following prefix data types:
strFirstName strLastName strPostCode strTelephoneNumber sglWeightAtBirth
intTestScore sglAverageHeight
Initializing a variable means setting it to a starter value. Initialization will usually set the value of an integer to 0 or 1
and a string to Empty (“”).
Programming
The following code initializes the variable Counter to zero before it is used in the iteration:
Counter=0 (the variable is initialized)
REPEAT
Counter=Counter+1
…
…
UNTIL Counter=10
Initializing a variable ensures that its value has not be been retained from a previous use of the routine and that the
value has not been accidently set in another part of the program – this helps avoid errors.
Programming
Comments/remarks
Comments (originally called remarks) are added to program code to provide useful, or essential, documentation for
the programmer and other people who read the code. All programs/subroutines should contain comments to indicate:
the details of the person who wrote the code;
the date that the code was written;
the purpose of the code;
how parts of the code work;
the use of certain variables.
Comments can be used anywhere in a program – either as separate lines by themselves or on the same line as
executable code provided they appear to the right of any code that will execute.
Comments are usually indicated by the use of an apostrophe (‘), or two forward slashes (//).
Programming
Indentation should be used within iteration and selection statements so that it is clear which instructions go together.
Original code Indented code
Conversion
Strings and numbers
Strings are a sequence of ASCII characters, even if they contain only numbers such as “241”, and so they cannot be
used within an arithmetic calculation – they need to be ‘evaluated’ first.
Likewise a number such as 27.3 is not stored as ASCII characters and so cannot be used within any of the string
functions (such as Left, Right, Mid).
Built-In Functions
The function STR converts a number into a string and VAL converts a string into a number:
Because it is the ASCII codes that are compared the following applies:
In order to get them sorted into the required order, they must be renamed with consistent use of uppercase letters,
spaces and leading zeros.