Module 2 Part 3 Operators The Basic Input

Module 2: Part 3 - Operators The Basic Input – Output concepts Introduction to Programming

Operators> Arithmetic Operators Introduction to Computer Programming

Operators> Relational Operator Introduction to Computer Programming

Operators> Relational Operator Operation Description Examples of Expression 6<9 Value 1 (true) < Less than <= Less than or equal to 5 <= 5 1 (true) > Greater than 2>6 0 (false) >= Greater than or equal to 9 >= 5 1 (true) == Equal to 7 == 5 0 (false) != Not equal to 6 != 5 1 (true) Introduction to Computer Programming

Operators> Relational Operator Of course, instead of using only numeric constants, we can use any valid expression, including variables. Suppose that a=2, b=3 and c=6, (a == 5) (a*b) >= c (b+4) > (a*c) (b=2) == a

Operator> Logical Operator Introduction to Computer Programming

Operator> Logical Operator a b a && b a || b !a !b 0 0 1 1 0 1 0 0 0 1 1 1 0 0 1 0 Introduction to Computer Programming

Examples 1. ( (5 == 5) && (3 > 6) ) 2. ( (5 == 5) || (3 > 6) ) Let a=2 , b=5, c=15, d=17 3. (a >= 1) && (b == 5) Let a=2 , b=5, c=15, d=17 4. (c >= ( b * 3 ) ) || (a == 3) 5. ! ( ( a < b ) || ( c > d ) ) Introduction to Computer Programming

Operators> Conditional operators Introduction to Computer Programming

Operators> Conditional operators The conditional operator evaluates an expression returning a value if that expression is true. Its format is: condition ? result 1 : result 2 If condition is true the expression will return result 1, if it is not it will return result 2. Introduction to Computer Programming

Examples 1. 2. 3. 4. 7 == 5 ? 4 : 3 7 == 5+2 ? 4 : 3 5>3 ? a : b a>b ? a : b 5. int a, b, c; // returns 3, since 7 != 5. // returns 4, since 7 = 5+2. // returns the value of a, since 5 > 3. // returns whichever is greater, a or b. Output: ? ? ? a=2; b=7; c = (a>b) ? a : b; cout << c; Introduction to Computer Programming

Operators>Operator precedence and associativity Introduction to Computer Programming

Operators>Operator precedence and associativity Level Operator Description Grouping 1 : : scope Left-to-right 2 () []. -> ++ -- postfix Left-to-right ++ -- ~ ! unary (prefix) *& indirection and reference (pointers) +- unary sign operator 4 (type) type casting Right-to-left 5 . * ->* pointer-to-member Left-to-right 3 Introduction to Computer Programming Right-to-left

Operators>Operator precedence and associativity Level Operator Description Grouping 6 */% multiplicative Left-to-right 7 +- additive Left-to-right 8 << >> shift Left-to-right 9 < > <= >= relational Left-to-right 10 == != equality Left-to-right 11 & bitwise AND Left-to-right 12 ^ bitwise XOR Left-to-right 13 | bitwise OR Left-to-right 14 && logical AND Left-to-right

Operators>Operator precedence and associativity Grouping defines the precedence order in which operators are evaluated in the case that there are several operators of the same level in an expression. a = 5 + 7 % 2; might be written either as: a = 5 + (7 % 2); or a = (5 + 7) % 2;

Operators>Operator precedence and associativity Depending on the operation that we want to perform. Always include parentheses to make your code easier to read. a=5+7%2 Can be written as: a = 5 + (7 % 2) a = (5 + 7) % 2 // with a result of 6, // with a result of 0 Introduction to Computer Programming
- Slides: 16