OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions...
-
Upload
randall-pearson -
Category
Documents
-
view
243 -
download
0
description
Transcript of OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions...
![Page 1: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/1.jpg)
OPERATORSChapter2:part2
![Page 2: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/2.jpg)
Operators• Operators are special symbols used for:
• mathematical functions• assignment statements• logical comparisons
• Examples of operators:• 3 + 5 // uses + operator• 14 + 5 – 4 * (5 – 3) // uses +, -, * operators
• Expressions: can be combinations of variables and operators that result in a value
![Page 3: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/3.jpg)
Groups of OperatorsThere are 5 different groups of operators:
• Arithmetic Operators• Assignment Operator• Increment / Decrement Operators• Relational Operators• Logical Operators
![Page 4: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/4.jpg)
Arithmetic Operators
![Page 5: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/5.jpg)
Order of Precedence1. ( ) evaluated first, inside-out
2. , /, or % evaluated second, left-to-right
3. +, evaluated last, left-to-right
![Page 6: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/6.jpg)
•
![Page 7: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/7.jpg)
Basic Assignment Operator• We assign a value to a variable using the basic
assignment operator (=).
• Assignment operator stores a value in memory.
• The syntax is leftSide = rightSide ;
Examples:• Literal: ex. i = 1;• Variable identifier: ex. start = i;• Expression: ex. sum = first + second;
![Page 8: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/8.jpg)
Arithmetic/Assignment OperatorsC++ allows combining arithmetic and assignment operators
into a single operator:
Addition/assignment=+Subtraction/assignment=
Multiplication/assignment=Division/assignment=/
Remainder/assignment =%
![Page 9: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/9.jpg)
Increase and Decrease)-- ,++( • The increase operator (++) and the decrease operator (--)
increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively.
1. c++; 2. c+=1; 3. c=c+1;
• The three above expressions are all equivalent in its functionality: the three of them increase by one the value of c.
![Page 10: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/10.jpg)
Increase and Decrease)-- ,++( • In the case that the increase operator is used as a prefix
(++a) the value is increased before the result of the expression is evaluated • Therefore the increased value is considered in the outer
expression.• In case that it is used as a suffix (a++) the value stored in
a is increased after being evaluated • Therefore the value stored before the increase operation is
evaluated in the outer expression
![Page 11: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/11.jpg)
Example// apply increment-decrement operators1.B=3; A=++B; // A contains 4, B contains 4 2.B=3;A=B++; // A contains 3, B contains 4
![Page 12: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/12.jpg)
Relational and Equality Operators• Relational operators compare two values• They Produce a boolean value (true or false) depending
on the relationship
![Page 13: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/13.jpg)
Example• int x = 3;• int y = 5;• bool result; result = (x > y);• now result is assigned the value false because 3 is not
greater than 5
![Page 14: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/14.jpg)
Logical OperatorsUsed to combine multiple conditions:
• && is the logical AND
• || is the logical OR
• ! (logical NOT, logical negation)
![Page 15: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/15.jpg)
Logical Operators && is the logical AND
• True if both conditions are true• Example: age>=50 $$ gender==‘f’
![Page 16: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/16.jpg)
Logical Operators || is the logical OR
• True if either of conditions is true
• Example: age>=50 || gender==‘f’
![Page 17: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/17.jpg)
Logical Operators! (logical NOT, logical negation)
• Return true when its condition is false and vice versa• Example:
(! grade==maximumGrade)• Alternative:
grade != maximumGrade
![Page 18: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/18.jpg)
18
Boolean Data Typebool test1,test2,test3;int x=3,y=6,z=4;test1=x>y; // falsetest2=!)x==y(; //truetest3=x<y && x<z; //truetest3= test1|| test2; //truetest2=!test1; //true
![Page 19: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/19.jpg)
19
Example1// compound assignment operators #include <iostream> using namespace std; int main () { int a, b=3; a = b; a+=2; // equivalent to a=a+2cout << a; return 0; }
![Page 20: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/20.jpg)
20
Example 2• Convert the following algebraic expression to an
arithmetic C++ expressions:
![Page 21: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.](https://reader033.fdocuments.in/reader033/viewer/2022050900/5a4d1b1b7f8b9ab05999348d/html5/thumbnails/21.jpg)
21
Example 3