vb operator
TRANSCRIPT
-
8/8/2019 Vb Operator
1/44
OPERATORSOPERATORS
-
8/8/2019 Vb Operator
2/44
CATEGORIES OF
OPERATORS
Arithmetic Operators
Comparison Operators
Logical Operators
Concatenation Operators
-
8/8/2019 Vb Operator
3/44
ARITHMETIC OPERATORS
Operator Mathematical Function Example
+ Addition 4 + 2
- Subtraction 4 - 3
* Multiplication 4 * 3=12
/ Division 12/4=3
Mod Modulus (returns the remainder) 15 Mod 4=3
\ Integer Division (discard decimal 19\4=4
places)
^ Exponential 2^4=16
-
8/8/2019 Vb Operator
4/44
COMPARISION OPERATORS
Operator Meaning
= Equal to
> More than
< Less than
>= More than and equal
-
8/8/2019 Vb Operator
5/44
LOGICAL OPERATORS
Operator Meaning
And Both condition must be true
Or One condition must be true
Not Negates Truth
-
8/8/2019 Vb Operator
6/44
CONCATENATION OPERATOR
These operators are used to join two expressions.They are used as:
Answer= concatenation operator
Where Answer is the result of concatenation
Concatenation can be & or +.
-
8/8/2019 Vb Operator
7/44
NOTE
In case of & operator, if an expression is not a
string, it is converted to a string before
concatenation.
In case of + operator, if both the expressions
are Variant and contain numbers, then the result
will be the sum of two numbers.
If both the expressions are Variant and contain
string values, the result will be a concatenated
string.
-
8/8/2019 Vb Operator
8/44
CONTROLCONTROL STRUCTURESSTRUCTURES
ControlControl StructuresStructures areare thosethose programmingprogramming structuresstructures
thatthat controlcontrol thethe functioningfunctioning ofof aa programprogram.. ThereThere areare 22
typestypes ofof controlcontrol structuresstructures::--
1.1. DecisionDecision StructureStructure
2.2. LoopLoop StructureStructure
-
8/8/2019 Vb Operator
9/44
DECISION STRUCTURESDECISION STRUCTURES
TheseThese enableenable usus toto executeexecute aa certaincertain setset of of
statementsstatements basedbased onon somesome conditioncondition whichwhich
needsneeds toto bebe specifiedspecified atat designdesign timetime andand notnot atat
runrun timetime..
AtAt runrun--time,time, thethe conditioncondition isis evaluatedevaluated andanddependingdepending onon thethe resultresult ofof text,text, thethe blockblock ofof
codecode isis executedexecuted..
-
8/8/2019 Vb Operator
10/44
-
8/8/2019 Vb Operator
11/44
IfIf--thenthen--ElseElse StatementStatement
AA conditioncondition isis testedtested.. IfIf conditioncondition isis true,true, thenthen thethe statementsstatements
afterafter thenthen areare executedexecuted.. OtherwiseOtherwise thethe statementsstatements afterafter ElseElse
areare executedexecuted..
SyntaxSyntax:: IfIfconditioncondition thenthen
statementstatementElseElse
statementstatement
EndEnd IfIf
ExampleExample:: IfIfusername=managerusername=manager thenthenMsgBoxMsgBox truetrue
ElseElse
MsgBoxMsgBox falsefalse
EndEnd IfIf
-
8/8/2019 Vb Operator
12/44
Nested IfNested If
Syntax:Syntax:--
IfIf conditioncondition ThenThen
statementstatement
ElseElse
IfIf conditionsconditions ThenThen
statementstatement
End IfEnd If
End IfEnd If
-
8/8/2019 Vb Operator
13/44
IfIf--thenthen--ElseIfElseIf--Else StatementElse Statement
Syntax:Syntax:
IfIfcondition1condition1 thenthen
statement1statement1
ElseIfElseIfcondition2condition2 thenthenstatement2statement2
ElseIfElseIfcondition3condition3 thenthen
statement3statement3
ElseElsestatementstatement
End IfEnd If
-
8/8/2019 Vb Operator
14/44
IfIf condtionscondtions11 isis true,true, thethe programprogram willwill executeexecutestatementstatement11 andand stopstop examineexamine otherother conditionsconditions..
IfIf condtioncondtion11 isis false,false, thethe programprogram willwill examineexamine
condtioncondtion22 andand actact accordinglyaccordingly..
ItIt isis possiblepossible thatthat nonenone ofof thethe statedstated conditioncondition isistruetrue.. InIn thisthis case,case, youyou shouldshould provideprovide aa ElseElse
SectionSection..
TheThe ElseElse SectionSection mustmust bebe lastlast inin thethe listlist of ofconditionsconditions andand wouldwould actact if if nonenone of of thethe primaryprimary
conditionsconditions isis truetrue..
IfIf--thenthen--ElseIfElseIf--Else StatementElse Statement
-
8/8/2019 Vb Operator
15/44
Example ofExample of IfIf--thenthen--ElseIfElseIf--ElseElse
StatementStatement
Private Sub Form_Load()
If BackColor = vbRed Then
BackColor = vbBlueElseIf BackColor = vbBlue Then
BackColor = vbGreen
Else
BackColor = vbBlackEnd If
End Sub
-
8/8/2019 Vb Operator
16/44
WriteWrite a Program in Visual Basic to checka Program in Visual Basic to check
greaternumber.greaternumber.
Private Sub Command1_Click()
Dim i As Integer, j As Integer
i = Text1.Text
j = Text2.Text
Ifi > j Then
MsgBox "i is Greater"
ElseMsgBox "j is Greater"
End If
End Sub
-
8/8/2019 Vb Operator
17/44
-
8/8/2019 Vb Operator
18/44
QuestionQuestion
Write a Program in Visual Basic to check
whether a number is Even or odd.
-
8/8/2019 Vb Operator
19/44
ProgramProgram
Private Sub Command1_Click()
Dim i As Integer
i =T
ext1.T
extIfi Mod 2 = 0 Then
MsgBox "even number"
Else
MsgBox "odd number"End If
End Sub
-
8/8/2019 Vb Operator
20/44
-
8/8/2019 Vb Operator
21/44
Select Case StatementSelect Case Statement
Another way to control the program flow, that is, theSelect Case control structure.
However, the Select Case control structure is slightlydifferent from the If....ElseIfcontrol structure .
The difference is that the Select Case control structurebasically only make decision on one expression or dimension (for example the examination grade) whilethe If ...ElseIfstatement control structure may evaluate
only one expression. Select Case is preferred when there exist many
different conditions because using If...ThenElseIfstatements might become too messy.
-
8/8/2019 Vb Operator
22/44
Select Case StatementSelect Case Statement
ForFor multiplemultiple conditionalconditional statements,statements, itit isis betterbetter toto useuse
SelectSelect CaseCase.. TheThe formatformat isis ::
Select CaseSelect Case expressionexpression
Case expression1Case expression1
Statement1Statement1
CaseCase expression2expression2
Statement2Statement2
CaseCase expression3expression3Statement3Statement3
CaseCase ElseElse
StatementStatement
End SelectEnd Select
-
8/8/2019 Vb Operator
23/44
Example of Select Case StatementExample of Select Case Statement
Private Sub Form_Load()
Select Case BackColor
Case vbRed
BackColor = vbBlueCase vbBlue
BackColor = vbGreen
Case vbGreen
BackColor = vbBlack
Case Else
BackColor = vbRed
End Select
End Sub
-
8/8/2019 Vb Operator
24/44
Example of Using Select Case StatementExample of Using Select Case Statement
' Examination Grades' Examination GradesDim grade As StringDim grade As String
Private Sub Compute_Click( )Private Sub Compute_Click( )
grade = txtgrade.Textgrade = txtgrade.Text
Select Case gradeSelect Case grade
Case "A"Case "A"
result.Caption="Distinction"result.Caption="Distinction"
Case "B"Case "B"
result.Caption=First Division"result.Caption=First Division"
Case "C"Case "C"result.Caption=Second Division"result.Caption=Second Division"
Case ElseCase Else
result.Caption="Fail"result.Caption="Fail"
End SelectEnd Select
End SubEnd Sub
-
8/8/2019 Vb Operator
25/44
-
8/8/2019 Vb Operator
26/44
LoopingLooping
The other major type of control statement is the
loop. You use loops to perform repetitive tasks
in your program.
Do Loop
While. Wend loop
For....Next Loop
-
8/8/2019 Vb Operator
27/44
Do LoopDo Loop
Do While Loop
The keyword While in the Do While statement tells theprogram that the loop will be repeated while the conditionexpression is True. When the condition in a Do While
loop becomes false, the program moves on to the nextstatement after the Loop statement.
Do Until Loop
The Do Until loop is basically the same as the Do Whileloop except that the statements inside a Do Until loopare run only as long as the condition is False. When thecondition becomes True, the loop terminates.
-
8/8/2019 Vb Operator
28/44
Syntax ofDo While LoopSyntax ofDo While Loop
a) DoWhile condition
Block of one or more VB statementsLoop
b) DoBlock of one or more VB statements
LoopWhile condition
Note:
By placing theWhile condition in the Do statement, you tellthe program that you want to evaluate the condition beforeyou run any statements inside the loop. If the condition is
True, the repetitive statements between the Do statementand the Loop statement are run. Then the program returnsto the Do statement to evaluate the condition again. To runthe Do While loop at least once, you must use the secondform of the Do While loop. This form of the loop places thecondition in the Loop statement.
-
8/8/2019 Vb Operator
29/44
ExampleExample of Using Doof Using Do--While LoopWhile Loop
Private Sub Command1_Click ()
Dim count as Integer
count=0DoWhile count < 10
count = count + 1
Print count
Loop
End Sub
-
8/8/2019 Vb Operator
30/44
ExampleExample of Using Doof Using Do--While LoopWhile Loop
Private Sub Command1_Click ()
Dim count as Integer
count=0Do
count = count + 1
Print count
LoopWhile count < 10
End Sub
-
8/8/2019 Vb Operator
31/44
Do Until LoopDo Until Loop
a) Do Until conditionBlock of VB statements
Loop
b) DoBlock of VB statements
Loop Until condition
Note:
Do Until loop has two forms: one with the condition in
the Do statement and one with the condition in theLoop statement. If you place the condition in the Dostatement, it is evaluated before the statements in theloop are executed. If you place the condition in theLoop statement, the loop is run at least once before
the condition is evaluated.
-
8/8/2019 Vb Operator
32/44
ExampleExample of Using Doof Using Do--Until LoopUntil Loop
Private Sub command1_click()
Dim i As Integer
i = 1
Do Until i > 10
Print i
i = i + 1Loop
End Sub
-
8/8/2019 Vb Operator
33/44
ExampleExample of Using Doof Using Do--Until LoopUntil Loop
Private Sub command1_click()
Dim i As Integer
i = 1
Do
Print i
i = i + 1
Loop Until i > 10
End Sub
-
8/8/2019 Vb Operator
34/44
While.Wend loopWhile.Wend loop
General Format of ForNext Loop is:-
While Logical expression
One or more VB statements
Wend
-
8/8/2019 Vb Operator
35/44
Example of Using While.Wend LoopExample of Using While.Wend Loop
Sum=0
Count=1
While Count
-
8/8/2019 Vb Operator
36/44
For....Next LoopFor....Next Loop
General Format of ForNext Loop is:-
Forcounter=startNumber to endNumber (Step increment)
One or more VB statements
Next
-
8/8/2019 Vb Operator
37/44
Example of usingExample of using For....Next LoopFor....Next Loop
Private Sub Command1_Click()
Dim i As Integer
Fori = 1 To 10
Print i
Next i
End Sub
-
8/8/2019 Vb Operator
38/44
Examples of usingExamples of using For....Next LoopFor....Next Loop
(a) For counter=1 to 10
Print counterNext
(b) Forcounter=1 to 1000 step 10
Next
(c) Forcounter=1000 to 5 step -5Print counter
Next
-
8/8/2019 Vb Operator
39/44
Nested LoopNested Loop
Fori=1 to 10
Forj=1 to 10Print i, j
Next j
Next i
-
8/8/2019 Vb Operator
40/44
USER DEFINEDDATATYPESUSER DEFINEDDATATYPES
It is declared in the General Declarations of a codeIt is declared in the General Declarations of a code
module by using the Type and End Type keywords.module by using the Type and End Type keywords.
E.g. [Private/Public] Type studentE.g. [Private/Public] Type student
RollNo AsRollNo As IntegerInteger
Address AsAddress As StringString
DateofJoin AsDateofJoin As DateDate
TotalFee AsTotalFee As DoubleDouble
Rupees AsRupees As CurrencyCurrency
End TypeEnd Type
This defines a userThis defines a user--defined data type Student that isdefined data type Student that is
used for storing the student details.used for storing the student details.
-
8/8/2019 Vb Operator
41/44
Declaring Variables forDeclaring Variables for
UserUser--Defined Data TypeDefined Data Type
WeWe use Dim statement for this.use Dim statement for this.
E.g.E.g. DimDim stud1 Asstud1 As StudentStudent
DeclaresDeclares aa variablevariable studstud11 ofof studentstudent datadata typetype.. ThisThis
variablevariable cancan storestore allall thethe detailsdetails ofof aa studentstudent suchsuch asas thethe
name,name, addressaddress etcetc..
-
8/8/2019 Vb Operator
42/44
Retrieving values fromRetrieving values from
UserUser--Defined Data TypeDefined Data Type
AssigningAssigning andand RetrievingRetrieving valuesvalues fromfrom useruser--defineddefined typestypes isis
donedone usingusing thethe ..(DOT)(DOT) operator operator.. ThisThis isis similar similar toto
accessingaccessing propertiesproperties ofof controlscontrols..
EE..gg.. PrivatePrivate SubSub Form_Load()Form_Load()
DimDim studstud11 AsAs StudentStudent
studstud11.. TotalFeeTotalFee ==15001500..5050
studstud11..Address=PvcAddress=PvcMsgBoxMsgBox studstud11..TotalFeeTotalFee && studstud11..AddressAddress
EndEnd SubSub
-
8/8/2019 Vb Operator
43/44
GoTo StatementGoTo Statement
ThisThis statementstatement allowsallows thethe controlcontrol toto jumpjump
forwardsforwards oror backwardsbackwards inin thethe samesame procedureprocedure..
ThisThis cancan bebe aa numbernumber oror aa wordword followedfollowed byby aa
coloncolon ((::))..
TheThe GoToGoTo statementstatement isis followedfollowed byby aa labellabel
namename withoutwithout thethe coloncolon..
-
8/8/2019 Vb Operator
44/44
Example of Goto StatementExample of Goto Statement
Example :Example : Private Sub Form_Load()Private Sub Form_Load()
Dim x As IntegerDim x As Integer
frmMain.showfrmMain.show
x=0x=0
startloop:startloop:x=x+1x=x+1
print xprint x
if x=10 thenif x=10 then GoToGoTo endloopendloop
GoToGoTo startloopstartloopendloop:endloop:
MsgBox DoneMsgBox Done
EndEnd
End SubEnd Sub