Computer Science
Computer Science
Selection arises when a condition exists in a problem and you need to take a decision or make a
choice.
In Visual Basic, a condition is coded by using a data name (or a variable) which compared with a
value or a text, depending on whether the variable is numeric or string.
Types of selection
Simple selection
Binary selection
Multiple selection
Relational operators
A relational operator is used to separate a condition and its value or text.
Operator Meaning
= Equal to
> Greater than
< Smaller than
>= Greater or equal to
<= Smaller or equal to
<> Not equal to
Simple Selection
Syntax IF condition THEN
Statement
Endif
Answer
Rem Declare Variables
End Sub
Binary Selection - Format 1
Syntax IF condition THEN
Statement(s)
Else
Statement(s)
ENDIF
Answer
Rem declare variables
End Sub
Dry run the program with the following data and write the expected result
Activity 1
Write a program to input product code, quantity and price. Calculate total cost. A
message is displayed whether to allow a discount or not on the total cost depending on
its amount as shown in the above table. Write a program to perform this task.
Syntax
OR AND
OR Syntax AND Syntax
IF (condition 1) OR (condition 2) THEN IF (condition 1) AND (condition 2) THEN
Statement(s) Statement(s)
ENDIF ENDIF
NB: You can have any number of conditions combined with OR and AND.
The following tables shows the result from separating 2 conditions using AND and OR
AND OR
Activity
The following table shows the amount of tax payable based on the disposable income.
Disposable Income Tax Rate
15000 – 30000 10%
30001 - 50000 20%
> 50000 30%
Write a program to input the disposable income. Calculate and print the tax payable/
NESTED IF...ENDIF
The nested IF... ENDIF structure provides an easy method to code a problem with multiple
conditions insted of using too many OR / AND operators
Syntax IF condition(s) THEN
Statement(s)
ELSEIF condition(s) THEN
Statement(s)
ELSEIF condition(s) THEN
Statement(s)
ElseIf...
ELSE
Statement(s
ENDIF
SELECT...CASE STRUCTURE
In situations where you have too many conditions concatenated by either using logical operators
(OR/AND) or nested IF structure which makes your program longer, then the SELECT...CASE is
another way to do the job.
Syntax Select CASE variable
Case conditions
Statement(s)
Case conditions
Statement(s)
Case Else (match not found for above conditions)
Statement(s)
End Select
Example Dim grade As String
End Sub
CHECK BOX
A Check Box provides a way to make choices from a list of items. You can select several, all, or none
of the items at one time.
Properties Effect
Caption Set the text to display beside the check box
Font Sets the font type
Note:
Frames are useful to regroup control buttons and make the screen more presentable
You must insert frame first and the insert controls inside it.
Create the following form
Frame 1
Text Boxes (1-4)
Label 1 Option Buttons (1-4)
Labels(2-5)
Frames (2-4)
Labels properties
Label 1
Name : lblTitle
Autosize: True
Boarder Style: Fixed Single
Caption: Members Information
Font: Ms Sans Serif Regular 18
Alignment: 2-Center
Frame properties
All frames have the property: 1-Fixed Single for Border Style
- MS Sans Serif 12 , Bold for font
Frame 1 Name :frmPersonal
Caption :Personal Information
Frame 2 Name :frmSex
Caption :Sex
Frame 3 Name :frmStat
Caption :Status
Frame 4 Name :frmMeal
Caption :Meal
Design a form (Login) that will request for a password. If the password is correct, another form is
given, otherwise an error message is displayed.
Combo box
Properties
Form Properties
Name :frmLogin
Border Style :1 – Fixed Single
Caption :Login
Startup position:2 – CenterScreen
Label properties
Label 1
Name :lblUser_name
Caption :&User Name
Label 2
Name :lblPassword
Caption :&Password
Command button 2
Name :cmdQuit
Caption :&Quit
Main Form
Command properties
Name :cmdClose
Caption :Close
Fontsize :12
Code
Private Sub Form_Load()
'does the initial setup of the screen and defines what data
'the control box should provide
With cboUser_name
.List(0) = "Administrator"
.List(1) = "nooreen"
.List(2) = "abdal"
End With
End Sub
End Sub
For ...Next
Syntax For counter = initial value Tofinal value
Instructions
Next counter
Instructions
Activity 1
Write a program using For ... Next that will output the integers from 1-5
Solution
Open a form with the following properties
Name :frmoutput
Caption :List Numbers
Activity 1
Write a program to print the even numbers less than 20
Solution
Open a form with the following properties
Name :frmEven
Caption :Even Numbers
Activity
Write a program using Do While ... Loop that will output the integers from 1-5
Solution
Open a form with the following properties
Name :frmList
Caption : Numbers
Write a program using Do... Loop Until that will output the integers from 1-5
Solution
Open a form with the following properties
Name :frmListintegers
Caption : Integers
Type the following program in view code window
Practise Questions
1. Write a program that will input 5 numbers. Calculate and print the total and average of these
numbers.
2. Write a program to input a number. Print the arithmetic table for that number.
3. A quality control inspector tests ovens by setting them to 200o and the measuring the actual
temperature inside the oven five times at ten minute intervals. Write a program to input the five
measurement and then print
a) The average temperature
b) The maximum temperature
c) The minimum temperature
During the test period.
4. Write a program that will enable you to input a password and output a message to inform you
whether it is correct or not. If you enter a wrong password, you will be given the chance to enter
it again up to a maximum of 3 times.
ERRORS IN VB
A bug is an error in the code which can prevent your program from running properly.
Debugging is the process of finding and removing errors.
The debug toolbar and Debug menu provide aids in tracking down logic errors.
Types of error
There are three types of errors:
Syntax errors – is a mistake in the grammar of Visual Basic. Examples include misspelling
keywords (e.g. Lop instead of Loop, forgetting an End If, an opening parenthesis without a
closing parenthesis.)
Run-time errors – causes the program to stop working. Example is trying to store a number that
is too big for a number (e.g. integer values can store up to 32767, if you try to store a number
bigger than this will cause overflow), attempting to divide a number by zero, trying to open a file
which is already open
Logic or semantic errors - error results from a mistake in the logic of your program code.
Examples are using the wrong logical operators (AND/OR) in loop conditions or assigning an
incorrect value to a variable or to use a wrong arithmetic symbol e.g. a + b instead of a - b.
Visual Basic’s Debug toolbar and menu