Você está na página 1de 20

Lecture 4: Variables, Constants,

and Data Types


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
Variables
“ A variable is a location in memory,
“ Which has been specifically reserved for holding data.
“ i.e., A variable is a ‘data box’.
“ A variable thus binds the stored data to a name...
“ Which enables convenient access at a later time.
“ Variables can be assigned values, via ‘assignment’:
“ Such an assignment operation takes the form:
variable_name = value

“ Here, ‘=’ is the assignment operator (NOT the ‘equality’ operator)


“ Examples:
Using Variables
“ Prior to use, variables must first be declared…
“ This reserves (creates) a place in computer memory.
“ The VB syntax for a variable declaration:
Dim variable_name As data_type

“ Dim is a keyword for declaring a variable;


“ variable_name is the name of the variable;
“ As is a keyword for specifying the data_type…
“ the type of data the variable will hold (i.e., Integer)
“ Example of variable use: Adding 10 + 15…
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_name As Data_Type = value


“ Const is a keyword declaring the constant;
“ Const_name and value are the name and value of the constant;
“ Data_Type is 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
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 ‘discrete’ math:
z counting objects (cardinal)…
z Example: “There were six customers.”

z ordering objects (ordinal)…


z 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 ‘normal’ arithmetic.

“ Note: If you do not explicitly declare a variable’s 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, let’s look at some basic mathematical operations.
Mathematical Operators
“ The table below contains the operators available for basic math
operations:
Program 4.1 - A Simple Calculator
“ Desired Functionality:
“ Make a simple program, to implement these operators…
Simple Calculator (cont.)
Simple Calculator (cont.)
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:
z x = 11.
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:
“ +=, -=, *=, /=
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?
Operator Precedence
“ In VB, the order of evaluation of math operators is determined by
precedence.
“ For arithmetic, the order of evaluation is (first to last):
“ Exponentiation (^)

“ Unary identity and negation (+, –)

z 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.
Overriding Precedence
“ What if we want to do the addition first…?
“ VB’s 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.
Program 2 – Price Calculator
“ Let’s 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”
z subtotal = price * quantity
“ “Compute Consumption Tax”
z Consumption_tax = subtotal * tax_rate
“ “Compute the Total”
z Total = subtotal + consumption_tax
“ Our ‘Algorithm’ = ‘Read the Input’ + ‘Compute each’ + ‘Display Results’
Program 2 (cont.)
“ So, in a more organized form, we have:

Const tax_rate As Double = 0.15


Program 2 (cont.)
Program 2 (cont.)
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.

Você também pode gostar