You are on page 1of 5

Ma. Rowena M.

Valencia
A2A

A. Define the ff.

VARIABLES

In programming, a variable is a value that can change,


depending on conditions or on information passed to the program.
Typically, a program consists of instruction s that tell the computer
what to do and data that the program uses when it is running. The data
consists of constants or fixed values that never change and variable
values (which are usually initialized to "0" or some default value
because the actual values will be supplied by a program's user).
Usually, both constants and variables are defined as certain data type
s. Each data type prescribes and limits the form of the data. Examples
of data types include: an integer expressed as a decimal number, or a
string of text characters, usually limited in length.

CONSTANT

In programming, a constant is a value that never changes. The other


type of values that programs use is variables, symbols that can
represent different values throughout the course of a program.

A constant can be
- a number, like 25 or 3.6
- a character, like a or $
- a character string, like "this is a string"

Constants are also used in spreadsheet applications to place non-


changing values in cells. In contrast, a spreadsheet formula can
produce a different value each time the spreadsheet is opened or
changed.

DATA TYPES
A data type in a programming language is a set of data with values
having predefined characteristics. Examples of data types are: integer,
floating point unit number, character, string, and pointer. Usually, a
limited number of such data types come built into a language. The
language usually specifies the range of values for a given data type,
how the values are processed by the computer, and how they are
stored.

B. Enumerate the different data types in VB


The two fundamental data types in Visual Basic are value types and
reference types. Primitive types (except strings), enumerations, and
structures are value types. Classes, strings, standard modules,
interfaces, arrays, and delegates are reference types. Every type has a
default value. Reference types are created on the Heap. The lifetime of
the reference type is managed by the .NET framework. The default
value for reference types is null reference. Assignment to a variable of
a reference type creates a copy of the reference rather than a copy of
the referenced value. Value types are created on the stack. The
lifetime is determined by the lifetime of the variable.

Boolean values
There is a duality built in our world. There is a Heaven and Earth, water
and fire, jing and jang, man and woman, love and hatred. In Visual
Basic the Boolean data type is a primitive data type having one of two
values: True or False. This is a fundamental data type. Very common in
computer programs.

Strings and chars


String is a data type representing textual data in computer programs.
A string in Visual Basic is a sequence of Unicode characters. A Char is a
single Unicode character. Strings are enclosed by single or double
quotes.

Type casting
We often work with multiple data types at once. Converting one data
type to another one is a common job in programming. Type conversion
or typecasting refers to changing an entity of one data type into
another. There are two types of conversion. Implicit and explicit.
Implicit type conversion, also known as coercion, is an automatic type
conversion by the compiler.

Date
A Date is value type, which contain date values, time values, or date
and time values.

Arrays
Array is a complex data type which handles a collection of elements.
Each of the elements can be accessed by an index. All the elements of
an array must be of the same data type.

Enumerations
Enumerated type (also called enumeration or enum) is a data type
consisting of a set of named values. A variable that has been declared
as having an enumerated type can be assigned any of the
enumerators as a value. Enumerations make the code more readable.
Floating point numbers
Floating point numbers represent real numbers in computing. Real
numbers measure continuous quantities, like weight, height, or speed.
In Visual Basic we have three important floating point types: Single,
Double, and Decimal.

Integers
Integers are a subset of the real numbers. They are written without a
fraction or a decimal component. Integers fall within a set Z = {..., -2,
-1, 0, 1, 2, ...} Integers are infinite.In computer languages, integers are
primitive data types. Computers can practically work only with a
subset of integer values, because computers have finite capacity.
Integers are used to count discrete entities. We can have 3, 4, 6
humans, but we cannot have 3.33 humans. We can have 3.33
kilograms.

C. Write the syntax for declaring variables and constants in VB

Declaring Visual Basic Variables

Variables are declared using the Visual Basic Dim keyword. The syntax for a
simple declaration of a variable is as follows:
Dim variableName As variableType

In the above outline, Dim is the keyword which indicates to Visual Basic that a
variable is being declared. variableName is the name assigned to the variable.
Try to use a descriptive variable name and prefix the name with something which
indicates the variable type. For example, when declaring a String variable prefix
the name with str (e.g. strFirstName). The As keyword precedes the declaration
of the variable type (String, Date, Integer etc). For a complete list of Visual Basic
variable types see Understanding Visual Basic Variable & Constant Types.

To declare an Integer value named intInterestRate for example:


DimintInterestRateAsInteger

It is also possible to declare multiple variables on the same line by separating


each variable with a comma. When the variables are of the same type, the type
declaration only needs to be made once at the end of the declaration:
DimintInterestRate,intExchangeRateAsInteger

In the case where the variables are of different types the type of variable must be
declared at the end of each group of the same type. For example:

Dim strCustomerName As String, intInterestRate,


intExchangeRateAsInteger

Initializing Visual Basic Variables


Visual Basic variables may be initialized either during the declaration, or after the
declaration. Unless there is a good reason to do otherwise, it is recommended
that variables be initialized during the declaration.
Initialization is performed using the Visual Basic assignment operator (=). To
initialize a single variable when it is declared:
D
imintInterestRateAsInteger=5

When declaring multiple variables each variable may be initialized in the


declaration line:

DimstrCustomerNameAsString="Fred",intInterestRate=5
AsInteger,intExchangeRateAsInteger=10

Assigning New Values to Visual Basic Variables


Once a variable has been declared, a new value can be assigned to the variable
at any time using the variable name and the assignment (=) operator. In the
following sample Visual Basic code, a variable is declared and initialized to 10. It
is then re-assigned the value of 20:

DimintInterestRateAsInteger=10

intInterestRate=20

Referencing Variable Values


Accessing the value of a variable in Visual Basic code is as simple as typing the
variable name. For example, the following code example adds 20 to the value of
intInterestRate and assigns the result to the intNewRate variable:
DimintInterestRateAsInteger=10
DimintNewRateAsInteger=0
intNewRate=intInterestRate+20

Declaring and Referencing Visual Basic Constants


Constants, once declared and initialized cannot be changed (hence the name
constant' and are declared using the Visual Basic Const keyword. The syntax for
declaring variables is as follows:
Const constName As datatype = value

For example, to declare a String constant named companyName with a value of


Techotopia the declaration would read as follows:
ConstcompanyNameAsString="Techotopia"

A Visual Basic constant is referenced using the name defined when the constant
was declared. For example, the following Visual Basic code sets the Text
property of a Label control to the string value contained in the companyName
constant:
ConstcompanyNameAsString="Techotopia"

companyLabel.Text=companyName

You might also like