0% found this document useful (0 votes)
32 views9 pages

Self - Learning Module - 3 - Q4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views9 pages

Self - Learning Module - 3 - Q4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Computer science

03

Working with Language Elements

Understand the different elements used


in programming.

Specific Objectives
1. Explain variable and identify the rules in naming a variable.
2. Identify the different data types.
3. Understand and use properly the different types of operators.

Materials
 Microsoft Word
 Computer

Lesson
Working with Language Elements

The Visual Basic program statement is a line of code in a Visual


Basic programming. It is any combination of keywords, properties and
functions, operators and symbols that work together to create a valid
instruction for the Visual Basic compiler.

 a simple keyword – BEEP


 a group of elements

1
object name
assignment operator

Label1.Caption = Time

property name

function

To process the data in the program, there is a need to follow the


rules in construction which is called the statement syntax.

Using Variables in a Program

Variables can have the same value or change values several times
as needed. There are rules to follow in naming variables.

 Start each variable name with a letter. It must be lesser than 256
characters long and does not contain a period.
 Use descriptive names.
Example: SalesTaxRate instead of Tax
 Use a combination of uppercase and lowercase characters.
Capitalize the first letter of each word, if needed.
 Never use Visual Basic keywords, objects or properties as variable
name such as Caption, Label…
 Start each variable name with a two or three character
abbreviation. Ex. sName (string data)
 The name must be unique within the variable’s scope.

Variable Naming Prefix

Variable Type Prefix Example


String s sLastname
Integer n nAge
Long Integer l lRate
Single (Floating-point) f fAverageGrade
Double d dRatio
Currency c cPayrate
Boolean b bTaxable

To change the value of a variable


1. Create two command buttons on a form.
2. Name the two CommandButtons with Show and Quit,
respectively.
3. Click Label button and create two labels.
4. Double-click the Show button.
5. Type the following code found below in the Code Window.

2
6. Run the program. Use the Start button or press F5.
7. Click the Show Button.
8. Click the Exit Button.

Specific Data Types

Variant variables can store all of data types and change formats
automatically. However, variant variables take too much memory space
unlike the integer values. They are easy to declare and use. They assist in
writing codes faster and more concise.

Variable storage size is measured in bytes, the amount of space


required to store 8 bits.

DATA TYPE SIZE RANGE EXAMPLE

Integer 2 bytes -32, 768 to 32, 767 Dim Cat%


Cat% = 24
Long Integer 4 bytes -2, 147, 483, 648 to 2, 148, Dim Loan&
483, 648 Loan&=2,300,000
Single 4 bytes -3.402823E38 to Dim Price!
Precision 3.402823E38 Price!=899.99
floating-point

Double 8 bytes -1.7976931486232D308 to to Dim Pi#


Precision 1.7976931486232D308 Pi#=3.1415926535
floating-point

Currency 8 bytes -922337203685477.5808 to Dim Debt@


to Debt@=7600300.50
922337203685477.5808
String 1 byte per 0 through 65, 535 Dim Dog$
character Dog$ = “Brownie”
Boolean 2 bytes True or False Dim Flag As Boolean

3
Date 8 bytes Jan.1, 1000 through Dec. Dim Birthday As
31, 9999 Date
Birthday = #3-1-63#
Variant 16 bytes All data type ranges Dim Total

Constants

Constant is a meaningful name that takes the place of a number or


text string that does not change. Variable that contains a value that never
changes such as pi, a fixed mathematical entity, should be stored as
constant instead of a variable. A constant is useful because it makes the
program readable, saves memory and makes global changes easier to
accomplish. It operates like a variable but its values cannot be changed
during runtime. It is declared with the Const keyword.

Const Pi = 3.14159265

To make a constant available to all the objects and events


procedures in your form, place the statement in the Declaration section
of your form (the top line in the code window).

To make the constant available to all the forms and modules in a


program, create the constant in a standard module with the Public
keyword in front of it.

Public Const Pi = 3.14159265

To use a constant in an event procedure


1. Make your form like the one on the right.
2. Double-click the Show button on the form.
The CommandButton1_Click event
procedure appears in the code window.
3. Type the following code:

4. Click the Start button to run the program.

Working with Operators

A formula is a statement that combines numbers, variables,


operators, and keywords to create a new value. Visual Basic contains
different language elements for use in formulas.

4
It provides the following mathematical operators which link the
parts of a formula.

Arithmetic Operators
Operators Description Example Result
+ Add 5+5 10
- Subtract 10 – 5 5
/ Divide 25 / 5 5
\ Integer Division 20 \ 3 6
* Multiply 5*4 20
^ Exponent (power 3^3 27
of)
Mod Remainder of 20 Mod 6 2
division
& String “George”&” “George Bush”
concatenation “&”Bush”

Visual Basic allows working with mixed operators as in a formula,


as long as each numeric variable and expression is separated from
another by one operator.

Formula: Answer = 10 + 15 * 2 / 4 ^ 2

The formula solves several values and assigns the result to a


variable named Answer. Visual Basic processes this formula with the use
of specific order of precedence for mathematical operations. The list of
rules tells Visual Basic which operators to use first when evaluating an
expression that contains more than one operator. It is important to keep
this in mind when you are building mathematical formulas. Operators are
evaluated from left to right as they appear in an expression.

Visual Basic includes advanced operators: integer division (\).


