OPERATORS Chapter2:part2
Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples of operators: // uses + operator – 4 * (5 – 3) // uses +, -, * operators Expressions: can be combinations of variables and operators that result in a value
Groups of Operators There are 5 different groups of operators: Arithmetic Operators Assignment Operator Increment / Decrement Operators Relational Operators Logical Operators
Arithmetic Operators
Order of Precedence 1. ( ) 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 Operators C++ 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 operators 1. 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 Operators Used 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
Boolean Data Type bool test1,test2,test3; int x=3,y=6,z=4; test1=x>y; // false test2=!(x==y); //true test3=x<y && x<z; //true test3= test1|| test2; //true test2=!test1; //true 18
Example1 // compound assignment operators #include using namespace std; int main () { int a, b=3; a = b; a+=2; // equivalent to a=a+2 cout << a; return 0; } 19
Example 2 Convert the following algebraic expression to an arithmetic C++ expressions: 20
Example 3 21