Download presentation
Presentation is loading. Please wait.
Published byRandall Pearson Modified over 9 years ago
1
OPERATORS Chapter2:part2
2
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
3
Groups of Operators There are 5 different groups of operators: Arithmetic Operators Assignment Operator Increment / Decrement Operators Relational Operators Logical Operators
4
Arithmetic Operators
5
Order of Precedence 1. ( ) evaluated first, inside-out 2. , /, or % evaluated second, left-to-right 3. +, evaluated last, left-to-right
7
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;
8
Arithmetic/Assignment Operators C++ allows combining arithmetic and assignment operators into a single operator: Addition/assignment+= Subtraction/assignment = Multiplication/assignment = Division/assignment/= Remainder/assignment %=
9
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.
10
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
11
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
12
Relational and Equality Operators Relational operators compare two values They Produce a boolean value (true or false) depending on the relationship
13
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
14
Logical Operators Used to combine multiple conditions: && is the logical AND || is the logical OR ! (logical NOT, logical negation)
15
Logical Operators && is the logical AND True if both conditions are true Example: age>=50 $$ gender==‘f’
16
Logical Operators || is the logical OR True if either of conditions is true Example: age>=50 || gender==‘f’
17
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 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
19
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
20
Example 2 Convert the following algebraic expression to an arithmetic C++ expressions: 20
21
Example 3 21
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.