Upload
akshay-suresh
View
222
Download
0
Embed Size (px)
Citation preview
8/8/2019 Unit II Programming Fundamentals
1/29
VB Programming Fundamentals
8/8/2019 Unit II Programming Fundamentals
2/29
Variables values that can change during a
program execution
Constants values that remain the fixed
throughout the program run.
Reserved words/ Keywords words with
predefined meaning. E.g.
8/8/2019 Unit II Programming Fundamentals
3/29
Data types in VB
Data types are means to identify the type of dataand associated operations on it.
8/8/2019 Unit II Programming Fundamentals
4/29
Data Type Description
Boolean True / false
Byte Allows positive integers in the
range 0-255
Currency Used to hold currency data (
up to 4 decimal places)
Date Used to hold date and time
Double Holds numeric values ( 14
digits of precision)
Integer Integers in range ( -32768 to
32767)
8/8/2019 Unit II Programming Fundamentals
5/29
Data Type Description
Long Integer / Long Holds integer values greater
than those supported by
integer data typeObject Used to hold and refer to
objects such as controls and
forms
Single Holds numeric values ( 6
digits of precision)String Holds multiple characters
textual data.
Variant Can hold data of any type
except strings of fixed length
or user defined data types
8/8/2019 Unit II Programming Fundamentals
6/29
A variable is a temporary data storage location in your program. Your code can use one or many variables,which can contain words, numbers, dates, properties, orobject references. Variables are useful because they let
you assign a short, easy-to-remember name to a piece ofdata you plan to work with. Variables can hold:
User information entered at run time.
The result of a specific calculation.
A piece of data you want to display on your form.
In short, variables are simple tools you can use to trackalmost any type of information whose value can change.
8/8/2019 Unit II Programming Fundamentals
7/29
Declaring a variable is the process of creating avariable in your Visual Basic program code. Youcan do this explicitly with the Dim keyword or
implicitly by typing a new variable name in aprogram statement.
Explicit Declaration
In Visual Basic, one of the ways to create avariable is to declare it explicitly using the Dimstatement.
8/8/2019 Unit II Programming Fundamentals
8/29
Dim < var name> [as ]
Where Dim is a keyword that tells VB that a variable is
being declared.
is the variable name
As is the keyword that tells VB the data type ofthe variable
is a legal data type in VB
[] brackets mean that the part is optional
8/8/2019 Unit II Programming Fundamentals
9/29
E.g.
Dim roll_no as Byte
Dim name as string
Dim amt as currency
Note : Default data type is Variant
8/8/2019 Unit II Programming Fundamentals
10/29
Implicit declaration
You can also declare a variable without the Dim
statement; this process is called implicitdeclaration.
To declare a variable implicitly, you simply use thevariable on its own and skip the Dim statement
altogether. E.g.I=34
8/8/2019 Unit II Programming Fundamentals
11/29
Variable naming conventions
1.
Variable name must be less than 255 characterslong.
2. Variable name cannot contain a space, period,
or a hyphen.
3. A variable must begin with a letter
4. It must be unique within the scope
8/8/2019 Unit II Programming Fundamentals
12/29
Strings
Can store multiple character data.
String length : no: of characters in string
Each character requires one byte for its storage.
8/8/2019 Unit II Programming Fundamentals
13/29
Strings can be :
Fixed length
Declaration: Dim s as string * 10
Variable length:
Declaration: Dim s as string
8/8/2019 Unit II Programming Fundamentals
14/29
Assigning values to variable
Dim I as integer
I= 24
Dim s as string
S= items selected
8/8/2019 Unit II Programming Fundamentals
15/29
8/8/2019 Unit II Programming Fundamentals
16/29
Default values for variables:
Data type Default value
Integer 0Long 0
Single 0
Double 0
String (Blank string)
8/8/2019 Unit II Programming Fundamentals
17/29
Data type Default value
Boolean False
Variant Empty
Date 0
Currency 0
8/8/2019 Unit II Programming Fundamentals
18/29
Literals
The values assigned to variables are called
literals. A literal whose data type is notspecified is considered to be a variant
type. The following guidelines must be
followed to explicitly provide data type of
a literal.
8/8/2019 Unit II Programming Fundamentals
19/29
String literals are enclosed in quotation
marks. E.g. Deepa
Date and time values must be enclosed in
#s E.g. #22-Jan-1999#, #2:15pm#
Boolean literals are : True or False
8/8/2019 Unit II Programming Fundamentals
20/29
Constants
If a variable in your program contains a value that neverchanges, you should consider storing the value as aconstant instead of as a variable. A constant is ameaningful name that takes the place of a number or textstring that doesnt change (such as p, a fixedmathematical quantity.)
Advantages of using constants include:
Making program code more readable
Saving memory Making program -wide changes easier to accomplish.
8/8/2019 Unit II Programming Fundamentals
21/29
How Constants Work
Constants operate a lot like variables, but you cant modifytheir values at run time. You declare constants with theConst keyword, as shown in the following example:
Const Pi = 3.14159265
This statement creates a constant called Pi that you can usein place of the value of p in the program code. To createa constant that's available to procedures throughout aprogram, use the Public keyword to create the constant
in a standard module. For example:
Public Const Pi = 3.14159265
8/8/2019 Unit II Programming Fundamentals
22/29
Operators in VB
Arithmetic : + Addition
- Subtraction* Multiplication
/ Division
\ IntegerDivision
Mod Modulus
^ Exponentiation
8/8/2019 Unit II Programming Fundamentals
23/29
E.g.
2 + 5 =7
51-3=482*5=10
5/2=2.5
5\2=25 mod 2=1
3^2=9
8/8/2019 Unit II Programming Fundamentals
24/29
ConcatenationOperator ( &)
Is used to concatenate two strings.
E.g S1=Geeta
S2=SahiName= S1 & & S2
Msgbox( Name)
The above code will display a message box with : Geeta Sahi
If + is used with strings then also it concatenates two strings.
8/8/2019 Unit II Programming Fundamentals
25/29
RelationalOperators / Comparison operators
= Equal to ( Returns True if both operands are equal otherwise
false)< >Not equal to ( returns true if its both operands are not
equal otherwise returns false)
> Greater than ( returns true if first operand is greater than the
second operand otherwise returns false)
> = Greater than equal to ( returns true if first operand isgreater than or equal to the second operand otherwise
returns false)
8/8/2019 Unit II Programming Fundamentals
26/29
< Less than ( returns true if first operand is less than the
second operand otherwise returns false)
8/8/2019 Unit II Programming Fundamentals
27/29
Conditional Expression Result
10 < > 20 True (10 is not equal to 20)
Score < 20 True if Score is less than 20;
otherwise, FalseScore = Label1.Caption True if the Caption property of the
Label1 object contains the same value
as the Score variable; otherwise, False
Text1.Text = Bill True if the word Bill is in the first text
box; otherwise, False
8/8/2019 Unit II Programming Fundamentals
28/29
Logical operators
LogicalOperator MeaningAnd If both conditional expressions are
True, then the result is True.
Or If either conditional expression is
True, then the result is True.
8/8/2019 Unit II Programming Fundamentals
29/29
Not If the conditional expression is False, then the
result is True. If the conditional expression is
True, then the result is False.
Xor If one and only one of the conditional expressions
is True, then the result is True. If both are True or
both are False, then the result is False.