Lecture 4 - Variables, Constants, And Data Types (1)

  • Uploaded by: curlicue
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Lecture 4 - Variables, Constants, And Data Types (1) as PDF for free.

More details

  • Words: 1,352
  • Pages: 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 “

“

Mathematical operations “

“

Integers and Doubles (floating point numbers) 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. “

“

A variable thus binds the stored data to a name... “

“

i.e., A variable is a ‘data box’.

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; “

“

As is a keyword for specifying the data_type… “

“

variable_name is the name of the variable; 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 “ “ “

“

Note: variables can also be given initial values… “ “

This is called ‘initialization’. Example: Dim x As Integer = 6 “

“

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.

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

z

“

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… “

“

counting objects (cardinal)… z Example: “There were six customers.” ordering objects (ordinal)… z Example: ‘The one-hundredth customer will win…’

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: “ “ “

“

For example, assume x starts out as 10: “ “

“

First, get the value stored in variable x. Then, add 1 to this value. Lastly, store the result in variable x. 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 “ “

“

At run-time, the right_side is evaluated… “

“

“

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 : “

“

And then passed to the left_side.

For instance, as a result of the statement: z = 2 * 3 “

“

Where, ‘left_side’ is a variable… While ‘right_side’ is a mathematical expression.

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

“ “ “ “

“

Multiplication and floating-point division (*, /) Integer division (¥) Modulus arithmetic (Mod) Addition and subtraction (+, –)

Note that operations on “strings” come next (more, later): “ “

“

Such as the ‘-’ in ‘x = -6’

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

“

“Compute Consumption Tax” z

“

Consumption_tax = subtotal * tax_rate

“Compute the Total” z

“

subtotal = price * quantity

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 “

“

Common Data Types for mathematics “

“

“ “

“

Integers and Doubles

Mathematical Operations “

“

Declaring Constants

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.

Related Documents


More Documents from "Shailendra Mishra"