0% found this document useful (0 votes)
3 views

Visual_Basic

The document provides an overview of the basics of Visual Basic, focusing on data types, variables, and operations. It explains various data types such as Integer, Long, Single, Double, Boolean, and String, along with rules for naming variables and examples of declaring and assigning values. Additionally, it covers arithmetic and logical operations, operator precedence, and includes exercises for practice.

Uploaded by

tashingamurove7
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Visual_Basic

The document provides an overview of the basics of Visual Basic, focusing on data types, variables, and operations. It explains various data types such as Integer, Long, Single, Double, Boolean, and String, along with rules for naming variables and examples of declaring and assigning values. Additionally, it covers arithmetic and logical operations, operator precedence, and includes exercises for practice.

Uploaded by

tashingamurove7
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Visual Basic

Basics of Visual BASIC language (Data Types, Variables and Operations)

Data Types, Variables and Operations

Letters
A…Z , a…z
Digits
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols
?, + , - , * , / , } , { , < , > , # , @ , & , ^ , % , ( , ) , !

Numeric Constants
5 , 10.5 , 20 , -40 , -10.2 , 3.14

Character Constants

“Jordan” , “ JD ” , “ Visual Basic ” , “Cheese” , “You have made an


error!”
“a” , “75” , “*********” , “How are you?”
Variables
A variable is simply a name you give to an area of memory in which
a data value used by your program is stored. When you need to
retrieve that data, or modify its value, you can refer to the memory
location by the variable's name.

Every variable has a Name , Type , Size , value


A value placed in a memory location replaces the value previously stored

Thepreviousvalueisdestroyed

When value is read from a memory location, it is not destroyed


Rules for Naming Variables

Which of the following are legal variable names ?


A. 2beOrNot2be
B. toBeOrNottoBe
C. XYZ123
D. XY+@#%234
E. PRINT
F. 10variable
G. My Name
H. variable_8
I. –variable
J. AB CDEF
K. VisualBasic
L. Hello
M. Schools
N. 5STAR
O. Ali Mohammad

Data Types

Integer(%) - Numbers without a decimal point (-32,768 to


32,767)
Long -Long integer (-2,147,483,648 to 2,147,483,647)
Single(!) - floating point number (seven significant digits)
Double -Long Single (15 significant digits)
Boolean -True or false
Date - use with date/time value from 1/1/100 to 12/31/9999.
String($)– Characters including
letters ,number ,spaces ,punctuation.
Currency -Dollar amounts
Object -Any object reference such as Word document
Variant - default, can hold any data type

EXAMPLE
integer 200 ,10 ,-70 , 80
long (integer) 35455 , 5678943
single 115.78 ,1.907 ,3.65
double 123.78129024, 12.876097
Boolean True or False
String “Hello ,my name is Ali”
Declaring Variables
General form:
Dim Variable1 As datatype1, variable2 As datatype2 , etc.
Dim var_name1, var_name2 , … As datatype
For example:
Dim x As long
Dim y As Double
Dim Name As String, Value As Single
Dim A ,B,C As Integer

Data type : is one of the following types:


Boolean, Date, Integer, Long, Single, Double, Currency, String,
Variant
Assigning Values to Variables
Variable_name = value
Examples:
X = 100
N1 = 5
Name ="Ali"
LoanAmount = 67.38
Length = 17.8
Note: Order is important. Variable name always on the left, and value on
the right.
Declare integer , single , string
Integer (%)
To declare : dim x% or dim x as integer
Single(!)
To declare : dim Y! or dim x as Single
String($)
To declare : dim Z$ or dim Z as String
Declare integer(%) and string($)
Private Sub Command1_Click()
Dim a%, b% integer a , b
a = 20 b = 30
Print a + b
End Sub
50

Private Sub Command1_Click()


Dim a$, b$ string a , b
a = 20 b = 30
Print a + b
End Sub

2030

StringVariable
Declaration Dim Name As String

Variable name Data type

Assignment: Name = “Ali


Mohammad”
Declaring and Assigning Values to Variables
Example
Dim Name As String ➔ Name= " Ali "
Dim Num1 As Integer ➔ Num1=33
Dim Num2 As single ➔ Num2=33.6
Dim Num3 As Double ➔ Num3 =12.35467
Dim X As Long ➔ x=12345678

Multiple Declarations
Dim a, b As Double
Two other types of multiple-declaration statements are
Dim a As Double, b As Integer
Dim c As Double, d As Integer
Dim a , b , c As Single , x , y As Double, i As Integer
a, b, and c are all Single; x and y are both Double
StringVariable
You can assign the value of one string variable to another.
Dim str1 As String str1 = "Hello"
Dim str2 As String str2 = "Goodbye"
str2 = str1
Print str2
Output: Hello
AssignmentStatement
Dim num As Double
Dim num2 As Double
Num =5 Num2 =4
num = 3 * num2
Print num
Output: 12
Example
Dim Name As String
Dim Num1 As Integer
Dim Today As Date
Name = " Ahmad "
Num1 = 12
Today = "12/02/2012 "

Dim x as date
x = #20/03/2012#
Print x ➔ 20/03/2012
Dim x As Date x =#12:30:44 AM#
Print x ➔ 12:30:44 AM

Example
Dim Number As Integer Number = 31
' This is ok
Number = “I didn't”
' Error: type mismatch (Number is an integer
while “I didn't” is a string)
DefaultValues for Data Types
Data type Default (Initial) value
All numeric types Zero (0)
( Integer , Long, single ,Double )

Boolean False

Char Binary 0

String or Object Empty


12:00 a.m. on January 1,
Date 0001

Arithmetic and Logic Expressions


Arithmetical Operators
+,-,*,/ (division), Mod, ^,\ ( integer division )
Logical Operators
AND, OR ,NOT
Equality and Relational Operators
>,<, >=, <= , = , <>
String Operators
+ , & ➔ String Concatenation
StringOperators
For String Variables the only valid operation is that of combining
two strings into one.

Use + or & to combine them


Example:
str1 = “Visual” + “ Basic” ➔ str1= Visual Basic str2 = “Visual”
& “ Basic” ➔ str2= Visual Basic
What is the output?
Private Sub Command1_Click()
Print 1 + 2 ' each of them is number Print "1" + 2
' One of them number
Print “1” + “2” ' each of them is String
Print “Computer “ + “Skills(2)”
Print “1” + 2 + “3” + 4 + “5”
Print 1 + “2” + 3 + 4 + “5”
End Sub

String Concatenation (&)

Private Sub Command1_Click()


Print 1 & 2 Print "1" & "2"
Print "Computer " & "Skills(2)"
Print "1" & 2 & "3" & 4 & "5"
End Sub

Arithmetic Expression
Integer division
 Uses the backslash, \
 7 \ 4 evaluates to 1
Floating-point division
 Uses the forward slash, /
 7 / 4 evaluates to 1.75
Modulus operator, Mod
 Yields the remainder after Integer division
 7 Mod 4 yields 3
 4 mod 7 yields 4
 7 mod 7 yields 0
 7 mod 1 yields 0
Precedence of arithmetic and relational operators

Order of Operations
Order of precedence in arithmetic expressions from highest to lowest
1. Any operation inside parentheses ()
2. Exponentiation ^
3. Multiplication and division * /
4. Integer division \
5. Modulus mod
6. Addition and subtraction + -
Arithmetic

Arithmetic
Ex: 5 – 3 + 6 + 2*2 ➔ Result = 12
Ex: 3 + 5*(10 – 6) ➔ Result = 23
How to represent the following example in vb6
Ex: 2 – 5(6 – 9)3
➔➔Print 2 - 5 * (6 - 9) ^ 3 ➔ Result = 137
Ex: 7 + 2 * (9 / 3) – 1
Expression in parentheses 7 + 2 * (9 / 3) – 1
1. Multiplication 7 + 2 * 3 – 1 2.
2. Addition and subtraction from left to right 7 + 6 - 1 = 12
Print 7 + 2 * (9 / 3) – 1 12
How to represent the following example in V.B 6.0

F= aX2+bX+C ➔F =a*X^2+b*X+C
F= 2((X-Y)+3XY)+Z ➔ F=2*((X-Y)+3*X*Y)+Z
F= X2+Y2+Z3 ➔ F= X^2+Y^2+Z^3
F= 20X2+3X+3 ➔ F= 20*X^2+3*X+3
F=(X2+Y3)2X ➔ F=(X^2+Y^3)*2*X

IF X=5 , Y=2 FIND THE VALUE OF Z


Z=X \ Y + X ^ 2 + 3 * X
Z=Y * X - ( X – Y ) + Y ^ 3
Z=( 5 * X –Y ^ 2 ) ^ 2
Z=Y – X ^ 2 + 3 * ( 4 * X + Y )
Z=3 * X + 5 * ( Y+7 ) +Y ^ X / Y * 2
Z=(X+Y) MOD Y
Exercise
A string variable that has not been assigned a value has the value zero.
1. True
2. False
The following is a string: "Hello World!“
1. True
2. False
The following is a string : 123e45
1. True
2. False
Exercise
Which of the following is a string?
(1) print
(2) 3.19e4
(3) "string"
(4) hello
Which of the following is not a legal variable name?
(1) 14_national_championships
(2) main
(3) X_Y
(4) sum
Exercise Which of the following is a legal variable name?
(a) And
(b) cdef
(c) Dim
(d) 1TwoThree
Which is a legal variable name?
(a) a
(b) False
(c) "a"
(d) 10
Exercise
Which of the following is not a legal statement in Visual Basic?
(a) dim x = -22
(b) x = -22
(c) x + 1 = 22
(d) x = a * b

What is the output of the following code? :


a = 75.2 b = 128
Print a, b
(a) 9625.6
(b) 75.2128
(c) 128
(d) 75.2 128
Exercise
What is the output of the following codes? :
1) Dim x As Date Print x ‘ output 12:00:00 AM
2) Dim x As Integer Print x ‘ output 0
3) Dim x As String Print x ‘ output Nothing to print
Exercise
Dim X As Single
Which of the following values the variable X can be assigned :
(a) "Lucas "
(b) 12/23/1999
(c) 25.99
(d) 33.94$
Which is the correct order for arithmetic operations ?
(a) *, /, +, -, ^, ( )
(b) ( ), ^, * ,/, + , -
(c) ^, * /, + -, ( )
(d) ( ), * /, + -, ^
Exercises
1. Find the Summation for two integer numbers
2. Find the Average for three integer numbers
Input Two Numbers and find the summation
Private Sub command1_Click

Declare variables
Dim First as Integer, Second as
Integer
Dim Sum as Integer
Assign values to variables
First = val (text1.Text)
Second = val (text2.Text)
Sum= First+ Second
Print Sum

End Sub

You might also like