Download presentation
Presentation is loading. Please wait.
Published byAriel Reynolds Modified over 9 years ago
1
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq
2
Content 2 C++ operators Assignment operators Arithmetic operators Increment and decrement operators Decision making operators (logical and relational) Conditional operator Precedence and associativity of operators Common errors
3
Operators Data connectors within expression or equation. Concept related Operand: data that operator connects and processes. Resultant: answer when operation is completed. Operators types based on number of operands 3 1.Unary operators Have only one operand. May be prefix or postfix. e.g. ! ++ -- 2.Binary operators Have 2 operands Infix ( between two operands). e.g. + && == 3.Ternary operators Have three operands. e.g. ? :
4
Operators (cont.) 4 Operators Types based on their mission Assignment Relational and Equality ArithmeticLogical =, +=, -=, *=, /=, %=, =, ==,!= +, -, *, /, %, ++, -- AND(&&) OR( || ) NOT( ! ) AND(&&) OR( || ) NOT( ! )
5
Assignment operators In C++ ( = ) is called the assignment operator. It is Binary operators. Assignment statement takes the form below: Expression is evaluated and its value is assigned to the variable on the left side. Shorthand notation: varName = varName operator expression; It will be: varName operator = expression; 5 varName = expression; c = c + 3; c += 3; c = c + 3; c += 3; Example:
6
Assignment operators (cont.) 6
7
Arithmetic Operators Arithmetic expressions appear in straight-line form. Parentheses () are used to maintain priority of manipulation. 7
8
Arithmetic Operators Precedence Operators in parentheses evaluated first. Nested parentheses. E.g. ( a * ( b + c ) ) Operators in innermost pair first. Multiplication, division, modulus applied next. Operators applied from left to right. Addition, subtraction applied last. Operators applied from left to right. 8
9
Example The statement is written in algebra as z = pr % q + w / (x – y) How can we write and evaluate the previous statement in C++ ? z = p * r % q + w / (x - y); 9 534216
10
Increment and Decrement Operators Increase operator (++): increases by one the value stored in a variable. Decrease operator (--): reduces by one the value stored in a variable. Increment operator gives the same result of (c=c+1) or (c+=1) Decrement operator gives the same result of (c=c-1) or (c-=1) 10
11
Increment and Decrement Operators (cont.) 11 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. In case that it is used as a postfix (a++) the value stored in a is increased after being evaluated.
12
Examples 12 int x = -10, y; y = ++x; cout << “x = “ << x << endl; cout << “y = “ << y << endl; int x = -10, y; y = ++x; cout << “x = “ << x << endl; cout << “y = “ << y << endl; Example # 1 x = -9 y = -9 x = -9 y = -9 output # 1 int x = -10, y; y = x++; cout << “x = “ << x << endl; cout << “y = “ << y << endl; int x = -10, y; y = x++; cout << “x = “ << x << endl; cout << “y = “ << y << endl; Example # 2 x = -9 y = -10 x = -9 y = -10 output # 2
13
Relational and Equality Operators Binary operators Used in decision -making statements 13
14
Relational and Equality Operators (cont.) Have the same level of precedence. Applied from left to right. Used with conditions. Return the value true or false. 14
15
Logical Operators Used to combine between multiple conditions. && (logical AND) true if both conditions are true gender==‘f’ && age >= 65 || (logical OR) true if either of condition is true semesterAverage >= 90 || finalExam >= 90 15 1 st condition2 nd condition
16
Logical Operators (cont.) ! (logical NOT, logical negation) Unary operator. Returns true when its condition is false, and vice versa !( grade == maximumGrade ) Also can be written as grade != maximumGrade 16
17
Conditional operator ( ?: ) Ternary operator requires three operands Condition Value when condition is true Value when condition is false Syntax 17 Condition ? condition’s true value : condition’s false value
18
Examples Can be written as Can be written as.. ? 18 grade >= 60 ? cout<<“Passed” : cout<<“Failed”; Example # 1 cout = 60 ? “Passed” : “Failed”); int i = 1, j = 2, Max; Max = ( i > j ? i : j ); int i = 1, j = 2, Max; Max = ( i > j ? i : j ); Example # 2
19
Summary of Operator Precedence and Associativity 19
20
Boolean Data Type 20 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 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 Code # 1
21
Common Compilation Errors Attempt to use % with non-integer operands. Spaces between pair of symbols e.g. (==, !=, …etc). Reversing order of pair of symbols e.g. =!. Confusing between equality (==) and assignment operator (=). 21
22
Exercise - 1 22 What is the output of the following program? 1 #include 2 3 using namespace std; 4 5 6 int main() 7 { 8 int a, b=3; a=b; a+=2; 9 10 cout << a << endl; 11 12 return 0; 13 14 } // end main 1 #include 2 3 using namespace std; 4 5 6 int main() 7 { 8 int a, b=3; a=b; a+=2; 9 10 cout << a << endl; 11 12 return 0; 13 14 } // end main
23
Exercise - 2 23 Convert the following algebraic expression to an arithmetic C++ expressions: Solution: M=(a+b+c+d+e)/5
24
Exercise - 3 24 Evaluate the following C++ expression: y= a * x * x + b * x + c; Assume that : A=2 X=5 B=3 C=7. Solution: y=2*5*5+3*5+7 y=10*5 +3*5 +7 y=50+3*5+7 y=50+15+7 y=65+7 y=72
25
Exercise - 4 25 Write a program that calculates the average of two entered real numbers. The output will be:
26
Exercise - 4 26
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.