5 03 Apply operators and Boolean expressions COMPUTER

5. 03 Apply operators and Boolean expressions COMPUTER PROGRAMMING I SUMMER 2011

Objective/Essential Standard: 5. 00 Apply Programming & Conditional Logic Indicator: 5. 03 Apply operators and Boolean expressions. (3%)

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.

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.

Samples of Math with various Operators Addition + ◦ int. Number = num 1 + num 2 Subtraction – ◦ int. Number = num 1 - 50 Multiplication * ◦ int. Number = num 1 * num 2 Division / ◦ int. Number = 51 / num 2 Integer Division ◦ int. Number = 51 num 2 Modulus Mod ◦ int. Number = 5 mod 2 Exponents ^ ◦ int. Number = 5^3 ‘ 5 cubed

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

Examples Using Familiar Operators Operator Example dbl. Result + dbl. Result = 3 + 4 7 - dbl. Result = 4 – 3 1 * dbl. Result = 3 * 4 12 *, + dbl. Result = 3 * 4 + 5 17 *, +, () dbl. Result = 3 * (4 + 5) 27 ^, * dbl. Result = 2^3 * 6 48

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)

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.

Modular division 19 Mod 5 = 4 Divisor 3 R 4 5 Quotient 19 Dividend 15 4 Remainder 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.

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

Sample Code ‘Declare & Input Dim int. Numer As Integer = Convert. To. Int 32(txtnumerator. Text) Dim int. Denom As Integer = Convert. To. Int 32(txtdenominator. Text) Dim int. Ans. Div, int. Ans. Int. Div, int. Ans. Mod As Integer ‘Calculate int. Ans. Div = int. Numer / int. Denom int. Ans. Int. Div = int. Numer int. Denom int. Ans. Mod = int. Numer Mod int. Denom ‘Output lbloutput. Text = int. Numer. To. String & "/" & int. Denom. To. String & " is " & int. Ans. Div. To. String & vb. Cr. Lf & int. Numer. To. String & "" & int. Denom. To. String & " is " & int. Ans. Int. Div. To. String & vb. Cr. Lf & "And" & vb. Cr. Lf & int. Numer. To. String & " Mod " & int. Denom. To. String & " Is " & int. Ans. Mod. To. String

Division Operators Dim int. Numer As Integer = 18 Dim int. An. Reg. Div As Integer = 0 Dim int. Ans. Int. Div As Integer = 0 Dim int. Ans. Mod. Div As Integer = 0 int. An. Reg. Div = int. Numer / 4 ‘sets int. Numer = 18 ‘int. An. Reg. Div = 4. 5 ‘so int. An. Reg. Div is ‘assigned 5 due to ‘rounding int. Ans. Int. Div = int. Numer 4 ‘int. Ans. Int. Div = 4 int. Ans. Mod. Div = int. Numer mod 4 ‘int. Ans. Mod. Div = 2

Relational Operators Used to create a Boolean expression ◦ One that evaluates to True or False Visual Basic Relational Operators > greater than < less than >= greater than or equal to <= less than or equal to = equal <> do not equal

Writing Boolean Expressions Below is an Desk. Check (table of possible outputs): Relational Operator Boolean Expression Result >, int. Num = 4 int. Num > 6 False >, int. Num = 8 int. Num > 6 True <, int. Num = 7 int. Num < 10 True >=, int. Num = 6 int. Num >= 6 True <=, int. Num = 6 int. Num <= 6 True <>, int. Num = 6 int. Num <> 6 False

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

Sample Code Private Sub btn. Add_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Add. Click Dim dbl. Num 1, dbl. Num 2, dbl. Result As Double dbl. Num 1 = Convert. To. Double(txt. Num 1. Text) dbl. Num 2 = Convert. To. Double(txt. Num 2. Text) dbl. Result = dbl. Num 1 + dbl. Num 2 lbl. Result. Text = dbl. Result. To. String End Sub

Wrap Up In this short Power. Point we looked at some of the different operators can be used in Visual Basic. For more information on arithmetic operators http: //msdn. microsoft. com/en-us/library/b 6 ex 274 z(v=VS. 80). aspx For more information on relational operators http: //msdn. microsoft. com/en-us/library/yf 3 abyat. aspx
- Slides: 18