LECTURE 6 STRUCTURED PROGRAMMING C OPERATORS By Mr
LECTURE # 6 : STRUCTURED PROGRAMMING C++ OPERATORS By Mr. Ali Edan
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
Operators ﺍﻟﺮﻣﻮﺯ ﺍﻟﺮﻳﺎﺿﻴﺔ ﺍﻭ ﺍﻟﻤﻨﻄﻘﻴﺔ 3 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 ﺭﻣﻮﺯ ﻣﻨﻄﻘﻴﺔ
Operators (cont. ) 4 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. ? :
Assignment operators 5 Assignment statement takes the form below var. Name = expression; Binary operators Expression is evaluated and its value is assigned to the variable on the left side Shorthand notation var. Name = var. Name operator expression; c = c + 3; var. Name operator = expression; c += 3;
Assignment operators (cont. ) 6 Assignment between objects of the same type is always supported
Arithmetic Operators 7 All of them are binary operators Arithmetic expressions appear in straight-line form Parentheses () are used to maintain priority of manipulation
8 Arithmetic Operators Precedence Operators in parentheses evaluated first Nested/embedded parentheses Multiplication, division, modulus applied next Operators in innermost pair first Operators applied from left to right Addition, subtraction applied last Operators applied from left to right
9 Arithmetic Operators Precedence
Example 10 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 = 6 p * 2 r % 3 q + 5 w / 4 (x 1 y);
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)
Increment and Decrement Operators (cont. ) 14
Examples 15 Example # 1 int x = 10; cout << “x = “ << ++x << endl; cout << “x = “ << x << endl; output # 1 x = 11 Example # 2 int x = 10; cout << “x = “ << x++ << endl; cout << “x = “ << x << endl; output # 2 x = 10 x = 11
Examples 16 Example # 1 int x = 10 , y; output # 1 y = ++x; cout << “x = “ << x << endl; cout << “y = “ << y << endl; x = 11 y = 11 Example # 2 int x = 10 , y; y = x++; cout << “x = “ << x << endl; cout << “y = “ << y << endl; output # 2 x = 11 y = 10
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 ) );
Answer: 15 3 324
19 Relational and Equality Operators Binary operators Used in decision -making statements
Relational and Equality Operators (cont. ) 20 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
Logical Operators 21 Used to combine between multiple conditions && (logical AND) true if both conditions are true 1 st condition gender == 1 2 nd condition && age >= 65 || (logical OR) true if either of condition is true semester. Average >= 90 || final. Exam >= 90
Logical Operators (cont. ) 22 ! (logical NOT, logical negation) Returns true when its condition is false, and vice versa !( grade == sentinel. Value ) Also can be written as grade != sentinel. Value
Conditional operator (? : ) 23 Ternary operator requires three operands Condition Value when condition is true Value when condition is false Syntax Condition ? condition’s true value : condition’s false value
Examples 24 Example # 1 grade >= 60 ? cout<<“Passed” : cout<<“Failed”; Can be written as cout << (grade >= 60 ? “Passed” : “Failed”); Example # 2 int i = 1, j = 2, Max; Max = ( i > j ? i : j ); Can be written as. . ?
25 Summary of Operator Precedence and Associativity
bool Variables in Expressions 26 false is zero and true is any non-zero The following codes applies implicit conversion between bool and int Code # 1 int bool x = -10 ; 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 int test 1 Code # 2 test 1, test 2, test 3 ; x = 3 , y = 6 , z = 4 ; = x > y ; // false test 2 = !(x == y ); // true test 3 = x < y && x < z ; // true test 3 = test 1 || test 2 ; // true test 2 = !test 1; // true
Common Compilation Errors 27 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 (=)
Exercise - 1 28 What is the output of the following program? 1 2 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <iostream> using namespace std; int main() { int x; int y; x = 30; y = 2; int z; z = 0; cout << (++++x && z ) << endl; cout << x * y + 9 / 3 << endl; cout << x << y << z++ << endl; return 0; } // end main
Exercise - 2 29 What is wrong with the following program? 1 2 3 4 5 6 int main() { int a, b, c, sum; sum = a + b + c ; return 0; }
30 End
- Slides: 30