Common Procedures in Programming
Common Procedures in Programming
com/site/computing9691/
Assignment
An assignment is an instruction in a program that places a value into a specified variable.
Some typical assignments are:
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:
“The new value of Counter is its existing value plus one”
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.
https://sites.google.com/site/computing9691/
Page 1 of 16
https://sites.google.com/site/computing9691/
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.
https://sites.google.com/site/computing9691/
Page 2 of 16
https://sites.google.com/site/computing9691/
2.4 (c) Relational operators, eg. =, <, <=, >, >= 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”
https://sites.google.com/site/computing9691/
Page 3 of 16
https://sites.google.com/site/computing9691/
AND and OR
The AND and OR operators always return a Boolean result and are used in the format:
The following ‘truth’ table summarises 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]
https://sites.google.com/site/computing9691/
Page 4 of 16
https://sites.google.com/site/computing9691/
PaperTrayEmpty = False
FilesWaiting = 2
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
END IF
https://sites.google.com/site/computing9691/
Page 5 of 16
https://sites.google.com/site/computing9691/
2.4 (e) Effects of the precedence of standard operators and the use of parentheses
to alter the order of precedence.
https://sites.google.com/site/computing9691/
Page 6 of 16
https://sites.google.com/site/computing9691/
2.4 (f) Expressions containing arithmetic, relational and Boolean operators and
parentheses.
Class based practices and with special consideration of chapter 1.10.
https://sites.google.com/site/computing9691/
Page 7 of 16
https://sites.google.com/site/computing9691/
2.4 (g) Functions for string manipulation and converting between characters and
their ASCII code.
Location
Many programming languages allow the searching for a short string of characters within a longer
string. This would be similar to searching this book for the word ‘computer’.
Common Key words for finding the location of one string within another are LOCATE, FIND and
POSITION
Extraction
At times, a programmer will only want part of a string. Any part of a string can be obtained using
the RIGHT, LEFT and MID functions.
https://sites.google.com/site/computing9691/
Page 8 of 16
https://sites.google.com/site/computing9691/
Concatenation
Concatenation is where two or more strings are joined together to make a single string.
Note that when two strings are added together the second string is added to the end of the first:
Length
Sometimes a programmer will need to know how many characters there are in a string.
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).
The function STR converts a number into a string and VAL converts a string into a number:
https://sites.google.com/site/computing9691/
Page 9 of 16
https://sites.google.com/site/computing9691/
https://sites.google.com/site/computing9691/
Page 10 of 16
https://sites.google.com/site/computing9691/
Storing strings
Strings are stored as a sequence of character codes - usually ASCII or Unicode (refer 1.3)
https://sites.google.com/site/computing9691/
Page 11 of 16
https://sites.google.com/site/computing9691/
Comparing strings
When comparing strings, the codes of each string are compared, character by character, to decide
which string is greater.
Because it is the ASCII codes that are compared the following applies:
Sorting filenames
The following table shows a list of inconsistently names music tracks and the order in which they
would be sorted:
In order to get them sorted into the required order, they must be renamed with consistent use of
uppercase letters, spaces and leading zeros.
https://sites.google.com/site/computing9691/
Page 12 of 16
https://sites.google.com/site/computing9691/
• Text boxes;
• Drop-down lists;
• Check-boxes;
• Option buttons.
https://sites.google.com/site/computing9691/
Page 13 of 16
https://sites.google.com/site/computing9691/
Other validation needs to be programmed into the system and is often added to the submit
button.
Presence check
Code can be added to check that a particular control has not been left empty or un-checked:
If Username.Text = “” Then
Msgbox(“You have not entered a Username”)
End
Range check
Code can be added to check that a particular control has a value between an allowed maximum
and minimum:
Length check
Code can be added to check that a particular control has a value between an allowed maximum
and minimum:
Note that all of the above validation code (and any additional validation) could be added to the
submit button.
https://sites.google.com/site/computing9691/
Page 14 of 16
https://sites.google.com/site/computing9691/
2.4 (k) Output data onto screen/file/printer, formatting the data for output as
necessary.
Output will be either to the screen, a file or the printer.
Output controls
Output to the screen could be via a dialogue/message box, a text box, a list box or simply direct
on the form.
Custom string formatting can be accomplished using specific characters recognized by the
Format$ function, shown in the table below:
To demonstrate custom string formats using combinations of the characters listed above, set up
another "Try It" project, and place the following code in the cmdTryIt_Click event of cmdTryIt
button:
End Sub
https://sites.google.com/site/computing9691/
Page 15 of 16
https://sites.google.com/site/computing9691/
Run the project and click the "Try It" button. When the input box appears, enter a name in mixed
case (in this example, Bruce was entered). The strings will be displayed as follows:
https://sites.google.com/site/computing9691/
Page 16 of 16