CIT - 206 - Lecture - 2 - Introduction To Event Driven Programming
CIT - 206 - Lecture - 2 - Introduction To Event Driven Programming
Operator Description
= Equal to
Example:
Dim 1a as Integer = 2
Dim 1b as Integer = 3
1a = 1b (Condition)
Answer is false
> Greater than
Example:
Dim 1a as Integer = 2
Dim 1b as Integer = 3
1a > 1b (Condition)
Answer is false
< Less than
Example:
Lecture Two-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
Dim 1a as Integer = 2
Dim 1b as Integer = 3
1a < 1b (Condition)
Answer is True
>= Equal to or
Greater
Than
Example:
Dim 1a as Integer = 2
Dim 1b as Integer = 3
1a >= 1b (Condition)
Answer is false
<= Less
than
or
Equal to
Example:
Dim 1a as Integer = 2
Dim 1b as Integer = 3
1a <= 1b (Condition)
Answer is true
Example:
Dim 1a as Integer = 2
Dim 1b as Integer = 3
1a < > 1b (Condition)
Answer is True
Upper case letters are less than lowercase letters, “A”<”B”<”C”<”D”…….<”Z” and
number are less than letters.
2. Logical Operators
In certain cases, we might need to make more than one comparisons to arrive
at a decision. In this case, using numerical comparison operators alone might
not be sufficient and we need to use the logical operators, as shown in Table
below Logical operators can be used to compare numerical data as well as
non-numeric data such as strings.
Example:
Dim 1a as Integer = 2
Dim 1b as Integer = 3
Dim 1c as Integer = 4
(1a < 1b) And (1c < > 1a)
Answer is true
One side or other must be true
Example:
Dim 1a as Integer = 2
Or
Dim 1b as Integer = 3
Dim 1c as Integer = 4
(1a > 1b) Or (1c < > 1a)
Answer is False
One side or other must be true but not both
Example:
Dim 1a as Integer = 2
Xor
Dim 1b as Integer = 3
Dim 1c as Integer = 4
(1a < 1b) Xor (1c < > 1a)
Answer is true
Negates true
Example:
Dim 1a as Integer = 2
Not
Dim 1b as Integer = 3
Dim 1c as Integer = 4
Not (1c < 1a)
Answer is true
This statements or structures allow you to run one or more lines of code
repetitively. We can write a code in Visual Basic that allows the program to
run repeatedly until a condition or a set of conditions is met. This is code
statements are known as loops. Looping is a very useful feature of Visual
Basic because it makes repetitive works easier. You can repeat the statements
in a loop structure until
A condition is True
A condition is False
A specified number of times
Once for each element in a collection.
1. Do loop
Lecture Two-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
2. For Next
3. For Each Next
4. While End While
5. With End With
Inside Loop statements code are executed one by one but you can use special
kind of statements known as control statements to change order of which code
is executed. The control statements in VB.NET are given below:
1. Exit Statement
2. Continue Statement
3. GoTo Statement
The Do...Loop construction allows you to test a condition at either the beginning
or the end of a loop structure. You can also specify whether to repeat the loop
while the condition remains True or until it becomes True.
' -or-
Do
[ statements ]
//[ Continue Do ]
[ statements ]
// [ Exit Do ]
[ statements ]
Loop { While | Until } condition
a)
Do While condition
Block of one or more VB statements
Loop
b)
Do
Block of one or more VB statements
Loop While condition
c)
Do Until condition
Lecture Two-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
Block of one or more VB statements
Loop
d)
Do
Block of one or more VB statements
Loop Until condition
Parts
Term Definition
Do Required. Starts the definition of the Do loop.
While Required unless Until is used. Repeat the loop until condition is False.
Until Required unless While is used. Repeat the loop until condition is True.
condition Optional. Boolean expression. If condition is Nothing, Visual Basic treats it
as False.
statements Optional. One or more statements that are repeated while, or
until, condition is True.
Continue Optional. Transfers control to the next iteration of the Do loop.
Do
Exit Do Optional. Transfers control out of the Do loop.
Loop Required. Terminates the definition of the Do loop.
Example 1
Create a Form with any name, add button with name BtnDoLoop, then
under its event procedure add below code: Then debugging the form
by place run debugging
Next
Example
Create a Form with any name, add button with name BtnForNext, then
under its event procedure add below code: Then debugging the form
by place run debugging
counter = counter + 1
MessageBox.show (counter)
Next
counter=counter-10
Beep()
Next
For n=1 to 10
Next
The For Each...Next construction runs a set of statements once for each element in
a collection. You specify the loop control variable, but you do not have to
determine starting or ending values for it.
[ statements ]
[ statements ]
// [ Exit For ]
[ statements ]
Next
Parts
Term Definition
element Required in the For Each statement. Optional in the Next statement.
Variable. Used to iterate through the elements of the collection.
datatype
Optional if Option Infer is on (the default) or element is already
declared;
required if Option Infer is off and element isn't already declared.
The data type of element.
group Required. A variable with a type that's a collection type or Object.
Refers to the collection over which the statements are to be repeated.
statements Optional. One or more statements between For Each and Next that run
on each item in group.
Continue For Optional. Transfers control to the start of the For Each loop.
Exit For Optional. Transfers control out of the For Each loop.
Next Required. Terminates the definition of the For Each loop.
Example
' Create a list of strings by using a collection initializer.
While condition
[ statements ]
Lecture Two-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
[ statements ]
[ statements ]
End While
Parts
Term Definition
condition Required. Boolean expression. If condition is Nothing, Visual Basic treats
as False.
statements Optional. One or more statements following While, which run every
time condition is True.
Continue Optional. Transfers control to the next iteration of the While block.
While
Exit While Optional. Transfers control out of the While block.
End While Required. Terminates the definition of the While block.
Example
End While
‘Output: 0 1 2 3 4 5 6 7 8 9 10
2. Decision structures
Visual Basic lets you test conditions and perform different operations depending on
the results of that test. You can test for a condition being true or false, for various
values of an expression. In order to do that you will use decision statements (In C++
they are called conditional.)
Statement Description
If A > 10 Then
MessageBox.Show("It is greater than ")
' Multiple-line syntax:
If condition Then
[ statements ]
End if
Lecture Two-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
Example:
' If A > 10, execute the MessageBox
statement in the order
If A > 10 Then
MessageBox.Show(" It is greater than ")
End if
Example:
Dim randomizer As New Random()
'set our variable
Dim count As Integer =
randomizer.Next(0, 5)
Syntax:
No General syntax just put one if statement
inside another if statement as shown in below
example
Example:
' Determine the current day of week and hour
of day.
Dim dayW As DayOfWeek =
DateTime.Now.DayOfWeek
Dim hour As Integer =
DateTime.Now.Hour
MessageBox.Show("Between 1 and 5,
inclusive")
' The following is the only Case
clause that evaluates to True.
Case 6, 7, 8
MessageBox.Show("Between 6 and 8,
inclusive")
Case 9 To 10
MessageBox.Show("Equal to 9 or 10")
Case Else
MessageBox.Show("Not between 1 and
10, inclusive")
End Select
nested Select Case statements You can use one select case statement inside
another select case statement(s).
Example:
Try put select case inside select case
Tutorial
1. Create a form and insert a text box and rename it as txtNum and a button
and rename it as OK. Write the code so that when the user runs the
program and enter a number that is greater than 100, he or she will see
the “You win a lucky prize” message. On the other hand, if the number
entered is less than or equal to 100, the user will not see any message.
2. This program can compute the grade for the mark entered by the user. Is
uses several Else If statements and the logical operator And to achieve
the purpose.
Dim a As Integer
Dim b As Integer
a = 3
Lecture Two-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
b = 4
If a > b Then
MsgBox ("a is greater then b")
Else
MsgBox ("b is greater then a")
End If
NB: Select Case and try Catch Finally is not your level. Read them fou
your passions.