Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming – 4 Operators

Similar presentations


Presentation on theme: "Introduction to Programming – 4 Operators"— Presentation transcript:

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

2 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

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

4 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 (right-to-left rule), e.g. x=y; would assign the value of y to x; the value of y will be unchanged.

5 Assignment operator a=4 b=4 Output?
#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

6 Compound assignment May be arithmetic or bit-wise logical Arithmetic:
+=, 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;

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

8 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--;

9 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<<“\t"<<c++<<“\t"<<++c<<“\t"<<c++<<endl; //just for fun return(0); }

10 Relational Operators The relational operators evaluate to either
true (1) or false (0) In C++ the relational operators are: == 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)

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

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

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

14 The ternary operator (? :) if-then-else
Also called the conditional operator Use: condition ? result1 : result1 (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 */

15 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); }


Download ppt "Introduction to Programming – 4 Operators"

Similar presentations


Ads by Google