C++ (Unit-2)
*C++ Data Types :-
A data type specifies the type of data that a variable can store such as integer, floating,
character etc.
There are 4 types of data types in C++ language.
Types Data Types
Basic Data Type int, char, float, double, etc
Derived Data Type array, pointer, etc
Enumeration Data Type Enum
User Defined Data Type Structure
Basic Data Types :-
The basic data types are integer-based and floating-point based.
The memory size of basic data types may change according to 32 or 64 bit operating
system.
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
Let's see the basic data types. It size is given according to 32 bit OS.
Data Types Memory Size
Char 1 byte
Short 2 byte
Int 2 byte
short int 2 byte
long int 4 byte
Float 4 byte
Double 8 byte
--------------------------------------------------------------------------------------------------------------------------------------
*C++ Variable
It is a way to represent memory location through symbol so that it can be easily
identified.
Let's see the syntax to declare a variable:
1. type variable_list;
The example of declaring variable is given below:
1. int x;
2. float y;
3. char z;
Here, x, y, z are variables and int, float, char are data types.
We can also provide values while declaring the variables as given below:
1. int x=5,b=10; //declaring 2 variable of integer type
2. float f=30.8;
3. char c='A';
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
Rules for defining variables
A variable can have alphabets, digits and underscore.
A variable name can start with alphabet and underscore only. It can't start with digit.
No white space is allowed within variable name.
A variable name must not be any reserved word or keyword e.g. char, float etc.
Valid variable names:
1. int a;
2. int _ab;
3. int a30;
Invalid variable names:
1. int 4;
2. int x y;
3. int double;
*PROGRAM:-
//WRITE A PROGRAM IN C++ TO DEMONSTRATE THE USE OF VARIABLE.
#include<iostream>
using namespace std;
int main()
int a;
float b;
char c;
long d;
a=29;
b=56;
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
c='H';
d=2548388;
cout<<"\n a="<<a;
cout<<"\n b="<<b;
cout<<"\n c="<<c;
cout<<"\n d="<<d;
return 0;
C++ Expression
C++ expression consists of operators, constants, and variables which are arranged
according to the rules of the language.
Examples of C++ expression:
1. (a+b) - c
2. (x/y) -z
3. 4a2 - 5b +c
4. (a+b) * (x+y)
An expression can be of following types:
o Constant expressions
o Integral expressions
o Float expressions
o Pointer expressions
o Relational expressions
o Logical expressions
o Bitwise expressions
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
o Special assignment expressions
If the expression is a combination of the above expressions, such expressions are
known as compound expressions.
*C++ Operators
An operator is simply a symbol that is used to perform operations. There can be many
types of operations like arithmetic, logical, bitwise etc.
There are following types of operators to perform different types of operations in C
language.
o Arithmetic Operators
o Relational Operators
o Logical Operators
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
o Bitwise Operators
o Assignment Operator
o Unary operator
o Ternary or Conditional Operator
o Misc Operator
Operators in C:-
1] arithmetic operators (+,-,*,/,%)
2] relational operators(<,>,<=,>=)
3] logical operators(&&,||,!)
4]assignment operators (=,+=,-=,*=,|=)
5] increment and decrement operators(++,--)
6] conditional operators(?,:)
7] bitwise operator(&,!,<<,>>,~)
8]special operator (, , size of)
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
In addition C++ introduced some new operators i.e incretion
Operators (<<) and the extraction Operators(>>).
Other new operators are
:: scope resolution operators
::* pointer-to-member operators
->* pointer- to -member operator
‘* pointer-to-member operator
Delete memory release operator
New memory allocation operators
Precedence of Operators in C++
The precedence of operator species that which operator will be evaluated first and
next. The associativity specifies the operators direction to be evaluated, it may be left
to right or right to left.
Let's understand the precedence by the example given below:
1. int data=5+10*10;
The "data" variable will contain 105 because * (multiplicative operator) is evaluated
before + (additive operator).
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
The precedence and associativity of C++ operators is given below:
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Right to left
Shift << >> Left to right
Relational < < = > >= Left to right
Equality == !=/td> Right to left
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Right to left
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right
*PROGRAM:-
//WAP TO CALCULATE THE ADDITION OF 2 NUMBERS.
#include <iostream>
using namespace std;
int main()
int a=19,b=23,c;
c=a+b;
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
cout<<"\n SUM="<<c;
return 0;
*PROGRAM:-
//WAP TO ACCEPT TWO NUMBERS FROM THE KEYBOARD AND CALCULATE THEIR SUM.
#include <iostream>
using namespace std;
int main()
int a,b,c;
cout<<"Enter two numbers \n";
cin>>a>>b;
c=a+b;
cout<<"\nAddition="<<a+b;
return 0;
-------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
CONTROL STRUCTURE
C++ if-else
In C++ programming, if statement is used to test the condition. There are various types
of if statements in C++.
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
o if statement
o if-else statement
o nested if statement
o if-else-if ladder
C++ IF Statement
The C++ if statement tests the condition. It is executed if condition is true.
1. if(condition){
2. //code to be executed
3. }
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
C++ If Example
1. #include <iostream>
2. using namespace std;
3.
4. int main () {
5. int num = 10;
6. if (num % 2 == 0)
7. {
8. cout<<"It is even number";
9. }
10. return 0;
11. }
Output:/p>
It is even number
C++ IF-else Statement
The C++ if-else statement also tests the condition. It executes if block if condition is
true otherwise else block is executed.
Backward Skip 10sPlay VideoForward Skip 10s
1. if(condition){
2. //code if condition is true
3. }else{
4. //code if condition is false
5. }
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
C++ If-else Example
1. #include <iostream>
2. using namespace std;
3. int main () {
4. int num = 11;
5. if (num % 2 == 0)
6. {
7. cout<<"It is even number";
8. }
9. else
10. {
11. cout<<"It is odd number";
12. }
13. return 0;
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
14. }
Output:
It is odd number
C++ If-else Example: with input from user
1. #include <iostream>
2. using namespace std;
3. int main () {
4. int num;
5. cout<<"Enter a Number: ";
6. cin>>num;
7. if (num % 2 == 0)
8. {
9. cout<<"It is even number"<<endl;
10. }
11. else
12. {
13. cout<<"It is odd number"<<endl;
14. }
15. return 0;
16. }
Output:
Enter a number:11
It is odd number
Output:
Enter a number:12
It is even number
C++ IF-else-if ladder Statement
The C++ if-else-if ladder statement executes one condition from multiple statements.
1. if(condition1){
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
2. //code to be executed if condition1 is true
3. }else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. else{
11. //code to be executed if all the conditions are false
12. }
C++ If else-if Example
1. #include <iostream>
2. using namespace std;
3. int main () {
4. int num;
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
5. cout<<"Enter a number to check grade:";
6. cin>>num;
7. if (num <0 || num >100)
8. {
9. cout<<"wrong number";
10. }
11. else if(num >= 0 && num < 50){
12. cout<<"Fail";
13. }
14. else if (num >= 50 && num < 60)
15. {
16. cout<<"D Grade";
17. }
18. else if (num >= 60 && num < 70)
19. {
20. cout<<"C Grade";
21. }
22. else if (num >= 70 && num < 80)
23. {
24. cout<<"B Grade";
25. }
26. else if (num >= 80 && num < 90)
27. {
28. cout<<"A Grade";
29. }
30. else if (num >= 90 && num <= 100)
31. {
32. cout<<"A+ Grade";
33. }
34. }
Output:
Enter a number to check grade:66
C Grade
Output:
Enter a number to check grade:-2
wrong number
+91 9359043819 (pallavidhole50@[Link])
C++ (Unit-2)
---------------------------------------------------------------------------
---------------------------------------------------------------------------
+91 9359043819 (pallavidhole50@[Link])