21
OPERATORS Chapter2:part2

OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples

Embed Size (px)

DESCRIPTION

Groups of Operators There are 5 different groups of operators: Arithmetic Operators Assignment Operator Increment / Decrement Operators Relational Operators Logical Operators

Citation preview

Page 1: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples

OPERATORSChapter2:part2

Page 2: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples

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

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

Arithmetic Operators

Page 5: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples

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

Page 7: OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples

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

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

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

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

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

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

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

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

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

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

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

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

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

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

21

Example 3