Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operators and Expressions

Similar presentations


Presentation on theme: "Operators and Expressions"— Presentation transcript:

1 Operators and Expressions

2 Operators & Expressions
C++ is very rich in built-in operators. In fact C++ places more significance on operators than most other computer languages do. C++ offers different classes of operators : arithmetic, relational, logical, increment-decrement, conditional, bitwise etc. In addition C++ has some special operators for particular tasks. Each operator performs a specific task it is designed for.

3 The Operations are represented by Operators
The objects of the operations are referred to as Operands

4 Operators in C++ I/O Operators Arithmetic Operator
Increment / Decrement Operators Relational Operator Logical Operator Conditional Operator ?:

5 I/O Operators Out put Operator(“<<“) Input Operator(“>>“)
Used to direct a value to standard output. E.g: cout <<“The sum is 5”; Input Operator(“>>“) Used to read a value from standard input. E.g: int first, second; cin >>first>>second;

6 Arithmetic Operators Unary Operators
Used to mean the value of a number. Requires only One Operand to act. Unary + Unary – Binary Operators Used for Arithmetic operations. Requires Two Operands to act.

7 Binary Operators Symbol Name (Action) Example Result Comment +
Addition 6+5 11 Add values of its Two Operands - Subtraction 6-5 1 Subtracts values of its Two Operands (right operand from Left ) * Multiplication 6*5 30 Multiplies values of its Two Operands / Division 60/5 12 Divides value of Left Operand with that on Right % Modulus (or Remainder) 6/5 1 Gives the remainder when the value on the Left is divided by that on Right

8 Increment/Decrement Operators

9 How post fix & prefix works?
int a = 10, C C = a a + ++a + a cout<<C << a<< endl; OUTPUT 48 13

10 Relational Operators p q p<q p<=q p==q p>q p>=q p!=q 9 4 1 1 1 5 7 1 1 1 Relational Operators are the Operators used to compare between two values. It gives either True or False as answers. In the above Table column ‘p’ and ‘q’ represents values of p and q. While the other columns namely ‘p<q’, ‘p<=q’, ‘p==q’, ‘p>q’, ‘p>=q’ and ‘p!=q’ shows the answers produced when such comparisons have been carried out. Here ‘0’ represents ‘FALSE’ and ‘1’ represents ‘TRUE’.

11 Logical Operators Symbol Name Example Result Comment && AND
(6 <=6)&&(5<3) False as One of it is False. || OR (6<=6)||(5<3) 1 One of the expression is true and hence True. ! NOT !(6<=6) Negates the result of the expression.

12 Conditional Operators ?:
Stores a value depends upon a condition Operator ( ?: ) is known as ternary operator Form: expression1 ? Expression2:expression3

13 Eg To Illustrate Conditional Operators ?:
int val, res, n = 1000; cin>>val; res = n + val > 1750 ? 400 : 200; cout<<res; i) if the input is 2000 iii) if the input is 500. OUTPUT i) 400, because the arithmetic operator + has higher precedence than ? : operator thus the condition before ? is taken as (n + val) and ( )> 1750 is true. ii) 200, because ( ) > 1750 is false.

14 Some other operators Sizeof Comma

15 sizeof Operator Unary compile-time operator
Returns the length (in bytes) of the variable or parenthesized type- specifier that it precedes Form sizeof var (where var is a declared variable) & sizeof (type) (where type is a C++ data type)

16 Comma Operator Used to string together several expressions. Example
b = (a=3, a+1); 1st step : assigns a the value i.e., 3 2nd step : assigns b the value a+1 i.e., 4

17 Precedence of Operators
++ (post increment), --(post decrement) ++ (Pre increment), -- (pre decrement), - (unary minus), + (unary plus) * (multiply), / (divide), % (modulus) + (add), - (subtract) < (less than), <= (less than or equal to), > (greater than), >= (greater than or equal to) == (equal), != (not equal) && (logical AND) || (logical OR) ?: (conditional Operator) = (assignment), and other assignment operators. (Arithmetic) Comma Operator

18 Expressions Expressions Logical Arithmetic Expressions Expressions
An Expression in C++ is any valid combinations of operators, constants and variables. Expressions Logical Expressions Arithmetic Expressions

19

20 Type Conversion C++ facilitates the following type conversion:-
Process of converting one predefined type into another C++ facilitates the following type conversion:- Implicit type conversion: An implicit type conversion is a conversion that is automatically performed by the compiler without the programmer’s intervention. Explicit type conversion: An explicit type conversion is a user-defined one or that is a forced conversion.

21 Implicit type Conversion
step no. if either’s type is Then resultant type of other operand otherwise 1 long double long double step 2 2 double double step 3 3 float float step 4 4 --- integral promotion takes place followed by step 5 --- 5 unsigned long unsigned long step 6 6 long int and the other is unsigned int long int (provided long int can represent all values of unsigned int) unsigned long int (if all values of unsigned int can’t be represented by long int) step 7 7 long long step 8 8 unsigned unsigned both operands -int

22 Some valid Logical Expressions
x>y (y+z) >+ (x/z) (a+b) > c && (c +d )>a (x) x || y && z (y > x) || (z<y) (-y) (x-y) (x > y) & & (!y <z)

23 C++ Shorthands Form var = var operator expression is same as

24 Examples of C++ Shorthands
equivalent to x = x -10; x * = 3; equivalent to x = x * 3; x/ = 2; equivalent to x = x / 2; X % = z; equivalent to x = x % z;

25


Download ppt "Operators and Expressions"

Similar presentations


Ads by Google