variables, constants, and calculations chapter 3 copyright © 2011 by the mcgraw-hill companies,...

33
Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

Upload: jeffry-watkins

Post on 12-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

Variables, Constants, and Calculations

CHAPTER 3

Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.McGraw-Hill

Page 2: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

VARIABLES

If you were told:

X = 5 and Y = 10

and

answer = x + y

What would answer equal?

2

Answer = 15

Page 3: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

VARIABLES

If you were told:

r=10

and

areaCircle = 3.14 * r ^2

What would areaCircle equal?

3

Answer = 314

Variable – the value can vary.

Constant – this number cannot change while the pgm runs

Page 4: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

CIRCLE AREA IN VB CODE

Const PI as Double = 3.1415R ‘declare constants before variables

‘the R means the number is a double

Dim dblR as Double

Dim dblCircleArea as Double

dblR = 10

dlbCircleArea = PI * dblR ^ 2

4

Page 5: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

DECLARING VARIABLES

• Declared inside a procedure using a Dim statement

• Declared outside a procedure using Private

• Always declare the variable’s data type.

Page 6: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

DECLARATION STATEMENT EXAMPLES

Dim strCustomerName As String

Private intTotalSold As Integer

Dim sglTemperature As Single

Dim decPrice As Decimal

Private decPrice As Decimal

Page 7: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

DATA TYPES Data Type Use For Storage Size in bytes

Boolean True or False value 2

Byte 0 to 255, binary data 1

Clear Single Unicode character 2

Date 1/1/0001 through 12/31/9999 8

Decimal Decimal fractions, such as dollars/cents 16

Single Single precision floating-point numbers with six digits of accuracy

4

Double Double precision floating-point numbers with 14 digits of accuracy

8

Short Small integer in the range -32,768 to 32,767 2

Integer Whole numbers in the range -2,147,483,648 to +2,147,483,647 4

Long Larger whole numbers 8

String Alphanumeric data: letters, digits, and other characters Varies

Object Any type of data 4

Common ones will be circled

Page 8: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

NAMING VARIABLES AND CONSTANTS

• Use camelCase & prefixes for variables (intQuantity) and UPPERCASE for constants (GST, PI)

• Cannot use reserved words or keywords to which Basic has assigned a meaning, such as print, name, and value

• Meaningful names consisting of letters, digits, and underscores; must begin with a letter and no spaces or periods. Include class (data type) of variable (variable: countInteger constant: QUOTA_Integer)

Page 9: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

TYPE CASTING

9

Dim intHeight as integer

intHeight = 6.7

intHeight will now equal 7. It cannot hold a decimal value since it was declared type integer It will automatically be cast (rounded) to a whole number

Page 10: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

CONSTANTS

• Named

•User assigned name, data type, and value

•Use CONST keyword to declare.

• Intrinsic

•System defined within Visual Studio (Color.Red)

Const COMPANY_ADDRESS As String = "101 S. Main Street"Const SALES_TAX_RATE As Doublel = .08R

Page 11: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

ASSIGNING VALUES TO CONSTANTS

Decimal D Decimal – 850.50D

Double R Double – 52875.8R

Integer I Integer – 12345678I

Long L Long – 134257987L

Short S

Single F Single – 101.25F

• Declare the data type of numeric constants by appending a type-declaration character.

Page 12: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

SCOPE AND LIFETIME OF VARIABLES (1 OF 2)

• Visibility of a variable is its scope.

• Scope may be

•Namespace

•Module level

•Local

•Block level

• Lifetime of a variable is the period of time the variable exists.

Page 13: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

MODULE LEVEL VARIABLE DECLARATION EXAMPLE

Code module-level declarations in the Declaration section at the top of the code.

Page 14: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

CALCULATIONS

• Calculations can be performed with variables, constants, properties of certain objects, and numeric literals.

• Do not use strings in calculations.

• Values from Text property of Text Boxes

•Are strings, even if they contain numeric data

•Must be converted to a numeric data type before performing a calculation

Page 15: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

GETTING INPUT FROM A TEXT BOX

15

intHeight = txtHeight.Text

•The above line works fine assuming that the user will not input anything except an integer. If they do, the program will crash.

Page 16: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

TEXT CHANGED PROCEDURES

16

• If a calculation is performed and results outputted due to a textbox value, then if the value in that textbox is changed, the result label should be cleared, along with anything else on the interface that corresponds to the now non-existing textbox text property.

• Me.lblResult.Text=““

Page 17: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

GOT FOCUS

When a text box receives the focus, the text inside the box should become selected, awaiting the user’s new input.

txtNumber.SelStart =0txtNumber.SelLength=Len(txtNumber)

17

Page 18: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

MOD AND DIV

There is a video in Moodle explaining these two types of INTEGER division.

When you divide 2 integers (not real numbers), you get a remainder (mod) and a dividend (div \).

