Introduction to Programming 4 Operators Dr Khizar Hayat

  • Slides: 15
Download presentation
Introduction to Programming – 4 Operators Dr. Khizar Hayat Associate Prof. of Computer Science

Introduction to Programming – 4 Operators Dr. Khizar Hayat Associate Prof. of Computer Science

Operators • Arithmetic operators • Assignment operator, i. e. = – Compound assignment or

Operators • Arithmetic operators • Assignment operator, i. e. = – Compound assignment or arithmetic assignment operators • Increment and Decrement operators – Pre-increment – Post-increment • Relational operators • Logical Operators • Bit-wise logical operators

Arithmetic Operators I- Unary, e. g. -5, +7 II- Binary 1. 2. 3. 4.

Arithmetic Operators I- Unary, e. g. -5, +7 II- Binary 1. 2. 3. 4. 5. Addition (+) Subtraction (-) Multiplication (*) Division (/) Modulo division (%) – For example 11%3=2 or 33%5=3 – Using ‘%’ for non-integer operands is a compilation error

Assignment operator • The operator ‘=‘ assigns a value to a variable • e.

Assignment operator • The operator ‘=‘ assigns a value to a variable • e. g. the statement i=7; would assign a value 7 to variable i. in this case is the i Lvalue (left value) and 7 is the Rvalue (right value) • Note that: – Lvalue must always be a single variable. – The Rvalue may be a constant or a variable or an expression involving multiple variables and constants. – The assignment always takes place from right to left (rightto-left rule), • e. g. x=y; would assign the value of y to x; the value of y will be unchanged.

Assignment operator #include<iostream> using namespace std; int main() { int a, b; a=10; b=4;

Assignment operator #include<iostream> using namespace std; int main() { int a, b; a=10; b=4; a=b; cout<<“a="<<a<<endl; cout<<“b="<<b<<endl; return(0); } Output? a=4 b=4

Compound assignment • May be arithmetic or bit-wise logical • Arithmetic: 1. 2. 3.

Compound assignment • May be arithmetic or bit-wise logical • Arithmetic: 1. 2. 3. 4. 5. +=, e. g. i+=3; means i=i+3; -=, e. g. i-=1; means i=i-1; *=, e. g. i*=7; means i=i*7; /=, e. g. i/=2; means i=i/2; %= , e. g. i%=10; means i=i%10; • Compound bitwise Logical work the same way but with logical bitwise operators, e. g. i|=77;

Increment/decrement operators • The unary Increment operator ++ – count++; is the same as,

Increment/decrement operators • The unary Increment operator ++ – count++; is the same as, count=count+1; or count+=1; • The unary decrement operator -– count--; is the same as, count=count-1; or Count-=1;

Pre-increment and Post-increment • When the operator ++ comes before the variable then it

Pre-increment and Post-increment • When the operator ++ comes before the variable then it is pre-increment. – , e. g. ++i; means first increase the value of i by 1 and then execute the statement • When the operator ++ comes after the variable then it is post-increment. – , e. g. i++; means first execute the statement and then increase the value of i by 1. • Same reasoning for pre- and post decrements, i. e. --j; and j--;

Example: Pre-increment and Post-increment #include<iostream> using namespace std; int main() { int c; c=5;

Example: Pre-increment and Post-increment #include<iostream> using namespace std; int main() { int c; c=5; cout<<“c="<<c<<endl; cout<<“c="<<c++<<endl; cout<<“c="<<c<<endl; cout<<“c="<<++c<<endl; cout<<“c="<<c<<endl; cout<<c<<“t"<<c++<<“t"<<++c<<“t"<<c++<<endl; //just for fun return(0); }

Relational Operators • The relational operators evaluate to either true (1) or false (0)

Relational Operators • The relational operators evaluate to either true (1) or false (0) • In C++ the relational operators are: 1. 2. 3. 4. 5. 6. == Equal to != Not equal to > Greater than < less than >= Greater than or equal to <= less than or equal to • Never confuse == (equal to) with = (assignment or gets)

Examples • What would the following expression evaluate to? • (7==5) //false 0 •

Examples • What would the following expression evaluate to? • (7==5) //false 0 • (5>4) //true 1 • (3!=2) //true 1 • (6>=6) //true 1

Examples • What would the following expression evaluate to? Let int a=2, b=3, c=6;

Examples • What would the following expression evaluate to? Let int a=2, b=3, c=6; • (a==5) • (a*b>=c) //false //true • (b+4>a*c) //false • ((b=2)==a) //true

Logical Operators • The logical operators also evaluate to either: true (1) or false

Logical Operators • The logical operators also evaluate to either: true (1) or false (0) • In C++ the Logical operators are: 1. 2. 3. ! NOT && AND || OR Unary Binary draw truth table • Bitwise logical operators are: 1. 2. 3. 4. 5. & bitwise AND | bitwise OR ^ bitwise XOR << left shift >> right shift

The ternary operator (? : ) if-then-else • Also called the conditional operator •

The ternary operator (? : ) if-then-else • Also called the conditional operator • Use: condition ? result 1 : result 1 (if true) (if false) • Examples: – 7==5? 4: 3; //return 3 – 7==5+2? 4: 3; //return 4 – a>b? a: b; /*return whichever greater, a or b */

Example ternary operator #include<iostream> using namespace std; int main() { int a, b, c;

Example ternary operator #include<iostream> using namespace std; int main() { int a, b, c; a=2; b=7; c= (a>b)? a: b; cout<<“c = "<<c<<endl; return(0); }