Lecture 4 - Variables, Constants, And Data Types (1)
Transcript of Lecture 4 - Variables, Constants, And Data Types (1)
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
1/20
Lecture 4: Variables, Constants,
and Data Types
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
2/20
Outline
In this lecture, we will discuss:
Declaring and using Variables
Declaring Constants Common data types used in math
Integers and Doubles (floating point numbers)
Mathematical operations
Including operators and precedence
And Implement Two VB .NET Programs:
Simple Calculator Price Calculator
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
3/20
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
4/20
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
5/20
Constants
Assigning a variable fixes a memory location for storage. However, within limits a variable may take arbitrary values
depending on the data type (i.e., Integer, Double, etc).
The value of a constant, on the other hand, is fixed.
The VB syntax for declaring a constant is:
Const const_nameAs Data_Type= value
Const is a keyword declaring the constant;
Const_nameand value are the name and value of the constant;
Data_Typeis the type of constant.
Note: variables can also be given initial values
This is called initialization.
Example: Dim x As Integer = 6
Declares Integer variable x, and sets its starting value to 6.
Some examples: Const x As Integer = 10
Const PI As Double = 3.14159265
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
6/20
Data Types for Numbers
When working with numbers, we use two types of data: Integers (usually take the Integer type):
Example: 1, 2, 3,
Useful for discretemath: counting objects (cardinal)
Example: There were six customers.
ordering objects (ordinal)
Example: The one-hundredth customer will win
Floating point numbers (usually take the Double type):
Example: 1.50, 3.1415926, etc have a decimal point.
More useful for normalarithmetic.
Note: If you do not explicitly declare a variables data type
It is declared by the system as an Object, by default.
We will talk more about Integers and Floats, and other data
types, shortly First, lets look at some basic mathematical operations.
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
7/20
Mathematical Operators
The table below contains the operators available for basic mathoperations:
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
8/20
Program 4.1 - A Simple Calculator
Desired Functionality:
Make a simple program, to implement these operators
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
9/20
Simple Calculator (cont.)
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
10/20
Simple Calculator (cont.)
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
11/20
Math Statements
In our previous example, we saw a math statement (C = A + B)
Question: what does the statement, x = x + 1 do?
Thinking in terms of arithmetic, this is a nonsense statement.
Since = is defined as equality But x is never equal to x + 1!
However, if we instead think in VB, it makes perfect sense!
Remember= is the assignment operator.
Thus, x = x + 1 tells the computer to: First, get the value stored in variable x.
Then, add 1 to this value.
Lastly, store the result in variable x.
For example, assume x starts out as 10:
Dim x As Integer = 10
x = x + 1
During run-time, the right side is first evaluated to yield 11.
Then, this result (11) is passed to the left side (x).
So, the overall result is to set: x = 11.
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
12/20
Assignment Operators
Simple one-variable expressions, such as:
n = n + 1
Are really assignment operations, Which involve the simple updating of the variable
Short-hand operators exist for such operations:
Which combine both operators into a single assignment operation.
For instance, the statement,
n = n + 8 can be written as: n += 8
Either form assigns the value n + 8 to the variable n.
Short-hand operators exist for all 4 basic operations: +=, -=, *=, /=
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
13/20
Math Statements (cont.)
More generally, a math statement takes the form:
left_side = right_side
Where, left_side is a variable
While right_side is a mathematical expression.
At run-time, the right_side is evaluated
And then passed to the left_side.
For instance, as a result of the statement: z = 2 * 3
First, the right side is evaluated (yielding 6).
Then, the result is passed to z (setting z equal to 6).
What about a compound statement (sevaral math ops):
x = 3 * 2 + 1 ? If we perform the multiplication first, we get :
x = 6 + 1 = 7.
If we add first, we get :
x = 3 * 3 = 9.
Which is correct?
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
14/20
Operator Precedence
In VB, the order of evaluation of math operators is determined byprecedence.
For arithmetic, the order of evaluation is (first to last):
Exponentiation (^) Unary identity and negation (+, )
Such as the - in x = -6
Multiplication and floating-point division (*, /)
Integer division ()
Modulus arithmetic (Mod)
Addition and subtraction (+, )
Note that operations on strings come next (more, later): String concatenation (+)
String concatenation (&)
So, for our example:
x = 3 * 2 + 1
= 6 + 1 = 7.
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
15/20
Overriding Precedence
What if we want to do the addition first?
VBs default operation order can be over-ridden easily!
By simply adding parentheses.
In particular, operations enclosed by parenthesis are evaluated first
Examples:
Our example, stated as: z = 3 * (2 + 1) = 3 * 3 = 9
However, stated as: y = (3 * 2) + 1 = 6 + 1 = 7 Thus, parenthesis provide simple program control, during execution.
This also applies to nested parentheses
Parentheses inside of parentheses.
The most internal operations are performed first. Example: X = (((2 + 1) * 3) + ((7 + 6) 4)) * 5
= ((3 * 3) + (13 -4)) * 5
= (9 + 9) * 5
= 18 * 5= 90.
P P i C l l
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
16/20
Program 2 Price Calculator
Lets make a program that allows us to:1. Name a product;
2. Assign it a price and a desired number to buy;
3. Calculate the subtotal, consumption tax, and total.
Assume a consumption tax rate of 5%.
It is easier to think in terms of: Variables and Calculations
Our Variables:
Input Data: price (a Double) and quantity (an Integer) Output Data: subtotal and total (both are Double type)
We also have a Constant: the tax_rate (a Double)
Our Calculations:
Compute the Subtotal
subtotal = price * quantity
Compute Consumption Tax
Consumption_tax = subtotal * tax_rate
Compute the Total
Total = subtotal + consumption_tax
Our Algorithm= Read the Input+ Compute each+ Display Results
P 2 ( )
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
17/20
Program 2 (cont.)
So, in a more organized form, we have:
Const tax_rate As Double = 0.15
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
18/20
Program 2 (cont.)
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
19/20
Program 2 (cont.)
-
8/14/2019 Lecture 4 - Variables, Constants, And Data Types (1)
20/20
Conclusion
In this lecture, we have discussed:
Declaration and Use of Variables Declaring Constants
Common Data Types for mathematics Integers and Doubles
Mathematical Operations arithmetic operators
assignment operators precedence
Algorithm Design
And Implemented two Programs, using Visual Studio .NET:A. Simple Calculator
B. Price Calculator
With the remainder of the lecture, you should practice: Try creating the programs yourself.