0% found this document useful (0 votes)
3 views16 pages

Common Procedures in Programming

Chapter 2.4 covers common facilities of procedural languages, including assignment statements, arithmetic and relational operators, and Boolean logic. It discusses type mismatch errors, string manipulation functions, and data validation techniques for user input. Additionally, it explains output formatting and the importance of operator precedence in expressions.

Uploaded by

flamepamsrap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Common Procedures in Programming

Chapter 2.4 covers common facilities of procedural languages, including assignment statements, arithmetic and relational operators, and Boolean logic. It discusses type mismatch errors, string manipulation functions, and data validation techniques for user input. Additionally, it explains output formatting and the importance of operator precedence in expressions.

Uploaded by

flamepamsrap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

https://sites.google.

com/site/computing9691/

Chapter 2.4: Common facilities of procedural languages

2.4 (a) Understand and use assignment statements.

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”

Type Mismatch errors


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:

DIM MyCounter AS Integer


MyCounter = “Charlie”
Other Type Mismatches will be produced by the following:
DIM RentalDate As Date
MemberRentalDate = “September”
DIM ShoeSize As Integer
JohnsShoeSize = 10.3

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/

2.4 (b) Arithmetic operators including operators for integer.

Addition, subtraction and multiplication

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/

2.4 (d) Boolean operators AND, OR and NOT

AND and OR
The AND and OR operators always return a Boolean result and are used in the format:

[Boolean] [Operator] [Boolean]

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]

The following truth table summarises the NOT operation:

Examples of Boolean ‘logic’


Consider the following algorithm, which is used to monitor a printer and display its output via an
LCD display in the front panel:

IF NOT(PaperTrayEmpty) AND (FilesWaiting > 0) THEN


OUTPUT “PRINTING…”
ELSE
OUTPUT “PLEASE ADD PAPER”
END IF

https://sites.google.com/site/computing9691/
Page 4 of 16
https://sites.google.com/site/computing9691/

If the values of the variables are:

PaperTrayEmpty = False
FilesWaiting = 2

Then the output will be “PRINTING…”

The following table shows why:

in which PaperTrayEmpty = 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
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.

Please follow BODMAS.


Also practice past paper questions.

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.

The following string manipulation functions are called by typing:

Result = FunctionName(String [,x,y])

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:

“Peter” + “Jones” = “PeterJones”


“Peter” + “ “ + “Jones” = “Peter Jones”
“36.85” + “47” = “36.8547”
“47” + “36.85” =“4736.85”
“3/10/03” + “15” = “3/10/0315”

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/

Characters and ASCII codes


The following two functions are used to either find the ASCII code for a specific character or to
find the character from the ASCII code:

https://sites.google.com/site/computing9691/
Page 10 of 16
https://sites.google.com/site/computing9691/

2.4 (h) Relational operations on alphanumeric strings.

Storing strings
Strings are stored as a sequence of character codes - usually ASCII or Unicode (refer 1.3)

ASCII character codes


Below is a table showing the most common characters and their ASCII codes:

Note the following:


• the codes less than 32 are ‘control’ codes that are not used in strings;
• ‘space’ (code 32) has the lowest code;
• next comes most of the punctuation symbols;
• then digits 0–9
• then uppercase letters
• then lowercase letters
• all other characters (e.g. é, å, π, etc.) have codes higher than these.

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:

“Computing” <> “computing”


in fact:
“Computing” < “computing”
“10” < “2”
“02” < “1”
“1 120” < “1,120”
“ computing” < “Computing”

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.

Recommended naming would be:


Track 01.mp3, Track 02.mp3, …, Track 10.mp3, Track 11.mp, …

https://sites.google.com/site/computing9691/
Page 12 of 16
https://sites.google.com/site/computing9691/

2.4 (i) Input and validate data.


Input controls
Input can be via a command line prompt, but in modern graphical user interfaces, input is usually
via a form or dialogue box. Such forms or dialogue boxes have a set of ‘controls’, which let the
user enter data:

• 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/

2.4 (j) Validation


The drop-down lists, check-boxes and option buttons are extremely useful in that they provide an
automatic means of validation – the user can only input pre-determined values.

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:

If val(Age.Text) < 11 OR val(Age.Text) > 70 Then


Msgbox(“The age that you have netered is not within the_
permitted range”)
End

Length check
Code can be added to check that a particular control has a value between an allowed maximum
and minimum:

If Len(Password.Text) > 12 Then


Msgbox(“The password that you have entered is too long”)
End

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:

Private Sub cmdTryIt_Click()

Dim strName As String

strName = InputBox("Please enter your name:")

Print "Using '>':"; Tab(25); Format$(strName, ">")


Print "Using '<':"; Tab(25); Format$(strName, "<")
Print "Using '@':"; Tab(25); "Hello there, "; Format$(strName, "@@@@@@@@@@"); ". How are you?"
Print "Using '&':"; Tab(25); "Hello there, "; Format$(strName, "&&&&&&&&&&"); ". How are you?"
Print "Using '!@':"; Tab(25); "Hello there, "; Format$(strName, "!@@@@@@@@@@"); ". How are you?"
Print "Using '!&':"; Tab(25); "Hello there, "; Format$(strName, "!&&&&&&&&&&"); ". How are you?"
Print "Using '\':"; Tab(25); Format$(strName, "\H\e\l\l\o\,\ &&&&&&&&&&\.")
Print "Using embedded quotes:"; Tab(25); Format$(strName, """Hello, ""&&&&&&&&&&"".""")

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

You might also like