Lesson 4 PowerPoint

37
Microsoft Visual Basic 2005 BASICS Lesson 4 Mathematical Operators

Transcript of Lesson 4 PowerPoint

Page 1: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS

Lesson 4

Mathematical Operators

Page 2: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 2

Objectives

Describe the purpose of operators and how calculations are performed in Visual Basic.

Create label controls. Use the addition and assignment

operators.

Page 3: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 3

Objectives (cont.)

Use text boxes to get data from the user and use the Val function to extract a numeric value from a text box.

Split code statements among the lines in the code window.

Use the subtraction operator. Use unary minus.

Page 4: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 4

Objectives (cont.)

Use the multiplication and division operators with the Fix function to remove the fractional portion of numbers.

Perform integer division and use the modulus operator.

Page 5: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 5

Performing Calculations in Visual Basic

Visual Basic allows you to use mathematical equations in your programs.

Operators Symbols that perform specific operations in

Visual Basic statements

Page 6: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 6

Performing Calculations in Visual Basic (cont.)

Page 7: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 7

Creating Label Controls

Label control Used to place text in a form

Sometimes a label is used to identify a text box. May also be used to add a title or message

to a form

Page 8: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 8

Creating Label Controls (cont.)

Labels can also be used to provide output. To provide output, you write code for the

desired calculation.

Page 9: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 9

Using the Addition and Assignment Operators

The addition operator (+) and the assignment operator (=) Perform just as you would expect

The term “hard-coded” Information entered directly into the code Cannot change while the program runs

Literals Values keyed directly into source code

Page 10: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 10

Using the Addition and Assignment Operators (cont.)

Page 11: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 11

Using Text Boxes and the Val Function

Text boxes Fields placed on dialog boxes and in other

windows Allow the user to enter a value

The Text property of a text box Specifies what text will appear on the text

box

Page 12: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 12

Using Text Boxes and the Val Function (cont.)

Page 13: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 13

How Text Differs from Numeric Data

Text Can include letters, symbols, and numbers Treated differently than strictly numeric

information Numbers in a text box must be converted

to a true numeric value before they can be used in a calculation.

Page 14: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 14

Using the Val Function

Val function Takes numbers that are in text format Returns a numeric value that can be used

in calculations Syntax example

lblTotal.Text = Val(txtPrice.Text) + Val(txtSalesTax.Text)

Page 15: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 15

Splitting Code Statements among Lines

Visual Basic provides a way to split a line of code among two or more lines.

Line-continuation character Within a line of code, you can key the

underscore. Tells the compiler to skip the next line Treats the text there as if it were a part of

the same line

Page 16: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 16

Splitting Code Statements among Lines (cont.)

Page 17: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 17

Splitting Code Statements among Lines (cont.)

Page 18: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 18

Splitting Code Statements among Lines (cont.)

Page 19: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 19

Using the Subtraction Operator

The subtraction operator Subtracts the value to the right of the

operator from the value to the left of the operator

Page 20: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 20

Using the Subtraction Operator (cont.)

Page 21: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 21

Using the Subtraction Operator (cont.)

Page 22: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 22

Using Unary Minus

Unary minus operator Performs negation

Making a positive value negative Or making a negative value positive

The addition operator can be used as a unary plus.

Page 23: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 23

Using Unary Minus (cont.)

Page 24: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 24

Using Fix

Truncation Drops the fractional part of a number

Fix function Returns a truncated whole number

Page 25: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 25

Using Fix (cont.)

Page 26: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 26

Performing Integer Division and Using the Modulus Operator

Integers are whole numbers. Visual Basic provides two special

operations Integer division Modulus

Page 27: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 27

Performing Integer Division

Integer division Returns only the whole number portion of

the division of integers Integer division is performed using the

backslash (\).

Page 28: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 28

Performing Integer Division (cont.)

Page 29: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 29

Performing Integer Division (cont.)

Page 30: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 30

Using the Modulus Operator

Modulus returns the remainder of integer division.

The modulus operator is (Mod).

Page 31: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 31

Summary

Visual Basic allows you to use mathematical equations in your programs.

Operators are symbols that perform specific operations in Visual Basic statements.

The addition operator (+) adds values.

Page 32: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 32

Summary (cont.)

The assignment operator (=) assigns the result of the expression on the right of the operator to the item to the left of the operator.

The subtraction operator (–) subtracts the value to the right of the operator from the value to the left of the operator. It can be used to perform negation and is then called the unary minus.

Page 33: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 33

Summary (cont.)

Values keyed directly into Visual Basic code are called hard-coded values or literals.

Text boxes are the fields placed on dialog boxes and in other windows that allow the user to enter a value.

Page 34: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 34

Summary (cont.)

The numbers in a text box are considered to be text characters. To use the numbers as actual values in a calculation, the Val function must be used to convert the numeric text to a numeric value.

Page 35: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 35

Summary (cont.)

When a line of code is long, you can split the code into two lines in the Code window by keying an underscore at the end of the line and continuing the statement on the next line. The underscore is called the line-continuation character.

Page 36: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 36

Summary (cont.)

Placing an apostrophe in code allows you to enter text (called a comment) into the code. Everything from the apostrophe to the end of the line will be ignored.

Multiplication is represented by an asterisk (*). Division is represented by a forward slash (/).

Page 37: Lesson 4 PowerPoint

Microsoft Visual Basic 2005 BASICS 37

Summary (cont.)

The Fix function removes the fractional portion of a number and performs an operation called truncation.

Integer division is represented by a backward slash (\) and returns only the whole number portion of the division of integers.

The modulus operator (Mod) returns the remainder of integer division.