National 5 - Computational Constructs

Computational Constructs

Throughout this page the examples will be written in Visual Basic 2012 code and assuming output to a label called lblDisplay when appropriate unless stated otherwise.

Expressions to assign values to variables.

Assigning a value to a variable is when a value e.g. 5 is stored into a declared variable e.g. firstnumber. In the program below a fixed value has been assigned but in most programs that you would do you would get that value from a user either from a text box or inputbox.

"assigning" a value is a key concept that exists across most programming languages. In Visual Basic the = sign is used to assign values to a variable. However in other programming languages sometimes two equals firstnumber == 5 signs are used or a colon and an equals sign e.g. firstnumber:= 5. This varies across languages.

Assign values to variables

					
'Declare the variables
Dim firstnumber as integer Dim secondnumber as integer Dim answer as integer firstnumber = 5
'This line assigns the value 5 to the integer variable firstnumber
secondnumber = 10
'This line assigns the value 10 to the integer variable secondnumber
answer = firstnumber + secondnumber
'This line adds the value stored in firstnumber [5]
to the value stored in secondnumber [10] and assigns
the answer [15] into the variable answer.
lblDisplay.text = answer
'This line outputs the answer to the text property of a
label called lblDisplay

Expressions to return values using arithmetic operations (+, -, *, /, ^, mod)

Most programming languages also allow use of the arithmetic operators to perform basic calculations. The rules of arithmetic operations apply in your programs (BODMAS).

Arithmetic Operations

					
'Continuing the program above answers could be as follows:
firstnumber = 5 secondnumber = 10
'Using the values for firstnumber and secondnumber the answers would be:
answer = firstnumber + secondnumber
'Addition 5+10, answer= 15
answer = firstnumber - secondnumber
'Subtract 5-10, answer= -5
answer = firstnumber * secondnumber
'Multiply 5x10, answer= 50 (the * sign means multiply)
answer = firstnumber / secondnumber
'Divide 5/10, answer= 0.5 (The forward slash means divide)
-----
'If we change the numbers for a few more examples:
firstnumber = 2 secondnumber = 3
'Using the values for firstnumber and secondnumber the answers would be:
answer = firstnumber ^ secondnumber
'Exponential Values 2^3, answer= 2x2x2 = 8
-----
'And again
firstnumber = 20 secondnumber = 3
' The Modulus is the whole number remainder of an integer division.
answer = firstnumber Mod secondnumber
'Modulus 20 MOD 3, answer= 20 divided by 3,
'equals 6 remainder 1 so the modulus is 1.

Expressions to concatenate strings and arrays using the and operator

Concatenation means to join two strings together into a single item (usually for output). This program asks the user for a pupilname and then says Hello plus the name e.g. Hello Colin.

Concatenate Strings

					

'Declare a variable to store an assigned pupil's name when prompted
Dim pupilname as string
'Get the pupil's name using an inputbox and store it
pupilname = inputbox("Please enter the pupil's name","Enter Pupil Name")
'The stored name is added onto hello to make this simple greeting.
lblDisplay.text = "Hello" & pupilname

Use of selection constructs including simple and complex conditional statements using logical operators (AND, OR, NOT)

Selection constructs are used to help programs make decisions. The most common selection construct is the IF statement. IF statements use simple conditions or complex conditions to make these choices. A simple condition includes only one conditional statement where a complex condition contains more than one. Examples of each are shown below.

Variable Declarations

					
'This is a sample comment.
x = 5; y = 6;
'This is another sample comment.
z = x + y;

Iteration and repetition using fixed and conditional loops

This is a big topic so we will break it down into different code boxes to simplify the explainations.

Iteration and Repetition using fixed and conditional loops

					
'This is a sample comment.
x = 5; y = 6;
'This is another sample comment.
z = x + y;

Pre-defined functions (with parameters) including Random, Integer and Round

Variable Declarations

					
'This is a sample comment.
x = 5; y = 6;
'This is another sample comment.
z = x + y;