2-3-1 Programming Basics
2-3-1 Programming Basics
Programmers should write code that is self-documenting and split into small sections.
It is important for a programmer to use good programming techniques when writing code so that the
code:
can be easier to understand by other programmers (who are either part of the programming team,
or who will be responsible for the maintenance of the program in the future);
can be understandable by the programmer himself in the future;
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.
Page 1 of 13
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
Constant
A constant is a value that is set when the program initializes and does not change during the program’s
execution.
Identifier
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. Typically, reserved words include labels for primitive data types in languages that
support a type system, and identify programming constructs such as loops, blocks, conditionals, and
branches.
Page 2 of 13
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
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
U
variables’ data type. Reserved words/keywords cannot be used as identifier names as this generates a
U
Some programming languages require intrinsic variable declarations. This means that variables must be
declared before they are used.
The advantage of intrinsic variable declaration is that it cuts out possible errors due to the misspelling of
a variable name – if a variable is used that has not been declared, the programming translator will identify
it as an error.
There are two types of variables used by programmers and they are categorized according to their scope.
Page 3 of 13
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
Scope indicates whether a variable can be used by all parts of a program or only within limited sections of
the program – for example, within a subroutine.
Global variable
A global variable is one that is declared at the start of the main program and is visible (useable)
everywhere in the program and exists for the lifetime of the program.
Note that if one procedure changes the value of a global variable, then the next procedure that uses the
variable will be given this changed value – this is a common cause of errors.
Local variable
A local variable is one that is only visible inside the procedure or function in which it is declared.
Note that a local variable cannot be referenced from outside the procedure. In fact a local variable does
not exist until the procedure starts executing and it disappears when the procedure stops executing. Thus
any value that is held by a local variable is only stored temporarily.
The lifetime of a local variable is the lifetime of the procedure in which the local variable is declared.
The advantage of using local variables rather than global variables is that the same variable names can be
used in several different procedures without any chance of values coinciding.
Constants will be declared at the start of the main program. The following shows the declaration of
constants for Pi and the VAT rate
CONST Pi = 3.142
CONST VatRate = 0.175
Declaring constants at the start of a program means that maintenance is made easier for two reasons:
if the value of a constant changes, it only has to be changed in the one part of the program where it
has been declared, rather than in each part of the program in which it is used;
rather than
Total=0.175*CostPrice
Page 4 of 13
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
TheLength = 20.5
TheUsersName$ = “Charlie”
TheArea = TheWidth * TheLength
TotalCost = LabelledCost + 15
Counter = Counter + 1
Note that the last example is a common method used to increment the value of a variable. It could be read
as:
A type Mismatch error occurs in a program when a variable has been declared as one data type, but it is
later assigned a value that is of an incompatible data type.
The following code will produce a ‘Type Mismatch’ error because “Charlie” is not an integer:
Note that a variable that is declared as a string will never produce a type mismatch error.
Arithmetic Operator
Page 5 of 13
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
Powers
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.
Page 6 of 13
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
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”
Page 7 of 13
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
AND and OR
The AND & OR operators always return a Boolean result and are used in the format:
The following ‘truth’ table summarizes the result of the Boolean operations:
Values Results
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:
Page 8 of 13
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
Consider the following algorithm, which is used to monitor a printer and display its output via a LCD
display in the front panel:
PaperTrayEmpty = False
FilesWaiting = 2
in whichPaperTrayEmpty = False.
To avoid this incorrect message, the algorithm should be rewritten using a nested IF, as shown on the
next page:
Page 9 of 13
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
Page 10 of 13
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
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):
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.
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:
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 (“”).
Page 11 of 13
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
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.
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.
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 (//).
PROCEDURE Arrange(NumberOfScores)
‘Procedure to move the largest element in an array to the end
‘Coded by J Jones, 02/03/09
DIM Ptr, Temp As Integer ‘ptr is the value of the array index
Temp = Scores(Ptr)
Scores(Ptr) = Scores(Ptr + 1)
Scores(Ptr + 1) = Temp
END IF
NEXT Ptr‘increment the index to examine the next element
END PROCEDURE
Page 12 of 13
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
Note that comments are ignored when the program code is compiled and so they are not present within
the stand-alone application.
Indentation should be used within iteration and selection statements so that it is clear which instructions
go together.
Examples:
U
Original code
U Indented code
U
Original code
U Indented code
U
Page 13 of 13