0% found this document useful (0 votes)
7 views4 pages

UNIT III Visual Basic

This document covers programming fundamentals including variable declaration, data types, and the concept of null, error, and empty values. It explains the scope of variables, modules, constants, arrays, procedures, and loops, along with their syntax and examples. Additionally, it details different types of operators in Visual Basic, including arithmetic, relational, logical, and assignment operators.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

UNIT III Visual Basic

This document covers programming fundamentals including variable declaration, data types, and the concept of null, error, and empty values. It explains the scope of variables, modules, constants, arrays, procedures, and loops, along with their syntax and examples. Additionally, it details different types of operators in Visual Basic, including arithmetic, relational, logical, and assignment operators.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT III Programming Fundamental: Declaring variables, Data Types, The Null Value, The Error

Value, The Empty Value, Scope of Variable, Modules, Modules level Properties, Constants, Creating
your own Constants, Arrays, Multidimensional Arrays, Declaring Fixed Size Arrays, Procedures,
Subroutine &Functions, Split Bar, The Loops.
Declaring Variables

A variable is a name used to store data in memory. Before using a variable, it should be declared.
Syntax:
Dim variable name As Datatype

Example:
Dim age As Integer
Dim name As String
Dim salary As Double
 Dim means declare variable.
 Variable name should be meaningful.

Data Types
Data type defines what kind of value a variable can store.

Data Type Description Example


Integer Whole numbers 10
Long Large integers 50000
Single Decimal numbers 12.5
Double Large decimal values 1234.567
String Text values "Hello"
Boolean True / False True
Date Date values 21/04/2026
Variant Any type of data Mixed

The Null Value

Null means variable contains no valid data. Mostly used in databases.


Example:
If IsNull([Link]) Then
MsgBox "No Data"
End If

The Error Value

Used to represent an error in a variable.


Example:
CVErr(2001)
Useful in calculations or custom error handling.

The Empty Value


Empty means variable is not initialized.
Example:
Dim x
MsgBox IsEmpty(x)
Output: True

Scope of Variables
Scope means area where variable can be used. Its Types:
(a) Local Variable
Declared inside procedure.
Sub Test()
Dim x As Integer
End Sub
(b) Module Level Variable
Declared at top of module.
Dim y As Integer
Used by all procedures in module.
(c) Global Variable
Declared using Public.
Public z As Integer
Used in whole project.

Modules

A Module is a container for code like variables, procedures and functions.


 Standard Module
 Form Module
 Class Module
Used to organize program code.

Module Level Properties

Variables declared in General Declaration section of module are module level properties.
Dim count As Integer
Accessible by all procedures in same module.

Constants
A constant stores fixed value that cannot change.
Syntax:
Const PI = 3.14
Example:
MsgBox PI

Creating Your Own Constants


Const CollegeName As String = "Bareilly College"
Const MaxMarks As Integer = 100

Arrays
An array stores multiple values in one variable.
Syntax:
Dim marks(5) As Integer
Example:
marks(0)=50
marks(1)=60

Multidimensional Arrays
Array with rows and columns.
Syntax:
Dim matrix(2,2) As Integer
Example:
matrix(0,0)=10
matrix(0,1)=20

Declaring Fixed Size Arrays


Size is fixed during declaration.
Dim num(10) As Integer
Cannot change size later.
Procedures
Procedure is block of code that performs task.
Types:
 Sub Procedure
 Function Procedure

Subroutine (Sub Procedure)


Used to perform task but returns no value.
Example:
Sub Welcome()
MsgBox "Welcome"
End Sub
Call by:
Welcome

Functions
Returns a value.
Example:
Function Add(a,b)
Add = a + b
End Function
Call:
MsgBox Add(5,3)

Split Bar
Split Bar is used to divide window into two panes for viewing different parts of code or document.
Useful in large programs.
Helps scrolling separately.

The Loops
Loops repeat statements multiple times.
(a) For...Next Loop
For i = 1 To 5
MsgBox i
Next i

(b) Do While Loop


Do While x < 5
x=x+1
Loop

(c) Do Until Loop


Do Until x = 5
x=x+1
Loop

(d) While...Wend
While x < 5
x=x+1
Wend

Differences Between Do While and Do Until


No. Do While Do Until
1 Executes while condition is True Executes until condition becomes True
2 Stops when condition becomes False Stops when condition becomes True
3 Uses positive condition Uses negative/opposite condition
4 Example: Do While x < 5 Example: Do Until x = 5
Visual Basic Operators
Operators are symbols used to perform operations on values and variables. Types of Operators in VB
Type
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operator
5. String Operator

1. Arithmetic Operators
Operator Meaning Example
+ Addition 5 + 3 = 8
- Subtraction 5 - 3 = 2
* Multiplication 5 * 3 = 15
/ Division 6 / 2 = 3
\ Integer Division 7 \ 2 = 3
Mod Remainder 7 Mod 2 = 1
^ Power 2 ^ 3 = 8

2. Relational Operators
Operator Meaning
= Equal to
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal

3. Logical Operators
Operator Meaning
And Both conditions true
Or Any one true
Not Opposite result
Xor Only one true

4. Assignment Operator
Used to assign value to variable.
x = 10
name = "Rahul"

String Concatenation Operator


Used to join text.
name = "BCA" & " Student"
Output: BCA Student

You might also like