0% found this document useful (0 votes)
41 views38 pages

L1 2

The document discusses different types of operators in C++ including arithmetic, relational, logical, assignment, bitwise, increment/decrement, and special operators. It provides examples and explanations of how each operator works and the differences between similar operators.

Uploaded by

Shrushti Bhange
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views38 pages

L1 2

The document discusses different types of operators in C++ including arithmetic, relational, logical, assignment, bitwise, increment/decrement, and special operators. It provides examples and explanations of how each operator works and the differences between similar operators.

Uploaded by

Shrushti Bhange
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Input & Ouput

I/O occurs in streams, which are sequences of


bytes.
• Input operation.
• Output operation.
Header files for I/O operation
• Iostream
• Iomanip
Setprecision: This function sets the precision for decimal or float values.

• fstream
Standard Output Stream (cout)
• cout
• << insertion or put to operator
• endl
1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 char str[] = "Focus";
8 char str1[] = "Academy";
9 cout << str << endl << str1;
10 return 0;
11 }
12
13
14
15
Output
Focus
Academy
Standard Input Stream (cin)
• cin
• >> extraction or get from operator
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int value;
7 cin >> value;
8 cout << value;
9 return 0;
10 }
11
12
13
14
15
Output
5
5
Type Conversion
Converting one predefined type into another
• Implicit Type Conversion
• Explicit Type Conversion
Implicit Type Conversion
• Done by compiler on its own
• Takes place in an expression when more than one data type is present
• All the data types of variables are upgraded to data type of variable with largest
data type
• bool -> char -> short int -> int -> unsigned int -> long -> unsigned-> long
long -> float -> double -> long double
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int x = 20;
7 char y = 'c';
8 x = x + y; // y implicitly converted into int
9 float z = x + 1.5; // x implicitly converted into float
10 cout << "x = " << x << endl
11 << "y = " << y << endl
12 << "z = " << z << endl;
13
14 return 0;
15 }
Output
x = 119
y = c
z = 120.5
Explicit Type Conversion
User can typecast the result to make it of a particular data type.
• Converting by assignment
• Conversion using cast operator
CONVERTING BY ASSIGNMENT
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 double x = 7.3;
7 int sum = (int)x + 3; // explicit conversion from double to int
8 cout << "Sum = " << sum;
9 return 0;
10 }
11
12
13
14
15

Output
Sum = 10
CONVERSION USING CAST OPERATOR
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 float f = 9.5;
7 int b = static_cast<int>(f); // using cast operator
8 cout << b;
9 }
10
11
12
13
14
15

Output
9
Operators
• An operator is a symbol that tells the compiler to perform certain mathematical or
logical manipulation.
• Operators are used in program to manipulate data and variables.

a + b a and b -> operands


+ -> operator

C++ Operators are classified into several categories.


Types of Operators
• Arithmetic Operators
• Relational Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Increment and Decrement Operators
• Special Operators
Arithmetic Operators
An arithmetic operator performs
Example
mathematical operations such as Operators
a = 10, b = 5
addition, subtraction and + a + b = 15
multiplication on numerical values - a–b=5
* a * b = 50
(constants and variables)
/ a /b=2
% a%b=0
If you have more than one arithmetic operator in an expression, which operator will
execute first?

B O D M A S

/ * + -
of

Brackets ()
What is the difference between % and /
operator?
/ -> Quotient

% -> Remainder
Relational Operators
• A relational operator checks the relationship between two operands. If the relation
is true, it returns 1; if the relation is false, it returns value 0.
• Relational operators are used in decision making and loops.
• It is used to form a condition.
Relational Operators
Logical Operators
• An expression containing logical operator returns either 0 or 1 depending upon
whether expression results true or false.
• It is used to combine the conditions when you have more than one.
Logical Operators
OPERATOR MEANING OF OPERATOR EXMPLE

&& Logical AND. True only if all If c = 5 and d = 2 then, expression ((c = 5)
operands are true. && (d>5)) equals to 0.

|| Logical OR. True only if either If c = 5 and d = 2 then, expression ((c = 5)


one operand is true. || (d>5)) equals to 1.

! Logical NOT. True only if the If c = 5 then expression !(c == 5) equals to


operand is 0. 0.
Assignment Operators
Right side value will be assigned to the left side variable
Operator Example Meaning

= a=b a=b

+= a += b a = a+b

-= a -= b a = a-b

*= a *= b a = a*b

/= a /= b a = a/b

%= a %= b a = a%b
Bitwise Operators
a b a&b a|b a^b

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0
1 // Program comment
2 #include <iostream>
3 using namespace std;
4 int main()
5 {
x = 1 0 1 1
6 int x = 11;
7 int y = 7; y = 0 1 1 1
8 int a = 8;
x | y = 1 1 1 1
9 int b = 1;
10 cout << x | y ;
11 cout << x & y ;
12 cout << x ^ y ;
13 cout << (a << b);
14 cout << (a >> b);
15 return 0;
16 }
17
18
19
20
21
22
1 // Program comment
2 #include <iostream>
3 using namespace std;
4 int main()
5 { x = 1 0 1 1
6 int x = 11;
y = 0 1 1 1
7 int y = 7;
8 int a = 8; x & y = 0 0 1 1
9 int b = 1;
10 cout << x | y ;
11 cout << x & y ;
12 cout << x ^ y ;
13 cout << (a << b);
14 cout << (a >> b);
15 return 0;
16 }
17
18
19
20
21
22
1 // Program comment
2 #include <iostream>
3 using namespace std;
4 int main()
5 { x = 1 0 1 1
6 int x = 11;
y = 0 1 1 1
7 int y = 7;
8 int a = 8; x ^ y = 1 1 0 0
9 int b = 1;
10 cout << x | y ;
11 cout << x & y ;
12 cout << x ^ y ;
13 cout << (a << b);
14 cout << (a >> b);
15 return 0;
16 }
17
18
19
20
21
22
Increment and Decrement Operators
++ Increment the value by 1.

-- Decrement the value by 1.


What is the difference between ++a and a++?

++a a++
• Pre – increment • Post - increment
• First increment by 1 then, it returns • First return the original value
the value then, it is incremented by 1.

Similarly, --a and a--


Special Operators
1) Sizeof()
2) &
3) *
4) Ternary( ? : )
Special Operators
1) sizeof() operator will returns the number of memory bytes allocated for the data
(constant, variables, array, structure etc).
1 // Program
2 #include <iostream>
3 using namespace std;
4
5 int main()
6 {
7 int a;
8 float b;
9 double c;
10 char d;
11 cout << sizeof(a));
12 cout << sizeof(b));
13 cout << sizeof(c));
14 cout << sizeof(d));
15 return 0;
16 }
17
18
19
20
21
22
Special Operators
2) & is used to get the address of the variable
3) * is used to get the value of the variable pointed by the pointer
Special Operators
4) Ternary Operator is also known as conditional operator. It works on three
operands.

Syntax:
condition ? (statement1) : (statement2);

E.g: 10 < 20 ? printf(" True ") : printf(" False ");


Operators Application
Operators Application
THANK YOU

You might also like