Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

1 | Event-Driven Programming 1

UNIT 2: DATATYPES AND VARIABLE, STATEMENTS,


OPERATORS
 

1.0 Intended Learning Outcomes


At the end of the unit, you should be able to:
a. Define data types based on how it is used in visual basic .net;
b. Observe how data type works;
c. Make use of the different kinds of data types;
d. Apply the gained knowledge in data types in variable declaration;
e. Acquire the knowledge of declaring a variable;
f. Apply variable declaration into a program;
g. Differentiate the kind of statements in Visual Basic.Net;
h. Enumerate the arithmetical operations used in computer programming;
i. Make use comparison operators and logical operators based on its
function.
 
1.1 Introduction
 In this chapter datatypes and variables will be discussed thoroughly. Students will
learn and understand how variable works and how variable is being used to the
program. Grasping the concept of both variable and datatype will help the students
write a statement correctly and use the operators in any manners.

1.2 Topics/Discussion (with Assessment/Activities)

1.2.1 Datatypes and Variables


Datatype refers to an extensive system used for declaring variables or
functions of different types. The type of a variable determines how much space it
occupies in storage and how the bit pattern stored is interpreted.
In other resources it is define as an identifier of what kind of value a
variable may contain.
Boolean Single Byte String
Char UInteger Date ULong
Decimal UserDefined Double UShort
Integer Long
This table shows the different datypes used in visual basic .net

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

How can we put the apple inside the basket?


Or technically speaking.
How can we put a value inside the variable?
Answer:
Basket = Apple

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

Variable declaration is the process of introducing the variable and its


datatype to the program. It is a must to declare it, or else the program will not
recognize the variable. The Dim statement is used for variable declaration and
storage allocation for one or more variables. The Dim statement is used at
module, class, structure, procedure, or block level.

Follow the steps below in making the first example.


1. Create a visual basic file
2. Under toolbox select Label

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

number. In the example the value of x is 5.

Example 2 of declaring a variable:

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.

Statement could be categorize as:

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

Note: Both declaration and executable statement must be complete for it to be


considered as statement or else you will encounter syntax error.

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

Arithmetic operators is used in basic mathematical computations. Following


table shows all the arithmetic operators supported by VB.Net. Assume variable A
holds 2 and variable B holds 7, then:

For the first example follow the steps below:

1. Create a visual basic file


2. Under toolbox select Label

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

A logical operator is a symbol or word used to connect two or more


expressions such that the value of the compound expression produced
depends only on that of the original expressions and on the meaning of the
operator. Common logical operators include AND, OR, and NOT. Following
table shows all the logical operators supported by VB.Net. Assume variable
A holds Boolean value True and variable B holds Boolean value False, then:

Operation Description Example


And It is the logical as well as bitwise (A And B) is False.
AND operator. If both the
operands are true, then condition
becomes true. This operator does
not perform short-circuiting, i.e.,
it evaluates both the expressions.
Or It is the logical as well as bitwise Not(A And B) is True.
OR operator. If any of the two
operands is true, then condition
becomes true. This operator does
not perform short-circuiting, i.e.,
it evaluates both the expressions.
Not It is the logical as well as bitwise Not(A And B) is True.
NOT operator. Use to reverses
the logical state of its operand. If
a condition is true, then Logical
NOT operator will make false.

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

Example for Logical Operators:

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

Example for Assignment Operators

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

18. If a=8 and b=2. What is the value of a when a/=b?


19. if num1=7 and num2=-8. Which of the following is false statement?
a. num2<=num1
b. num1<>num2
c. num1>=num2
d. num1=num2
20. Which of the following is a comparison operator?
a. > b. + c. and d. +=
21. Which operator is used to get the remainder of the divided number?
22. Which is not true in OR truth table?
a. True and True is False
b. False and True is True
c. True and False is True
d. False and False is False
23. if x=5,y=7 and z=10. What is the value of x when a=y+x?
24. What is the logical operator that becomes true, only if both conditions are true?
a. And b. Or c. Not d. True

25. What type of operators are used in decision making?


a. Arithmetic and Comparison
b. Comparison and Logical
c. Arithmetic and Assignment
d. Logical and Arithmetic

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

You might also like