If....Then statements - the driving license example
Back to Main Page
We use If....then statements to allow a user to choose betwen 2 or more options. You can see this in action at a highstreet bank's cashpoint machine:

If you select cash - then you are asked how much?
If you select statement - then a statement is printed
If you hit cancel - then your card is returned

The program will repond in a different way to different user's needs.

Before we begin coding loops, we need to look at the mathematical symbols we use for setting out our choices; VB uses the following mathematical symbols:


= means 'is equal to'                                   e.g.   2 + 3 = 4 + 1
< means 'is less than'                                 e.g.  7 < 9
<= means 'is less than or equal to'            
> means 'is greater than'                            e.g.  8 > 6
>= means 'is greater than or equal to'   


To demonstrate this, we will look at a simple program that decides whether a person should be issued with a provisional driving license. To be allowed this, the applicant must be 17 or over. In other words, their age must be greater than or equal to 17
Remember that we use the following format for if...then statements:

VB code                                              How it works

If Age >= 17 Then            - IF the age is17 or over THEN
Print out 'yes' message   - message says "collect your license"
Else                                    - if the condition is NOT met - age under 17
Print out 'no' message    - message says "you can't collect your license"
End If                                - ends the If..Then statement



Task 1

Type in the code below and check its functionality - enter the ages 16, 17 and 18 - is the result what you expect?
Task 2

In this exercise, you will write a program which checks a user's PIN number - just as a cashpoint machine would.
Let's imagine that the user's pin number is 1234

Write a program which:
- Asks the user to enter their PIN number
- Checks to see if the PIN is 1234
- If the PIN is correct, tells the user that they are allowed access
- If the PIN is incorrect, tells the user that they are denied access

End of tasks
Back to Main page
Hosted by www.Geocities.ws

1