L1 2
L1 2
• 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.
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.
= 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.
++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.
Syntax:
condition ? (statement1) : (statement2);