Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS161 Introduction to Computer Science Topic #6.

Similar presentations


Presentation on theme: "1 CS161 Introduction to Computer Science Topic #6."— Presentation transcript:

1 1 CS161 Introduction to Computer Science Topic #6

2 CS161 Topic #62 Today in CS161 More Selective Execution –Logical Operations: && and || –Truth Tables –Applying logicals to if statements Operator Precedence –Increment and Decrement Operators

3 CS161 Topic #63 Logical Operators There are 3 logical (boolean) operators: &&And (operates on two operands) ||Or (operates on two operands) !Not (operates on a single operand) && evaluates to true if both of its operands are true; –otherwise it is false.

4 CS161 Topic #64 Logical Operators || evaluates to true if one or the other of its operands are true; –it evaluates to false only if both of its operands are false. ! gives the boolean complement of the operand. –If the operand was true, it results in false.

5 CS161 Topic #65 Logical Operators Conditional ExpressionLogical value True/False (5 == 10) && (30 < 88)0False (5 == 10) || (30 < 88)1True !(5==10) && (30 < 88)1True 40 != 441True !(40 != 44)0False

6 CS161 Topic #66 Expressions in C++ Every expression in C++ results in a value For example, when you call the pow function, it results in a value that consisted of raising exp to the power supplied. This value can then be used within a larger expression, eg: x = y + pow(x,3); This value is called the residual value

7 CS161 Topic #67 AND Truth Table op1 && op2 results in: op1op2residual value truetruetrue 1 truefalsefalse 0 falsetruefalse 0 falsefalsefalse 0

8 CS161 Topic #68 OR Truth Table op1 || op2 results in: op1op2residual value truetruetrue 1 truefalsetrue 1 falsetruetrue 1 falsefalsefalse 0

9 CS161 Topic #69 NOT Truth Table !op1 results in: op1 residual value truefalse 0 falsetrue 1

10 CS161 Topic #610 Logicals in if Statements Now let’s apply this to the if statements. For example, to check if our input is only an ‘m’ or an ‘i’ char selection; cin >> selection if ((‘m’ != selection ) && (‘i’ != selection) ) cout << “Error! Try again”;

11 CS161 Topic #611 Logicals in if Statements Why would the following be incorrect? if ((‘m’ != selection ) || (‘i’ != selection) ) cout << “Error! Try again”; Because no mater what you type in (m, i, p, q) it will never be both an m and an i ! If an m is entered, it won’t be an i !!!!!

12 CS161 Topic #612 Logicals in if Statements Let’s change this to check if they entered in either an m or an i: (this is correct) if ((‘m’ == selection ) || (‘i’ == selection ) ) cout << “Correct!”; else cout << “Error. Try Again!”;

13 CS161 Topic #613 Logicals in if Statements Now, let’s slightly change this.... if (!((‘m’ == selection ) || (‘i’ == selection ))) cout << “Error. Try Again!”; Notice the parens...you must have a set of parens around the logical expression

14 CS161 Topic #614 Arithmetic Expressions Let's take a look on operations that can be performed on real data: result = +realvariable <== No change result = -realvariable <== Takes the negative result = a+b <== Takes the sum result = a-b <== Takes the difference result = a*b <== Takes the product result = a/b <== Performs division

15 CS161 Topic #615 Arithmetic Expressions Other Interesting Operators for... –Compound Assignment *=/=+=-= result += 10result = result+10 result *= x+yresult = result *(x+y) result /= x+yresult = result/(x+y)

16 CS161 Topic #616 Arithmetic Expressions One more operator... –Integer division /%(remainder) int number; number = 10/3;//answer is 3 number = 10%3;//answer is 1 10 3 3 1

17 CS161 Topic #617 Operator Precedence One operator cannot follow another (2.5 + -3.6 is illegal)....you can do this by using parentheses: (2.5 + (-3.6)) With parentheses, the operations within parens is performed first. When parens are nested...the innermost set of parens is performed first: 2.0+(3.0*(4.0-1.0)) is the same as 2.0+(3.0*3.0) which is 11.0

18 CS161 Topic #618 Operator Precedence Watch out for ambiguous expressions when parens are not used: –What does 3.0+1.0*4.0 mean? 7.0? Or, 16.0? Since no parens are given, we go by the order of precedence of operators. *, /, % have a higher precedence....than + and -. So, the answer to above is 7.0!

19 CS161 Topic #619 Operator Precedence What about, 3.0*2.0-7.0/2.0? 1. First take the highest priority (* and /)...and go left to right. 2. So, first multiply 3.0*2.0 ==>> 6.0 3. Then divide 7.0/2.0 ===>>>3.5 4. So, we have so far 6.0 - 3.5 5. Which is 2.5

20 CS161 Topic #620 Increment/Decrement Ops There are two more operators that add or subtract 1 ++i means i = i + 1 --i means i = i - 1 These are used in their prefix form They can also be used in a postfix form: i++i-- Although in books you will find the postfix form very common, get in the habit of using the prefix whenever possible!

21 CS161 Topic #621 Postfix Increment/Decrement i++means: 1) Residual value is the current value of the variable 2) Then, increment the variable by 1 3) For example: int i = 100; cout << i++; Displays 100 not 101!

22 CS161 Topic #622 Increment/Decrement More examples: int i; cin >> i; i++; ++i; cout << i; inputoutput 50 52 100102

23 CS161 Topic #623 Increment/Decrement More examples: int i, j; cin >> i; j = i++; j = ++j; cout << j; inputoutput 50 51 100101


Download ppt "1 CS161 Introduction to Computer Science Topic #6."

Similar presentations


Ads by Google