6.01 apply operators and boolean expressions

20
COMPUTER PROGRAMMING I SUMMER 2011 6.01 Apply operators and Boolean expressions

Upload: thao

Post on 07-Feb-2016

37 views

Category:

Documents


0 download

DESCRIPTION

6.01 Apply operators and Boolean expressions. Computer Programming I Summer 2011. Objective/Essential Standard. Essential Standard: 6.00- Apply Conditional Logic Indicator: 6.01 Apply operators and Boolean expressions. (3%). Operators. What are operators? Welcome back to Math class! - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 6.01  Apply operators and Boolean expressions

COMPUTER PROGRAMMING ISUMMER 2011

6.01 Apply operators and Boolean expressions

Page 2: 6.01  Apply operators and Boolean expressions

Objective/Essential Standard

Essential Standard: 6.00- Apply Conditional Logic

Indicator: 6.01 Apply operators and Boolean expressions. (3%)

Page 3: 6.01  Apply operators and Boolean expressions

Operators

What are operators?

Welcome back to Math class! Operators are the symbols we use for performing

math. You have used them since early in life. There are several types of operators. Basic Math operators, Boolean/Comparison Operators. We will use all of these in programing. For now let’s look at the math operators.

Page 4: 6.01  Apply operators and Boolean expressions

Basic Math Operators

These you are familiar with:+ (add),- (subtract)/ (divide)* (multiply)^ (Exponents)

New Math operators include:\ integer divide - returns the whole of the

division. Mod (Modulus division) - returns the remainder

of the division.

Page 5: 6.01  Apply operators and Boolean expressions

Samples of Math with various Operators

Addition + intNumber = num1 + num2

Subtraction – intNumber = num1 - 50

Multiplication * intNumber = num1 * num2

Division / intNumber = 51 / num2

Integer Division \ intNumber = 51 \ num2

Modulus Mod intNumber = 5 mod 2

Exponents ^ intNumber = 5^3 ‘5 cubed

Page 6: 6.01  Apply operators and Boolean expressions

Operators of Operations

Just as in math, Please Excuse My Dear Aunt Sally describes the precedence applied to math in programming

Parentheses Exponents Multiply Divide (NOTE: Division will be complete in the order

in which it appears in the problem, no ranking with division types.)

Add Subtract

Page 7: 6.01  Apply operators and Boolean expressions

Examples Using Familiar Operators

Operator Example dblResult

+ dblResult = 3 + 4 7

- dblResult = 4 – 3 1

* dblResult = 3 * 4 12

*, + dblResult = 3 * 4 + 5 17

*, +, () dblResult = 3 * (4 + 5) 27

^, * dblResult = 2^3 * 6 48

Page 8: 6.01  Apply operators and Boolean expressions

Reading in Numbers

Remember that a TextBox returns a String.

Visual Studio will automatically “convert” this for you; however, it is good programming style to explicitly convert the string to a number.

Use the Convert Method that is part of the System class.

You should use the Convert methods when you are bringing in numeric input to use for “math” from now on.

Page 9: 6.01  Apply operators and Boolean expressions

Converting Strings to Numbers

Method Purpose Example

ToInt16(String) Converts the specified string representation of a number to an equivalent 16-bit signed integer.

intNum = Convert.ToInt16(strInput)

ToDouble(String

Converts the specified string representation of a number to an equivalent double-precision floating-point number.

dblNum = Convert.ToDouble(strInput)

There are other Convert methods as well.http://msdn.microsoft.com/en-us/library/system.convert_methods.aspx

Page 10: 6.01  Apply operators and Boolean expressions

Division Operators

Regular division Rounds the decimal portion if assigned to an integer. This is the division with which you are most familiar.

Integer division cuts off the decimal portion and returns the integer ( \

)

Modulus division returns the remainder resulting form the division

(mod)

Page 11: 6.01  Apply operators and Boolean expressions

Integer division

19 \ 5 = 3

You may be a little confused these new types of divisions but it is really simple.

Anytime you divide a number into another number the answer returned is the whole part of the quotient.

Page 12: 6.01  Apply operators and Boolean expressions

Modular division

19 \ 5 = 3

Modular Division is about finding the remainder

Anytime you divide a number into another number the answer returned is the remainder part of the quotient.

Divisor 5 19 Dividend

3 R 4 Quotient

154 Remainder

Page 13: 6.01  Apply operators and Boolean expressions

Sample Program

Write a program that will accept 2 numbers and perform regular division, integer division and mod division based on the button clicked.

Page 14: 6.01  Apply operators and Boolean expressions

Sample Code

Dim intx As Integer = Convert.ToInt16(txtdividend.Text)Dim intd As Integer = Convert.ToInt16(txtdivisor.Text)Dim inta, intb, intc As Integer

inta = intx / intdintb = intx \ intdintc = intx Mod intd

Lbloutput.Text = intx.ToString & "/" & intd.ToString & " is " & inta.ToString & vbCrLf & intx.ToString & "\" & intd.ToString & " is " & intb.ToString & vbCrLf & "And" & vbCrLf & intx.ToString & " Mod " & intd.ToString & " Is " & intc.ToString

Page 15: 6.01  Apply operators and Boolean expressions

Division Operators

Example: Dim x As Integer = 18 ‘sets x = 18 Dim a, b, c As Integer ‘sets variable a, b and c_ ‘as an integerNotice that:

a = x / 4 ‘a is 4.5 so a is assigned 5 because of rounding

b = x \ 4 ‘b is assigned 4

c = x mod 4 ‘c is assigned 2

Page 16: 6.01  Apply operators and Boolean expressions

Relational Operators

Used to create a Boolean expression One that evaluates to True or False You have seen these also. Remember

> greater than< less than>= greater than or equal to<= less than or equal to= equal <> do not equal

Page 17: 6.01  Apply operators and Boolean expressions

Writing Boolean Expressions

Relational Operator Boolean Expression Result

>, intNum = 4 intNum > 6 False

>, intNum = 8 intNum > 6 True

<, intNum = 7 intNum < 10 True

>=, intNum = 6 intNum >= 6 True

<=, intNum = 6 intNum <= 6 True

<>, intNum = 6 intNum <> 6 False

Below is an deskcheck (table of possible outputs):

Page 18: 6.01  Apply operators and Boolean expressions

Sample Program

Write a program that will accept 2 numbers and perform addition, subtraction, multiplication, division, or mod division based on the button clicked.

Page 19: 6.01  Apply operators and Boolean expressions

Sample Code

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

Dim decNum1, decNum2, decResult As Decimal

decNum1 = txtNum1.Text decNum2 = txtNum2.Text

decResult = decNum1 + decNum2 lblResult.Text = decResult.ToString()

End Sub

Page 20: 6.01  Apply operators and Boolean expressions

Wrap Up

In this short PowerPoint we looked at some of the different operators can be used in Visual Basic

Please continue to 6.02 which introduces decision making statements.