Professional Documents
Culture Documents
Unit 2: Datatypes and Variable, Statements, Operators: 1.0 Intended Learning Outcomes
Unit 2: Datatypes and Variable, Statements, Operators: 1.0 Intended Learning Outcomes
C. M. D. Hamo-ay
1 | Event-Driven Programming 2
Let’s discuss the common or the very basic datatype used to program in visual
basic .net:
Boolean- This datatype is handles TRUE or FALSE value. If the datytype of a
variable is Boolean the value it may contain is either True or False only.
Short- This datatype handles 2 bytes of whole numbers
Integer- This datatype handles 4 bytes of whole numbers
Long- This datatype handles 8 bytes of whole numbers
Single- This datatype handles 4 bytes of the numbers with decimal points
Double- This datatype handles 8 bytes of the numbers with decimal points
Decimal- This datatype handles 16 bytes of the numbers with decimal points
String- This datatype handles set/group of character or “word” values
Note: Short, Integer, and Long belongs to the datatype category that holds whole
number they only differ in size same through with Single, Double, and Decimal
they belong to the category of datatypes that hold numbers with decimal point.
Example:
1. If the value of variable is 9.1. What is its datatype?
2. If the value of variable is “B”. Whats is its datatype
3. If the value of variable is 2,147,483,647. What is its datatype?
Answer:
1. Short since the number has a decimal point and its under 2 bytes
2. Char since the value is single letter only. It is not a string because string is for
group of letters/characters.
3. Integer since the value is a whole number and the
Variable is nothing but a name given to a storage area that our programs can
manipulate. Each variable in VB.Net has a specific type, which determines the
size and layout of the variable's memory; the range of values that can be stored
within that memory; and the set of operations that can be applied to the variable.
Illustration: Container of
Storage of data VARIABLE
The BASKET serves as our value
variable (container/storage) and the
APPLE is our value. Putting the apple
inside the basket is like putting value
in a variable.
Question:
C. M. D. Hamo-ay
1 | Event-Driven Programming 3
The EQUAL SIGN (=) is what we called assignment operator. It assigns the value
from the left side (apple) to the right side which is the variable (basket). So now
when asked. What is the value of Basket? The answer is apple.
If x=1 then the value of x is 1.
If y=3 then the value of y is 3.
Therefore the value of z in z=x + y is 4.
Rules of naming a variable.
It should not start with a number
It must not have space
It must not have special character
Example:
firstNumber, Num1, x, Last_name
3. Put the Label inside the form by click the form itself.
C. M. D. Hamo-ay
1 | Event-Driven Programming 4
4. Double click the open space inside the form except the Label object
5. Write the code from the example and click the Start button to run the
program
Dim is the keyword, x is our variable, and Integer is our datatype.
Meaning the value that x (our variable) may contain is a whole
In this case
we used string as datatype. A value of string or char datatype should
always have a double quote (“ ”).
Always remember
The value on your right is always the value on the left side
Lvalue is always the container
Rvalue is always the content
1.2.2 Statement
A Statement is a complete instruction in Visual Basic programs. It may
contain keywords, operators, variables, literal values, constants, and expressions.
C. M. D. Hamo-ay
1 | Event-Driven Programming 5
Declaration statements - these are the statements where you name a variable,
constant, or procedure, and can also specify a data type.
Example:
Dim Statement Declares and allocates storage space for one or more
variables.
Executable statements - these are the statements, which initiate actions. These
statements can call a method or function, loop or branch through blocks of
code or assign values or expression to a variable or constant. In the last case, it
is called an Assignment statement.
Example:
Decision Making Statement
Dim num1 as Integer
num1=6
if(num1>=5)
label1.text=”The value of num1 is greater than 5”
end if
1.2.3 Operators
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations.
Arithmetic Operators
C. M. D. Hamo-ay
1 | Event-Driven Programming 6
3. Put the Label inside the form by click the form itself.
4. Double click the open space inside the form except the Label object
C. M. D. Hamo-ay
1 | Event-Driven Programming 7
5. Write the code from the example and click the Start button to run the
program to see the output.
Note: The same process will be used on next examples but will just differ in codes.
Comparison Operators
C. M. D. Hamo-ay
1 | Event-Driven Programming 8
This are the operators that compare values and return true or false .
Following table shows all the comparison operators supported by VB.Net.
Assume variable A holds 10 and variable B holds 20, then:
Example for Comparison Operators. You don’t have to write the once
in green it MUST be read.
Logical Operators
C. M. D. Hamo-ay
1 | Event-Driven Programming 9
Truth Table
Boolean And Or Not
T T T T F
T F F T T
F T F T T
F F F F T
C. M. D. Hamo-ay
1 | Event-Driven Programming 1
Assignment Operator
Simple assignment operator, Assigns values from right side operands to
left side operand. C = A + B will assign value of A + B into C. += Add AND
assignment operator, It adds right operand to the left operand and assigns the
result to left operand.
C. M. D. Hamo-ay
1 | Event-Driven Programming 1
Exercise No. 1
Answer the following questions correctly.
1. What is the key word used to declare variable?
2. Left or Right. The variable is always placed on which side?
3. Which data type are used for the variable to contain numbers with decimal point?
4. What do we call the storage or container of data or value?
5. Choose a value variable x can contain if its variable is Boolean.
a.True b. 50 c. 5.56 d. Correct
6. if x=500, what is its datatype?
7. Which is not true about declaring a variable?
a. None of the choices
b. It must not have space
c. It must not have special character
d. Must not start with a number
8. Which is the proper declaration of variable?
a. Dim x As Integer
b. Dim Integer using x
c. Integer x
d. Use String in x
9. Declare the variable fnum with a datatype Date
10. What is the operator used to assign value to a variable?
11. What do we call the complete instruction that contain keyword, operators,
variables, etc.?
12. What do we call the statement that initiate action?
13. Dim statement is an example of what statement?
14. Decision making is what example of statement?
15. What is the similarities between the executable and declaration statement
16. What is the value of y, if y=30 mod 4
17. Which of the following does not belong to the group?
a. *= b. += c. == d. =
C. M. D. Hamo-ay
1 | Event-Driven Programming 1
1.3 References
Anjan’s VB.Net Tutorial for Beginners
Tutorials Point (I) Pvt. Ltd.- “VB.Net Programming Language Reference”
www.youtube.com- VB.Net for Beginners
1.4 Acknowledgment
The images, tables, figures and information contained in this module were
taken from the references cited above.
C. M. D. Hamo-ay