Lesson 4 Mathematical Operators! October 6, 2009.

Post on 21-Jan-2016

219 views 0 download

Tags:

Transcript of Lesson 4 Mathematical Operators! October 6, 2009.

Lesson 4Mathematical Operators!

October 6, 2009

Today’s Agenda

• Looking at Vocabulary Words• Working with Mathematical

Operators• Create a Simple Addition Program

Performing Calculations in Visual

Basic

Operators

Are symbols that perform certain operations in Visual Basic Statements

Mathematical Operators

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

divisionMod Modulus

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

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.

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.

What happen?

Who can explain?

Working with Mathematical Operators

October 7, 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

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.

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!

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)

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.

Comments!

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

Example:‘ Calculate total expenses.

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.

Homework• Make sure you pick up the Homework

worksheet before you leave!

Performing Integer Division using Mod!

October 8, 2009

Open VB and design the following screen!

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.

• 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?

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!!!!!!!