Вы находитесь на странице: 1из 5

TYBSC (IT) Visual Basic

Chapter 3. Variables, Constants and Calculations


Q1. Name and give the purpose of five types of data available in Visual Basic. ( 8 mrks )
Ans:
The data type of variable and constant indicates what type of information will be the stored in
the allocated memory space: perhaps a name, a dollar amount, a date or a total. If none of the
data type is specified the default data type is variant. Following are some data types and there
use:
Integer:
Integer data type can store any whole numbers with in the range -32,768 to 32,767.
String:
String is also the most commonly used data type which can store Alphabetical data like
letters, digits, other characters.
Variant:
Variant data type is default data type which can store the any kind of data like Integer,
String, Object etc.
Currency:
Currency stores Decimal fractions, such as dollars and cents.
Date:
The date is also a data type which stores eight-character date.
Double:
Double-precision floating-point numbers with 14 digits of accuracy.
Long:
Larger whole number.
Q2. What dose declaring variables means? (6 mrks )
Ans:
1. Declaring variables means naming the variable and specifying their data type, that is
declaration statements establish your projects variable and constants give them names,
and specify the data type they will hold.
2. These declaration statements are not considered as executable; that is, they are not
executed in flow of instructions during program execution.
3. Although there are several ways of declaring the variables, the most commonly used
way is the Dim statements with data type. You can omit the data type, then default data
type becomes variant.
4. Here are some sample examples for declaration statement:
Dim strName as String
Declared the String variable
Dim intCounter
as Integer
Declared an Integer variable
Dim curDiscount as Currency
Declared a currency variable
Q3. What effect does the location of a Dim statement has on the variables is declares?
Ans:
1. There are several ways of declaring the variable, the most commonly used statement is
Dim statement to declare the variable. The syntax for declaring the variable using Dim is
as follows:
Dim Identifier [As Data type]
2. The reserved word Dim is really short for dimension, which means Size. When you
declare a variable, the amount of memory reserved depends on its data type.
3. In the Dim statement [as data type] is optional. If you omit that statement the default data
type is applied to the variable.

TYBSC (IT) Visual Basic

4. Example for using Dim statement:


Dim strName as String
Declared the String variable
Dim intCounter
as Integer
Declared an Integer variable
Dim curDiscount
as Currency Declared a currency variable
Dim vntChanging
Default variant data type
Q4. Explain difference between a constant and variable. ( 4 mrks )
Ans:
1. Variables:
a. The memory locations that hold data that can be changed during the project
execution are called variables.
b. In Visual Basic variables are declared with Dim or with out Dim.
c. Syntax for declaring variables is Dim identifier [As Data Type].
2. Constants:
d. The memory locations that hold data that cannot change during the execution are
called constants.
e. In Visual Basic constants are declared using Const statement.
f. Syntax for declaring constant is Const Identifier [As Data Type].
Q5. What is purpose of the Val function? ( 4 mrks )
Ans:
1. Visual Basic provides number of built in functions. The Val function is one of it. Val
function converts the text data into a numeric value.
The general form of Val function is as follows:
Val(Expression to Convert)
2. The expression you wish to convert can be the property of control, a variable, or a
constant.
3. A function can not stand by itself. It returns (produces) a value that can be used a part of
a statement, such as the assignment statements in the following examples.
4. When the Val function converts as argument to numeric, it begins at arguments left
most character. If that character is a number digit, decimal point, or sign, Val converts
the character to numeric and moves to the next character. As soon as a nonnumeric
character is found, the operation stops.
5. Example: Val(123.45) will return = 123.45 and Val(123A5) will return 123 only.
Q6. Explain order of precedence of operators for calculations?
Ans:
1. The order in which operator are performed determines the result. Consider the
expressions 3 + 4 * 2. Over here if addition is done first, the result is 14. However, if
multiplication is done first then result is 11.
2. The hierarchy of operations, or order of operations, or order of precedence, in arithmetic
expressions from highest to lowest is:
a. Exponentiation
b. Multiplication and Division
c. Addition and Subtraction
3. In the previous example, the multiplication is done before addition, and the result is 11.
to change the order of evolution, use parenthesis:
(3 + 4) * 2
will yield 14 as the result. One set of parenthesis can be used inside another set. In the
case, parenthesis are said to be nested.

TYBSC (IT) Visual Basic

Q7. What statements can be used to declare a variable?