Examples:

11 / 4 = 2 remainder 3 therefore:

11 mod 4 = 3

11 \ 4 = 2

18

Page 19: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

DEBUGGING

• Now that you are performing calculations, you will need to learn how to debug your programs when the results are not what you expected them to be.

• There will be a video on moodle for an explanation on how to add breakpoints and step through your code.

19

Page 20: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

DEBUGGING WITH BREAKPOINTS

20

• There will be times when you program is not producing the desired output. These logic errors can be tracked down using breakpoints.

• Add the breakpoints by clicking at the left of the line of code where you want to start debugging, and a red dot will appear.

• When the program halts on this line, right click on the variable in question to add a watch

• Click step into and continue stepping through your program, watching the values as your program runs.

• Clicking the dot will remove the breakpoint

Page 21: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

UNEXPECTED INPUT FROM USER

If you ask the user for a number, and they input something that isn’t a number, your program will crash (halt execution).

Use the val function to convert non-numeric characters to a zero to keep the program from crashing.

IintHeight=val(Me.txtHeight.Text)

•The val function will convert the retrieved string to a 0 if it begins with anything except an integer

21

Page 22: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

ARITHMETIC OPERATIONS

Operator Operation+ Addition– Subtraction* Multiplication/ Division\ Integer Division

Mod Modulus – Remainder of division

^ Exponentiation

Page 23: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

ORDER OF OPERATIONS (BEDMAS)

• Hierarchy of operations, or order of precedence, in arithmetic expressions from highest to lowest

1. Any operation inside parentheses

2. Exponentiation

3. Multiplication and division

4. Integer division

5. Modulus

6. Addition and subtraction

Page 24: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

USING CALCULATIONS IN CODE• Perform calculations in assignment statements.

• What appears on right side of assignment operator is assigned to item on left side.

• Assignment operators — allows shorter versions of code =, +=, -=, *=, /=, \=, &=

‘Accumulate a total.

TotalSalesDecimal += salesDecimal

Page 25: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

ROUNDING NUMBERS

• Round decimal fractions

• Decimal.Round method returns a decimal result rounded to a specified number of decimal positions.

• Decimal.Round and Convert methods use technique called “rounding toward even.”

Decimal Value to Round

Number of Decimal Positions Results

1.455 2 1.46

1.445 2 1.44

1.5 0 2

2.5 0 2

Page 26: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

FORMATTING DATA FOR DISPLAY• To display numeric data in a label or text box, first convert value to

string.

• Use ToString method

• Format the data using formatting codes.

• Specifies use of dollar sign, percent sign, and commas

• Specifies number of digits that appear to right of decimal point

DisplayTextBox.Text = NumberInteger.ToString()

Page 27: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

USING FORMAT SPECIFIER CODES• "C" code

• Currency — String formatted with dollar sign, commas separating each group of 3 digits and 2 digits to the right of decimal point

• "N" code

• Number — String formatted with commas separating each group of 3 digits and 2 digits to the right of decimal point

• Can specify number of decimal positions

• Example: "C0" zero digits

Page 28: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

FORMAT SPECIFIER CODES

Format Specifier Codes Name

C or c Currency

F or f Fixed-point

N or n Number

D or d Digits

P or p Percent

Page 29: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

FORMAT SPECIFIER CODE EXAMPLES

Variable Value Code Output

totalDecimal 1125.6744 "C" $1,125.67

totalDecimal 1125.6744 "N0" 1,126

pinInteger 123 "D6" 000123

rateDecimal 0.075 "P" 7.50%

rateDecimal 0.075 "P3" 7.500%

rateDecimal 0.075 "P0" 8%

valueInteger -10 "C" ($10.00)

Page 30: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

DATE SPECIFIER CODE• Format DateTime values using format codes and ToString method.

Page 31: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

COUNTING AND ACCUMULATING SUMS

• Declare module-level variables, since local level variables reset to 0 each time the procedure is called.

• Summing Numbers

• Counting

• Calculating an Average

dblTotal += dblItemPrice

Private intCounter As IntegerintCounter += 1

dblAverage = dblTotal / intCounter

Page 32: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

APP DEPLOYMENT

32

• When your wonderful program is ready for the world, you can deploy it by publishing it to an URL, FTP location, or a space on your file server. There is a wizard to guide you

Page 33: Variables, Constants, and Calculations CHAPTER 3 Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill

3-

ASSIGNMENTS AND PRACTICE

33

• Case Study, LVP Book

• Hands on Programming Example, Ch 3, Bradley

• Pizza Cost, LVP

• Change, LVP

• DigitsofaNumber

• International

• Lennies Bail Bonds, 3.2 Bradley

• Recording Studio, 3.4 Bradley

• Car Rental, 3.7 Bradley

• Video Bonanza 3