Chapter 12 - Elements of Visual Basic Programming - Act Malappuram
Chapter 12 - Elements of Visual Basic Programming - Act Malappuram
act malappuram
Syllabus home > Syllabus > Theory Notes > +1 Commerce Theory Notes Titles >
Syllabus
Chapter 12: Elements of Visual Basic Programming
Year Plan
Year Plan
1 of 10 7/25/17, 7:46 PM
Chapter 12: Elements of Visual Basic Programming - act malappuram https://sites.google.com/site/togethertowinbykhalil/home/syllabus/theory-...
Syntax
Dim Variablename As Datatype
Eg: -
Dim amount As currency
Dim mark As integer
Dim name As string
Variable naming rules
It must be unique not a key word.
It can contain maximum 255 characters.
It must begin with an alphabet.
Symbols, Special symbols, space are not allowed except underscore.
Constants
They are values stored in a variable. They are numeric constants
and string constant.
Numeric constants may be integer, long, single, double.
Eg:- Dim age As Integer
Age=20
String constants enclosed in double quotes.
Eg: - Dim state As String
state= Kerala
Operators
They are symbols used in an expression or operation. Data stored in
a variable or constants are processed by using operators.
They are Arithmetic Operators, Relational Operators, Logical
Operators, and Concatenation Operator.
2 of 10 7/25/17, 7:46 PM
Chapter 12: Elements of Visual Basic Programming - act malappuram https://sites.google.com/site/togethertowinbykhalil/home/syllabus/theory-...
Arithmetic Operators
Operator Usage Example A=5, B=2
+ Addition A+B (7)
- Subtraction A-B (3)
* (Asterisk) Multiplication A*B (10)
/ Division A/B (2.5)
\ Integer Division A\B (2)
Mod Reminder of division A mod B (1)
^ (Cap) Exponentiation A ^ B (25)
Relational Operators
They are used to check some conditions. Its result is either true or
false. They are
Operator Usage Example (A=6, B=9)
= Equal to A=B (False)
< Less than A<B (True)
> Greater than A>B (False)
<= Less than or equal to A<=B (True)
>= Greater than or equal to A>=B (False)
<> Not equal to A<>B (True)
Logical Operators
It helps us to combine two relational operations. Its output is also
true or false. They are AND, OR, NOT
If A and B are outputs of any relational operations, then
A B A AND B A OR B NOT A NOT B
T T T T F F
T F F T F T
F T F T T F
F F F F T T
Concatenation Operator
It is used to join two strings.
Eg:-
Dim a, b, c as string
a= Together
b= to win
3 of 10 7/25/17, 7:46 PM
Chapter 12: Elements of Visual Basic Programming - act malappuram https://sites.google.com/site/togethertowinbykhalil/home/syllabus/theory-...
c= a&b
Its output is: Together to win.
Control Structures
They are used to alter the normal/ sequential execution of a
program. They are classified into two, Decision statements and Loop
statements.
Decision statements
It contains an expression (Relational or Logical) it evaluates the
condition first and executes a group of statements according to that
condition. They are
If..then.
If.Then.else.
If..then.Else if.else.
Select case
Ifthen
Syntax:
If <condition> then
<Statements>
End if
Eg:-
If a>b then
Msgbox a is large
Here the condition evaluates first, if it is true the statements will be
executed otherwise exit from if statement.
If.Then.else.
Syntax:
If <condition> then
<Statement1>
Else
<statement2>
End if
Eg:-
If a>b then
Msgbox a is large
Else
Msgbox b is large
4 of 10 7/25/17, 7:46 PM
Chapter 12: Elements of Visual Basic Programming - act malappuram https://sites.google.com/site/togethertowinbykhalil/home/syllabus/theory-...
End if
Here the condition evaluates first, if it is true the statement1 will be
executed otherwise statement2 will be executed.
If..then.Else if.else.
Syntax
If <condition1> then
<statement1>
elseif <condition2>
<statement2>
elseif <condition3>
<statement3>
.
.
else
<statements>
end if
Eg:-
If percent > 90 Then
Text3.Text = "A+"
ElseIf percent > 80 AND percent < 90 Then
Text3.Text = "A"
ElseIf percent > 70 AND percent < 80 Then
Text3.Text = "B+"
ElseIf percent > 60 AND percent < 70 Then
Text3.Text = "B"
ElseIf percent > 50 AND percent < 60 Then
Text3.Text = "C+"
ElseIf percent > 40 AND percent < 50 Then
Text3.Text = "C"
ElseIf percent > 30 AND percent < 40 Then
Text3.Text = "D+"
Else
Text3.Text = "D"
End if
5 of 10 7/25/17, 7:46 PM
Chapter 12: Elements of Visual Basic Programming - act malappuram https://sites.google.com/site/togethertowinbykhalil/home/syllabus/theory-...
6 of 10 7/25/17, 7:46 PM
Chapter 12: Elements of Visual Basic Programming - act malappuram https://sites.google.com/site/togethertowinbykhalil/home/syllabus/theory-...
Case 7
MsgBox "Saturday"
Case Else
MsgBox "Enter a valid number of day"
End Select
End Sub
For..next
Do.loop
Syntax
<Initialization expression>
Do while <Condition expression >
<Body of loop>
<Updating expression >
Loop
Eg:-
Private Sub Command1_Click()
i=1
Do While i <= 10
Print i
i=i+1
Loop
End Sub
It will print 1 to 10 on the form. Here Body of loop will executed
repeatedly if the condition is true until it false.
Do until..loop
7 of 10 7/25/17, 7:46 PM
Chapter 12: Elements of Visual Basic Programming - act malappuram https://sites.google.com/site/togethertowinbykhalil/home/syllabus/theory-...
Syntax
<Initialization expression>
Do until <Condition expression >
<Body of loop>
<Updating expression >
Loop
Eg:-
Private Sub Command1_Click()
i=1
Do Until i > 10
Print i
i=i+1
Loop
End Sub
It will print 1 to 10 on the form. Here Body of loop will executed
repeatedly if the condition is false until it true.
While.wend
Syntax
<Initialization expression>
While <Condition expression >
<Body of loop>
<Updating expression >
Wend
Eg:-
Private Sub Command1_Click()
i=1
While i <= 10
Print i
i=i+1
Wend
End Sub
It will print 1 to 10 on the form. Here Body of loop will executed
repeatedly if the condition is true until it false, i.e. same as do while loop.
For..Next
Syntax
8 of 10 7/25/17, 7:46 PM
Chapter 12: Elements of Visual Basic Programming - act malappuram https://sites.google.com/site/togethertowinbykhalil/home/syllabus/theory-...
Eg:-
Private Sub Command1_Click()
For i = 1 To 10 Step 1
Print i
Next i
End Sub
It will print 1 to 10 on the form. Here Body of loop will executed
repeatedly if the value of loop control variable equal to 10.
Nesting of control structures
If one control structure contains another control structure inside in
its body is called nesting of control structures.
It may be
A decision within another decision
A decision within another loop
A loop within another loop
A loop within another decision
Eg:-
Private Sub Command1_Click()
For i = 1 To 10
If i <= 5 Then
Print i
End If
Next i
End Sub
9 of 10 7/25/17, 7:46 PM
Chapter 12: Elements of Visual Basic Programming - act malappuram https://sites.google.com/site/togethertowinbykhalil/home/syllabus/theory-...
Next i
End Sub
It will print 1 to 5 on the form then exit from the for loop statement.
Goto statement
It is used to jump from one line of statement to another statement
unconditionally.
Eg:-
Private Sub Command1_Click()
a = Text1.Text
b = Text2.Text
If b = 0 Then
GoTo Error
Error:
MsgBox "Division is not possible"
Else
c=a/b
MsgBox c
End If
End Sub
It will produce the divided result if b not equal to zero otherwise displays
a message box with message "Division is not possible".
End statement
It indicates the termination of a VB program. It closes the running
windows of VB Program.
Private Sub Command1_Click()
End
End Sub
10 of 10 7/25/17, 7:46 PM