lesson 3 - c++ operators

25
Lesson -03

Upload: john-son

Post on 03-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 1/25

Lesson -03

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 2/25

What is an Operator? They are used to manipulate primitive data types.

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 3/25

What is an Operand? Various operators are applied on Operands.

a numeric variable - integer, floating point orcharacter

any primitive type variable - numeric and boolean

reference variable to an object

a literal - numeric value, boolean value, or string.

an array element char primitive, which in numeric operations is

treated as an unsigned two byte integer

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 4/25

Types of Operators Assignment Operator ( = ) 

 Arithmetic Operators ( +, -, *, /, % ) 

Compound Assignment Operators (+=, -=, *=, /=, %=,>>=, <<=, &=, ^=, |=) 

Increase and decrease (++, --) 

Relational and equality operators ( ==, !=, >, <, >=, <=

)  Logical operators ( !, &&, || ) 

Conditional operator ( ? ) 

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 5/25

Assignment Operator “=” – Is used as the assignment operator

< variable> = <expression>

 An expression will be assigned to a variable using theassignment operator.

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 6/25

#include <iostream>using namespace std;

int main (){

int a, b; // a:?, b:?a = 10; // a:10, b:?b = 4; // a:10, b:4a = b; /* a:4, b:4 This statement assigns to variable a (the lvalue) the value

contained in variable b (the rvalue).*/b = 7; // a:4, b:7cout << "a:";

cout << a;cout << " b:";cout << b;

return 0;}

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 7/25

Arithmetic Operators

Operator Result

+ Addition

- Subtraction

* Multiplication

/ Division

% Modules – the remainder 

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 8/25

Modulus Operator % - Modulus

Gives the remainder after a division operation is

executed.Ex : -

13 % 5 =3

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 9/25

#include <iostream>

using namespace std;

int main ()

{int a, b,g,h,p,q;

double r;

a = 10; // a:10, b:?

b = 4; // a:10, b:4

g = a+b;

h = a-b;

p = a*b;

r = 10/6.0;

q = a%b;cout<<a<<b<<g<<h<<p<<q<<r;

return 0;

}

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 10/25

Increment and Decrement Increment – Add 1 to the variable

Decrement – Subtract 1 from the variable

Two types. Prefix (++operator , --operator)

Postfix (operator++, operator--)

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 11/25

Increment and DecrementPrefix Sufix

#include <iostream>using namespace std;int main ()

{

int x =10;

int y;

y = ++x;

cout<<“x =”; 

cout<<x;

cout<<“y =”; 

cout<<y;

return 0;

}

#include <iostream>using namespace std;int main ()

{

int x =10;

int y;

y = x++;

cout<<“x =”; 

cout<<x;

cout<<“y =”; 

cout<<y;

return 0;

}

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 12/25

Increment and Decrement#include <iostream>

using namespace std;

int main ()

{ int i = 3;

i++;cout<<i; // "4"++i;cout<<i; // "5"

cout<<++i; // "6"cout<<i++; // "6"cout<<i; // "7"

}

}

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 13/25

Compound Operators The compound operators perform shortcuts in

common programming operations.

Syntax:argument1 operator = argument2.

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 14/25

Compound OperatorsOperator Use Equivalent

+= op1 += op2 op1 = op1 + op2

-= op1 -= op2 op1 = op1 - op2

*= op1 *= op2 op1 = op1 * op2

/= op1 /= op2 op1 = op1 / op2

%= op1 %= op2 op1 = op1 % op2

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 15/25

Compound Operators#include <iostream>

using namespace std;

int main ()

{ int x = 0, y = 5;x += 3;cout<<“x : “<< x;  y *= x;cout<<“y : “<< y; 

}

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 16/25

Relational OperatorsOperator Result

= = Equal to

!= NOT equal to

> Grater than

< Less than

>= Greater than or equal to

<= Less than or equal to

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 17/25

Relational Operators#include <iostream>using namespace std;

int main ()

cout<<(7 == 5) // evaluates to false.

cout<<(5 > 4) // evaluates to true.

cout<<(3 != 2) // evaluates to true.

cout<<(6 >= 6) // evaluates to true.cout<<(5 < 5) // evaluates to false.

}

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 18/25

Of course, instead of using only numeric constants, we canuse any valid expression, including variables. Suppose thata=2, b=3 and c=6,

(a == 5) // evaluates to false since a is not equal to 5.

(a*b >= c) // evaluates to true since (2*3 >= 6) is true.

(b+4 > a*c) // evaluates to false since (3+4 > 2*6) is false.

((b=2) == a) // evaluates to true.

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 19/25

Logical operators ( !, &&, || )

!(5 == 5) // evaluates to false because the expression at itsright

(5 == 5) //is true.

!(6 <= 4) // evaluates to true because (6 <= 4) would befalse.

!true // evaluates to false

!false // evaluates to true.

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 20/25

( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false ).

( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ).

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 21/25

Conditional operator ( ? )

7==5 ? 4 : 3 // returns 3, since 7 is not equal to 5.

7==5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2.

5>3 ? a : b // returns the value of a, since 5 is greater than3.

a>b ? a : b // returns whichever is greater, a or b.

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 22/25

#include <iostream>using namespace std;int main ()

{ int a,b,c;a=2;b=7;c = (a>b) ? a : b;

cout << c;

return 0;}

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 23/25

Comma operator ( , )

a = (b=3, b+2);

 Would first assign the value 3 to b, and then assign b+2to variable a. So, at the end, variable a would contain

the value 5 while variable b would contain value 3.

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 24/25

Explicit type casting operator

Type casting operators allow you to convert a datum of a given type to another.

int i;

float f = 3.14;

i = (int) f;

7/28/2019 Lesson 3 - c++ Operators

http://slidepdf.com/reader/full/lesson-3-c-operators 25/25

Precedence of operators

 When writing complex expressions with severaloperands, we may have some doubts about whichoperand is evaluated first and which later. For

example, in this expression:

a = 5 + (7 % 2) // with a result of 6, or

a = (5 + 7) % 2 // with a result of 0