Upload
randall-pearson
View
242
Download
0
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
OPERATORSChapter2:part2
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
Groups of OperatorsThere are 5 different groups of operators:
• Arithmetic Operators• Assignment Operator• Increment / Decrement Operators• Relational Operators• Logical Operators
Arithmetic Operators
Order of Precedence1. ( ) evaluated first, inside-out
2. , /, or % evaluated second, left-to-right
3. +, evaluated last, left-to-right
•
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;
Arithmetic/Assignment OperatorsC++ allows combining arithmetic and assignment operators
into a single operator:
Addition/assignment=+Subtraction/assignment=
Multiplication/assignment=Division/assignment=/
Remainder/assignment =%
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.
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
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
Relational and Equality Operators• Relational operators compare two values• They Produce a boolean value (true or false) depending
on the relationship
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
Logical OperatorsUsed to combine multiple conditions:
• && is the logical AND
• || is the logical OR
• ! (logical NOT, logical negation)
Logical Operators && is the logical AND
• True if both conditions are true• Example: age>=50 $$ gender==‘f’
Logical Operators || is the logical OR
• True if either of conditions is true
• Example: age>=50 || gender==‘f’
Logical Operators! (logical NOT, logical negation)
• Return true when its condition is false and vice versa• Example:
(! grade==maximumGrade)• Alternative:
grade != maximumGrade
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
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; }
20
Example 2• Convert the following algebraic expression to an
arithmetic C++ expressions:
21
Example 3