Download presentation
Presentation is loading. Please wait.
Published byShana Wade Modified over 9 years ago
1
LECTURE # 6 : STRUCTURED PROGRAMMING C++ OPERATORS By Mr. Ali Edan
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 their mission انواع الرموز حسب عملها Assignment علامة المساواة Arithmetic: addition, subtraction, modulo division,...etc رموز رياضية Relational: equal to, less than, grater than, …etc رموز علائقية Logical (decision-making): NOT, AND, OR رموز منطقية 3
4
Operators (cont.) Operators types based on number of operands Unary operators احادية Have only one operand May be prefix or postfix e.g. ! ++ -- Binary operators ثنائية Have two operands Infix e.g. + && == Ternary operators ثلاثية Have three operands e.g. ? : 4
5
Assignment operators Assignment statement takes the form below Binary operators Expression is evaluated and its value is assigned to the variable on the left side Shorthand notation varName = varName operator expression; varName operator = expression; 5 varName = expression; c = c + 3; c += 3; c = c + 3; c += 3;
6
Assignment operators (cont.) Assignment between objects of the same type is always supported 6
7
Arithmetic Operators All of them are binary 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/embedded parentheses 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
Arithmetic Operators Precedence 9
10
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); 10 534216
12
Example:
13
Increment and Decrement Operators Unary operators Adding 1 to or (subtracting 1 from) variable’s value 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) 13
14
Increment and Decrement Operators (cont.) 14
15
Examples 15 int x = 10; cout << “x = “ << ++x << endl; cout << “x = “ << x << endl; int x = 10; cout << “x = “ << ++x << endl; cout << “x = “ << x << endl; Example # 1 x = 11 output # 1 int x = 10; cout << “x = “ << x++ << endl; cout << “x = “ << x << endl; int x = 10; cout << “x = “ << x++ << endl; cout << “x = “ << x << endl; Example # 2 x = 10 x = 11 x = 10 x = 11 output # 2
16
Examples 16 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 = 11 y = 11 x = 11 y = 11 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 = 11 y = 10 x = 11 y = 10 output # 2
17
Exercise State the order of evaluation of the operators in each of the following C++ statements and show the value of x after each statement is performed. x = 7 + 3 * 6 / 2 - 1; x = 2 % 2 + 2 * 2 - 2 / 2; x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );
18
Answer: 15 3 324
19
Relational and Equality Operators Binary operators Used in decision -making statements 19
20
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 Used only with a single condition 20
21
Logical Operators Used to combine between multiple conditions && (logical AND) true if both conditions are true gender == 1 && age >= 65 || (logical OR) true if either of condition is true semesterAverage >= 90 || finalExam >= 90 21 1 st condition2 nd condition
22
Logical Operators (cont.) ! (logical NOT, logical negation) Returns true when its condition is false, and vice versa !( grade == sentinelValue ) Also can be written as grade != sentinelValue 22
23
Conditional operator ( ?: ) Ternary operator requires three operands Condition Value when condition is true Value when condition is false Syntax 23 Condition ? condition’s true value : condition’s false value
24
Examples Can be written as Can be written as.. ? 24 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
25
Summary of Operator Precedence and Associativity 25
26
bool Variables in Expressions false is zero and true is any non-zero The following codes applies implicit conversion between bool and int 26 int x = -10 ; bool flag = x ; // true int a = flag ; // assign the value 1 int b = !flag; // assign the value 0 x = flag + 3; // assign the value 4 int x = -10 ; bool flag = x ; // true int a = flag ; // assign the value 1 int b = !flag; // assign the value 0 x = flag + 3; // assign the value 4 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 # 1Code # 2
27
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 (=) 27
28
Exercise - 1 28 What is the output of the following program? 1 #include 2 3 using namespace std; 5 6 int main() 7 { 8 int x; int y; int z; 9 10 x = 30; y = 2; z = 0; 11 12 cout << (++++x && z ) << endl; 13 cout << x * y + 9 / 3 << endl; 14 cout << x << y << z++ << endl; 15 16 return 0; 17 18 } // end main 1 #include 2 3 using namespace std; 5 6 int main() 7 { 8 int x; int y; int z; 9 10 x = 30; y = 2; z = 0; 11 12 cout << (++++x && z ) << endl; 13 cout << x * y + 9 / 3 << endl; 14 cout << x << y << z++ << endl; 15 16 return 0; 17 18 } // end main
29
Exercise - 2 29 What is wrong with the following program? 1 int main() 2 { 3 int a,b,c,sum; 4 sum = a + b + c ; 5 return 0; 6 } 1 int main() 2 { 3 int a,b,c,sum; 4 sum = a + b + c ; 5 return 0; 6 }
30
End 30
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.