0% found this document useful (0 votes)
6 views

Lecture 2 - Notes

Uploaded by

allwyn2ebenezer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 2 - Notes

Uploaded by

allwyn2ebenezer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SPECIAL RULES CONCERNING ARITHMETIC

EXPRESSIONS
• Special problems can arise if an arithmetic expression is not correctly written. Such problems
can be avoided by remembering the following rules.
1. Preceding a variable by a minus sign is equivalent to multiplication by −1.
• EXAMPLE 2.12
The arithmetic expression
−x ^ n
is equivalent to −(x ^ n) or −1 * (x ^ n), since exponentiation has precedence over
multiplication. Hence, if x and n are assigned values of 3 and 2, respectively, then −x ^ n will
yield a value of −9.
2. Except for the condition just described, operations cannot be implied
String Expressions
• Numerical operations cannot be performed on string constants or string variables. However,
strings and string variables can be concatenated (i.e., combined, one behind the other). In Visual
Basic we use either the ampersand (&) or the plus sign (+) as a string concatenation operator
(the ampersand is favored).
• EXAMPLE 2.15
• Suppose the string variables str1 and str2 have been assigned the following values:
• Str1 = "TEN"
• Str2 = "THOUSAND"
• Then the string expression
• Str1 & " " & str2 & " DOLLARS"
• will cause the three individual strings to be concatenated, resulting in the single string
• TEN THOUSAND DOLLARS
• Note that we could also have written the string expression as
• Str1 + " " + str2 + " DOLLARS"
Assigning Values to Variables
• The equal sign (=) is used to assign a numeric or string value to a variable. The general form
is
• Variable = Expression
• where the value of the expression on the right is assigned to the variable on the left. Note that
the expression can consist of a constant, a single variable, or a more complex expression.
• EXAMPLE 2.16
Shown below are several unrelated assignment statements.
X = 12.5
Cmax = X
Area = 3.141593 * Radius ^ 2
Label = "Name: "
Str = FirstStr + LastStr
• In each statement, the value of the expression on the right of the equal sign is assigned to the
variable on the left.
Displaying out -Library Functions
• The Print statement is used to display information within the currently active form, beginning in the upper left
corner.
• It is very convenient for displaying the results of very simple programs.
• It provides a way to view the results of small program segments during the development of a large project.
• The Print statement consists of the keyword Print, followed by a list of output items.
• Successive items must be separated either by commas or semicolons. Commas result in wide separation
between data items; semicolons result in less separation.
• A Visual Basic program contains the following statements.
Dim Student As String, X As Integer, C1 As Single, C2 As Single
Student = "Aaron"
X = 39
C1 = 7
C2 = 11
Print "Name:", Student, X, (C1 + C2) / 2
• The Print statement will generate the following line of output:
Name: Aaron 39 9
PROGRAM COMMENTS
• Comments provide a convenient means to document a program (i.e., to provide a
program heading, to identify important variables, to distinguish between major
logical segments of a program, to explain complicated logic, etc.). A comment
consists of a single apostrophe ('), followed by a textual message. Comments can be
inserted anywhere in a Visual Basic program. They have no effect on the program
execution.
• EXAMPLE 2.24
A Visual Basic program includes the following statements:
'Program to Calculate the Roots of a Quadratic Equation
.....
X1 = (−b + root) / (2 * a) 'calculate the first root
X2 = (−b − root) / (2 * a) 'calculate the second root
Print X1, X2

You might also like