Remainder division (Mod), exponentiation (^) and string concatenation
(&). They are useful in mathematical formula and text processing
applications.

Try This!!!

Form =

Write the Code:

5
Solve:
No. 1 No. 2 Operation Result
11 2 Integer Division
13 4 Remainder
9 3 Exponentiation
8 5 Concatenation

OPERATOR(S) ORDER OF PRECEDENCE


() Values within parentheses are always evaluated first
^ Exponentiation (raising a number to a power) is
second
- Negation (Creating a negative number) is third
*/ Multiplication and division are fourth
\ Integer division is fifth
Mod Remainder division is sixth
+- Addition and subtraction are last.

To solve the expression: Total = 10 + 15 * 2 / 4 ^ 2


Total = 10 + 15 * 2 / 16
Total = 10 + 30 / 16
Total = 10 + 1.875
Total = 11. 875

Using Parentheses in a Formula

To clarify the order of precedence, use one or more pairs of


parentheses in a formula. Visual Basic can compute the formula:

Number = ( 17 – 5 * 2 ) * 4 ^ 2

The value within the parentheses, which is 7, is computed before


doing the exponentiation, which is 16, and then multiplication. The result
is 112. You can refine by placing another parenthesis to calculate the
difference in the inner set of parentheses.

Number = ( ( 17 – 5 ) * 2 ) * 4 ^ 2

The result is different. The value in the inner parentheses is 12. The
multiplication in the inner parentheses is 24. The exponentiation is 16. The
multiplication of the two numbers is 384. Parentheses can change the
result of a mathematical operation.

The operator order of precedence is always important. The table


shows which operator is used first when evaluating an expression that
contains more than one operator. The table shows the operators from first
to last in the order in which they will be evaluated. The operators on the

6
same level are evaluated from left to right as they are shown in an
expression.

OPERATOR(S) ORDER OF PRECEDENCE


() 1. Values within parentheses
^ 2. Exponentiation (raising a number to a power)
- 3. Negation
*/ 4. Multiplication and division
\ 5. Integer Division
Mod 6. Remainder division is sixth
+- 7. Addition and subtraction

Using Conditional Expressions

A conditional expression is a part of a complete program statement


that asks True-or-False question about property, a variable, or another
data in the program code. It is one of the most useful tools for processing
information in an event procedure. Suppose the conditional expression is
Money < 200, Visual Basic evaluates to True if Money variable contains a
value that is less than 200, and it evaluates to False if Money contains a
value that is greater than or equal to 200.

COMPARISON MEANING
OPERATOR
= Equal to
<> Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Expressions that can be evaluated as True or False are also known


as Boolean expressions, and the True or False result can be assigned to a
Boolean variable or property. You can assign Boolean values to certain
object properties, variant variables or Boolean variables that have been
created by using the Dim statement and as the As Boolean keywords.

Example:
CONDITIONAL EXPRESSION RESULT
20 < > 40 True (20 is not equal to 40)
Total < 40 True if Total is less than 40; otherwise
False
Total = Label1.Caption True if the Caption property of the Total 1
object contains the same value as the
Total variable, otherwise False

7
Text1.Text = “Jose” True if the word Jose is in the first
textbox; otherwise, False

Using Logical Operators in Conditional Expressions

Logical operators allow you to add tests to the expressions. Visual


Basic allows you to test more than one conditional expression in If..Then
and ElseIf clauses if you want to include more than one item criterion in
the decision structure. The extra conditions are linked together by
applying one or more of the logical operators.

LOGICAL OPERATOR MEANING


And If both conditional expressions are True, then
the answer is True.
Or If either conditional expression is True, then the
answer is True.
Not If the condition expression is False, then the
answer is True. If the conditional expression is
True, then the result is False.
Xor If one and only one of the conditional
expression is True, then the answer is True. If
both are True or both are False, then the
answer is False.

Example:

Fruit = “Papaya” and Cost < 30 True (both conditions are True)
Fruit = “Bananas” or Cost < 40 True (one condition is True)
Not Cost < 20 True (condition is False)
Fruit = “Papaya” Xor Cost < 30 False (both conditions are True)

When your program evaluates a complex expression that mixes


different operator types, it evaluates mathematical operators first,
comparison operators second and logical the last.

EXERCISES

BASIC
A. Identify the following operators.
_____________ 1. + _____________ 5. ^
_____________ 2. - _____________ 6. /
_____________ 3. * _____________ 7. Mod
_____________ 4. \ _____________ 8. &

8
B. Write the order of precedence in the formula. Write 1 to 5.

Answer = ( ( 15 + 16 ) * 8 – 1 ) / 5 * 4

C. Answer briefly.
1. Define the following words:
a. formula c. operators
b. expression d. variable
2. What can parentheses do to the order of precedence of an
expression?
3. What is a part of a program statement that asks True or False
question about property, a variable or another data?
4. Enumerate the different operators used by:
a. comparison operator
b. logical operator

Closure
A. Please check the box if you can do it already.

• I can identify the different elements in visual basic;


• I can manipulate and use the different elements properly.

B. If you have questions in mind, please use the space below to address it.
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________

References
Amoto Jr., Tito B. Visual Basic 6.0. Quezon City, Book Craft Publishing Co., Inc.,

2001.

https://www.freetutes.com/learn-vb6/lesson2.html

https://www.tutlane.com/tutorial/visual-basic/vb-data-types

You might also like