LATEST UPDATES

Search This Blog

Chapter 3. Elements of VB Programming

Wednesday, September 4, 2013

Chapter 3. Elements of VB Programming
Data types
Data types are used to store and identify the type of data that are supplied through the variables to a program.
They are, String, Integer, Long, Single, Double, Currency, Date, Boolean and Variant.
Data type/
Key words
Usage
Storage size
Range
String
To store Group of characters enclosed  in double quotes called string
10 bytes +
string length
0-2billion
Integer
To represent numeric data
without fractions (Whole numbers)
2 bytes
-32768 - +32767
Long
To represent large numeric
data without fractions
(large Whole numbers)
4 bytes
214 crore
(Aprx)
Single
To represent numeric data
with fractions
(Single precision floating
point numbers)
4 bytes
(7 digit Fractional part)
Double
To represent large numeric
data with fractions
(Double precision floating
point numbers)
8 bytes
(16 digits Fractional part)
Currency
To represent financial data
8 bytes
(4 digits Fractional part)
Date
To store date and time in a format
8 bytes

Boolean
For logic variables to store
true or false
2 bytes

Variant
It can contain any type of data
22 bytes
+ string length

Variables
They are named storage locations to hold data. At run time we can change the content of a variable.
Declaring variables
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.
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”
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
If…then…
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”
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
          It will display the grade according to the percentage. Here we can include any number of conditions and it works with its true or false results.
Select case statement
The select case statement is used to check different values for an expression.
Syntax
Select case <Expression>
Case <value1>
<statement1>
Case <value2>
<statement2>
Case <value3>
<statement3>
………………
……………..
case else
<statements>
End select

Eg:-
Private Sub Command1_Click()
numday = Text1.Text
Select Case numday
Case 1
MsgBox "Sunday"
Case 2
MsgBox "Monday"
Case 3
MsgBox "Tuesday"
Case 4
MsgBox "Wednesday"
Case 5
MsgBox "Thursday"
Case 6
MsgBox "Friday"
Case 7
MsgBox "Saturday"
Case Else
MsgBox "Enter a valid number of day"
End Select
End Sub
It display the day in a week if the input is 1 to 7 otherwise displays a message box with message "Enter a valid number of day".
Loop statements
Loop statements are used to execute a group of statements repeatedly.
In a loop there is three elements first one is an initialization of loop control variable (To control the repeated execution of statements), second one is the condition checking of loop control variable, third one is updating of loop control variable and last one is loop body (Group of statements that are to be executed repeatedly).
Loop statements are classified as follows,
·        Do…loop (Do while….loop, Do until……loop)
·        While….wend
·        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
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
For <Loopcontrolvariable>  = Ist to Last <Step Incrementt/Decrement>
<Body of loop>
Next <Loopcontrolvariable>
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
          It will print 1 to 5 on the form.
Exit statement
It is used to exit from a loop conditionally.
Eg:-
Private Sub Command1_Click()
For i = 1 To 10
Print i
If i = 5 Then Exit For
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

No comments:

Post a Comment

 

Welcome Plus one ,How you rate IT Club SKHSS

Members

Contact Form

Name

Email *

Message *

Visitors Counter

Most Reading

SREEKRISHNA HIGHER SECONDARY SCHOOL- IT CLUB. Powered by Blogger.