Ans:
1. Declaring variables means naming the variable and specifying their data type, that is
declaration statements establish your projects variable and constants give them names,
and specify the data type they will hold.
2. These declaration statements are not considered as executable; that is, they are not
executed in flow of instructions during program execution.
3. Although there are several ways of declaring the variables, the most commonly used
way is the Dim statements with data type. You can omit the data type, then default data
type becomes variant.
4. The reserved word Dim is really short for dimension, which means Size. When you
declare a variable, the amount of memory reserved depends on its data type.
5. In the Dim statement [as data type] is optional. If you omit that statement the default data
type is applied to the variable.
6. Here are some sample examples for declaration statement:
Dim strName as String
Declared the String variable
Dim intCounter
as Integer
Declared an Integer variable
Dim curDiscount
as Currency Declared a currency variable
Q8. Explain how to make an interest rate stored in sngRate as a percentage with three
decimal digits.
Ans:
FormatPercent function is used to display numeric values as percent. This function multiplies
the argument by 100, adds percent sign, and rounds to two decimal places.
To make interest rate stored in sngRate as a percent with three digits write a code given below:
lblInterestRate.Caption = FormatPercent(sngRate, 3)
Q9. Should formatting functions be included for all captions and text display in a
program? Justify your answer.
Ans:
No formatting function should not be included for all caption and text display. Because
formatting functions formats data according to function that is called for example;
FormatPercent will convert data into percent and it will put % mark after it.
But not necessary that every time we display the numeric data on the caption or text, but we can
display plain string as well, which dont need formatting.
Q.9 Explain in detail about the various function used for formatting the data ? ( 8 mrks )
Ans.
Formatting data :
When you want to formate data for display , either on the printer or on the
screen , use the formatting function.To format means to control the way
the output will look.
Example :
12 is just a number but $12.00 conveys more meaning for dollar amount.
VB 6.0 introduces four new formatting function:
i. The FormatCurrency Function :
The FormatCurrency function returns a string of character
formated as dollar and cents by default , The currency value display a dollar sign and comma
and two postion to the right of the decimal point.

TYBSC (IT) Visual Basic

Syntax:
FormatCurrency ( NumericExpressionToFormat )
Example:
lblBalance.Caption = FormatCurrency ( Balance )
varible

value

curBalance

1275.675

curAmount

.5

Function

Output

FormatCurrency ( curBalance )

$1,275.68

FormatCurrency ( sngAmount )

$0.50

ii. The FormatNumber Function :


The FormatNumber function is similar to the FormatCurrency
function. The default format is determine by your computer aegional setting. It will generally
display commas and two digit to the right of the decimal point.
Syntax:
FormatNumber ( Expression-To-Format )
Example:
lblSum.Caption = FormatNumber ( Sum )
Varible

Value

Funtion

Output

mcurTotal

1125.67

FormatNumber ( mcurTotal , 0 )

1,126

curBalance

1234.567

FormatNumber ( curBalance , 2 )

1,234.57

iii.The FormatPercent Function :


To display numeric values as a percent ,use the FormatPercent( ) . This
function multiplies the argument by 100 , adds a percent sign, and rounds to the decimal places.
Syntax :
FormatPercent ( ExpressionToFormat [ , NamedFormat ] )
Example :
lblInterest.Caption = FormatPercent ( Rate )
In the complete form of the FormatPercent function. You can select the
number of digit to the right of the decimal point .
Varible

Value

Function

Output

curCorrect

.75

FormatPercent ( curCorrect )

75%

curCorrect

.75

FormatPercent ( curCorrect , 1 )

75.0%

iv. The FormatDateTime Function :


You can format an expression as a date or a time . The Expression may
be a string that holds a date or time value , a date type varible or a function that return a date.
If you omit the optional named format, the function returns the date using
vbGeneralDate.
Named Format
Example

Returns

TYBSC (IT) Visual Basic

vbGeneralDate
date. If it holds a time,

A date and/or time. If the expression holds a date, returns a short


2/28/99 6:01
returns a long time. If it holds both , returns both a short date and long

time.
vbLongDate
Sunday,February 28,1999
vbShortDate
2/28/99

Day of week , Month Day , Year

MM/DD/YY

vbLongTime
6:01:24 PM

HH:MM:SS AM/PM

vbShortTime
18:01

HH:MM( 24 hour clock )

Example:
lblStartDate.Caption = FormatDateTime ( StartDate , vbShortDate )
lblStartTime.Caption = FormatDateTime ( "1/1/07" ,vbLongDate )

Вам также может понравиться