Title: VB Constructs
1VB Constructs
2More on Comments
- Should you comment every declaration and every
line of code? - NO!!
- Comments should ADD information
- Always include the Header comment (unit1)
- Comment declarations ONLY if meaningful name is
insufficient to explain purpose - Comment a line or block of code ONLY if purpose
is not obvious
3More on Data Types
- The Integer Data Type
- Dim NumberOfCars As Integer
- Used to store WHOLE numbers
- Use for whole numbers that will be used in
calculations - If a car dealer has 36 cars in stock and a
delivery of a further 10 then - NumberOfCars NumberOfCars NewCars
4Given
Variables NumberOfCars NewCars accessed
current values stored brought forward
NumberOfCars NumberOfCars NewCars
Calculation performed giving result of 46. This
result is then saved back into variable
NumberOfCars thus overwriting the old value,36,
that had been stored there
After assignment
5More on Arithmetic Operators - Comparison
- Values can be compared
- These comparisons can be used by program at run
time to make decisions about what code to execute
next - In computing a comparison is called a condition
- Conditions are built using relational operators
6Relational Operators
- Symbol Relation Tested
- gt greater than
- lt less than
- equal to
- (note same symbol as for assignment!)
- ltgt not equal to
- gt greater than or equal to
- lt less than or equal to
7Using Conditions - If, Then, Else, EndIf
Statement
8If, Then, Else, EndIf Statement
- The condition following If is tested
- If condition is TRUE all statements between Then
and Else are executed - Processing then goes to statement AFTER Endif
- If condition is FALSE all statements between Else
and EndIf are executed - Processing then goes to statement AFTER Endif
9Commenting If statements
- If age gt cDriveAge Then
- user can drive
- MsgBox You are old enough, vbOKonly
- Else
- user cant yet drive
- calculate how many years until driving age
- yearsUntilDriving cDriveAge - age
- MsgBox yearsUntilDriving _
- years until able to drive, vbOkonly
- End If
10Commenting If statements
- In example, condition tests to see if someone is
17 or more years of age. - If condition is true, comment that user is old
enough to drive - If condition is false, comment that user is not
yet old enough. Additional comment highlights
that program will calculate how long it will be
before user can drive
11The Problem
- To write a program to calculate the factorial of
a number between 2 and 12. - Questions to answer
- How is a factorial calculated in maths?
- How can we ensure that the user only inputs a
number between 2 and 12? - We will start with the second question which
refers to concept of Data Validation
12Data Validation
- In our problem, we need to ensure that user
inputs a number between 2 and 12 - Programmers should NEVER assume that a user will
always get it right first time! - If we need to ask for input, check whether it is
valid and re-ask until it is, this implies that
we may need to do the same thing several times -
iteration - Computers are very good at iteration!
13A Validation Loop
- Consider the following piece of code
- number InputBox (Please enter number -
- in range 2-12)
- Do While condition
- number InputBox (Please re-enter -
- must be in range 2-12)
- Loop
- User is prompted to enter number, then
re-prompted within a loop construct until some
condition is met
14The Do While Loop
15Do While Loop
- Upon entering the loop, the condition is
evaluated - If FALSE, statements within loop are ignored and
processing moves to the statement after Loop - If TRUE, statements within loop are executed and
then the condition is re-evaluated. This
continues until the condition evaluates FALSE
16Do While Loop
- Allows open-ended iteration
- However, beware - at some point the condition
MUST evaluate FALSE otherwise program NEVER
leaves the loop - So - it is usual that one or more statements
within the loop will affect the condition - Always establish a value within variable to be
tested BEFORE the Do While statement
17Do While Loop Condition
- Looked at conditions use of relational
operators in unit 4 - Problem requires a number between 2 12
- So if number is less than 2 or greater than 12 it
is not acceptable - (number lt 2) Or (number gt 12)
- This is called a Compound Condition where we join
two simple conditions with a boolean logical
operator
18Boolean Logical Operators
- Anything of type boolean can be only either TRUE
or FALSE
Operator Meaning Or if one condition or both
conditions are true, entire condition is true
And Both conditions must be true for entire
condition to be true Not Reverses truth of
condition
19The Finished Validation Loop
number InputBox (Please enter number -
in range 2-12) Do While (number lt 2) Or (number
gt 12) number InputBox (Please re-enter
- must be in range 2-12) Loop
20The Second Question - How is a factorial
calculated in maths?
- All to do with developing an algorithm
- An Algorithm is a set of rules that define or
perform or solve a specific problem. (Collin S,
1989, Hamlyn Dictionary of Computing) - Factorial - product of positive integers from 1
to n - 2! 1 x 2 2
- 4! 1 x 2 x 3 x 4 24
- 8! 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 40320
21Large Integers
- In unit 4, looked at Integer Data Type
- In Visual Basic the Integer data type can store
any whole number between - -32,768 and 32,767
- 8! 40320 - too large for Integer data type
- Long data type - can store any whole number from
- -2,147,483,648 to 2,147,483,647
22Calculating the factorial
- Uses iterative multiplication
- This indicates that we need to use a loop
construct again. But - this time we know how many times we will go round
the loop - - for 2! - 1 multiplications
- for 4! - 3 multiplications
- for 8! - 7 multiplications
- Generalises to (number-1) multiplications
23The For Next Loop
For loopIndex startNumber To finishNumber Step
stepSize statements forming body of loop Next
loopIndex
24Back to calculating the factorial
- Given variables
- number receives number of factorial to be
calculated - fact holds the factorial during and after
calculation - count the loop index for the For Next loop
- Set fact 1 and then start the loop at 2 with
the finish point being number
25The Factorial Calculation
fact 1 For count 2 to number Step 1 fact
fact count Next count
Note that Step 1 is not essential. The default
is to add 1 to the loopIndex
26Calculating 3!
fact count number number InputBox()
3 fact 1 1 For count 2 to number Step
1 2 count lt number so iteration starts
fact fact count 2 Next count
3 count number so iteration continues
fact fact count 6 Next count
4 count gt number so iteration stops
27Dim number As Integer Dim count As Integer Dim
fact As Long number InputBox (Please enter
number in range 2-12) Do While (number lt 2) Or
(number gt 12) number InputBox (Please
re-enter - must be in range
2-12) Loop fact 1 For count 2 to number
Step 1 fact fact count Next
count MsgBox number factorial is fact