lesson 4 mathematical operators! october 6, 2009

24
Lesson 4 Mathematical Operators! October 6, 2009

Upload: shauna-wells

Post on 21-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson 4 Mathematical Operators! October 6, 2009

Lesson 4Mathematical Operators!

October 6, 2009

Page 2: Lesson 4 Mathematical Operators! October 6, 2009

Today’s Agenda

• Looking at Vocabulary Words• Working with Mathematical

Operators• Create a Simple Addition Program

Page 3: Lesson 4 Mathematical Operators! October 6, 2009

Performing Calculations in Visual

Basic

Operators

Are symbols that perform certain operations in Visual Basic Statements

Page 4: Lesson 4 Mathematical Operators! October 6, 2009

Mathematical Operators

Operator Description= Assignment+ Addition- Subtraction* Multiplication/ (Forward Slash) Division\ (Backward Slash) Integer

divisionMod Modulus

Page 5: Lesson 4 Mathematical Operators! October 6, 2009

Purpose of Label ControlsLabel Control

• Is used to place text on a form• Sometimes is used to identify a text

box• Sometimes is used to add a title• Sometimes is used to add a message• Cannot be changed by user• Also can be used to provide output

Page 6: Lesson 4 Mathematical Operators! October 6, 2009

Let’s create our example program!

1. Open Visual Basic.2. Open New Standard EXE project.3. In the properties window, give the new

form the name frmAddition and the caption Addition.

4. Insert a label and click the Caption property.

5. Key in your name for the label.6. Name your label lblMyName.7. Save the project with the form named

frmAddition and project name Addition.

Page 7: Lesson 4 Mathematical Operators! October 6, 2009
Page 8: Lesson 4 Mathematical Operators! October 6, 2009

1. Insert another label on the form.2. Position it in the center of the form.3. Change the caption of the label to the

number zero (0).4. Change the name of the new label to

lblAnswer.5. Insert a command button.6. Name the command button

cmdCalculate.7. Change the button’s caption to Calculate.8. Add code in code view window.

lblAnswer.Caption = 16 + 89. Close the code view window and run your

program.

Page 9: Lesson 4 Mathematical Operators! October 6, 2009
Page 10: Lesson 4 Mathematical Operators! October 6, 2009

What happen?

Who can explain?

Page 11: Lesson 4 Mathematical Operators! October 6, 2009

Working with Mathematical Operators

October 7, 2009

Page 12: Lesson 4 Mathematical Operators! October 6, 2009

Note!

• Hard-coded values– The term hard-coded refers to

information that is entered directly into the source code and cannot change while the program runs.

– Example: lblAnswer.Caption = 16 + 8

Page 13: Lesson 4 Mathematical Operators! October 6, 2009

Using Text Boxes and the Val Function

Text boxes• Are the fields placed on dialog boxes

and in other windows that allow the user to enter a value.

• The text property of a text box specifies what text will appear on the text box.

Page 14: Lesson 4 Mathematical Operators! October 6, 2009

Text –vs-Numeric DataText Boxes• Accept data from the user.• Comes in the form of text.• Includes letters, symbols, and numbers.

Numeric Data• Numbers in a text box must be converted to a

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

• The conversion used to convert to numbers is the Val Function!

Page 15: Lesson 4 Mathematical Operators! October 6, 2009

The Val FunctionTakes numbers that are in a text

format and returns numeric value that can be used in calculations.

Example:lblTotal.caption = Val(txtPrice.Text) +

Val(txtSalesTax.Text)

Page 16: Lesson 4 Mathematical Operators! October 6, 2009

Splitting Code Statements Among Lines

When splitting a line of code among two or more lines use a underscore (_)!

Example:lblTotal.caption = Val(txtPrice.Text)+_

Val(txtSalesTax.Text)

The underscore is called line-continuation character and it tells the compiler to skip to the next line and treat the text there as if it were part of the same line.

Page 17: Lesson 4 Mathematical Operators! October 6, 2009

Comments!

The apostrophe ( ‘ ) at the beginning of the code tells the compiler to ignore everything that follows.

Example:‘ Calculate total expenses.

Page 18: Lesson 4 Mathematical Operators! October 6, 2009

Fix Function• There are times when you are

interested in only whole numbers after a calucation is performed. Use the Fix Function!– Drops the fractional part of a number.– In other words, it removes everything to

the right of the decimal point which is called truncation.

– Returns the truncated whole number.

Page 19: Lesson 4 Mathematical Operators! October 6, 2009

Homework• Make sure you pick up the Homework

worksheet before you leave!

Page 20: Lesson 4 Mathematical Operators! October 6, 2009

Performing Integer Division using Mod!

October 8, 2009

Page 21: Lesson 4 Mathematical Operators! October 6, 2009

Open VB and design the following screen!

Page 22: Lesson 4 Mathematical Operators! October 6, 2009

Performing Integer Division and Using Mod!

1. Name your form frmDivision.2. Change the caption of your

form to Division.3. Name the first button to

cmdcalculate and second button cmdExit.

4. Name the first text box txtDivisor.

5. Name the second text box txtDividend.

6. Name the first label lblQuotient.

7. Name the third label lblRemainder.

Page 23: Lesson 4 Mathematical Operators! October 6, 2009

• Double click the calculate button.• Enter the following code:'Calculate QuotientlblQuotient.Caption=Val(txtDividend.Text) \ Val(txtDivisor.Text)

'Calculate RemainderlblRemainder.Caption = Val(txtDividend.Text) Mod Val(txtDivisor.Text)

• Run your program! What happens?

Page 24: Lesson 4 Mathematical Operators! October 6, 2009

Worksheet!• Create the program on your

worksheet!• Save to your flash drive.• Bring back tomorrow for coding!• Don’t forget to turn in your

homework before you leave!!!!!!!