Chapter 1
Chapter 1
What is C++
C++ is a general purpose, case-sensitive, free-form programming language that supports
object-oriented, procedural and generic programming.
C++ is a middle-level language, as it encapsulates both high and low level language
features.
Object-Oriented Programming (OOPs)
C++ supports the object-oriented programming, the four major pillar of object oriented
programming used in C++ are:
1. Inheritance
2. Polymorphism
3. Encapsulation
4. Abstraction
C++ Standard Libraries
Standard C++ programming is divided into three important parts:
o The core library includes the data types, variables and literals, etc.
o The standard library includes the set of functions manipulating strings, files, etc.
o The Standard Template Library (STL) includes the set of methods manipulating a data
structure.
Usage of C++
By the help of C++ programming language, we can develop different types of secured and robust
applications:
o Window application
o Client-Server application
o Device drivers
o Embedded firmware etc
C++ Program
In this tutorial, all C++ programs are given with C++ compiler so that you can easily change the
C++ program code.
File: main.cpp
1. #include <iostream>
<iostream> It is used to define the cout, cin and cerr objects, which correspond to standard output
stream, standard input stream and standard error stream, respectively.
A list of 30 Keywords in C++ Language which are not available in C language are given
below.
/*
This is a multi-line comment
spanning multiple lines
*/
Data Types: Define the type of data a variable can hold. Common C++ data types
include: There are 4 types of data types in C++ language.
float 4 byte
double 8 byte
Statements: Instructions that perform actions. Each statement ends with a semicolon (;)
in C++.
int x = 10; // Declaration and initialization statement
x = x + 5; // Assignment statement
std::cout << x; // Function call statement
1.4. Control Statements
Control statements manage the flow of execution in a C++ program, allowing for decision-
making, looping, and altering the execution path.
1.4.1. Decision/Conditional Statements
These statements enable the program to execute certain blocks of code based on specified
conditions.
C++ if-else
In C++ programming, if statement is used to test the condition. There are various types of if
statements in C++.
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.
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.
1. if(condition){
2. //code if condition is true
3. }else{
4. //code if condition is false
5. }
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. {
1. switch(expression){
2. case value1:
3. //code to be executed;
4. break;
5. case value2:
6. //code to be executed;
7. break;
8. ......
9.
10. default:
11. //code to be executed if all cases are not matched;
12. break;
13. }
Output:
Enter a number:
10
It is 10
Output:
Enter a number:
55
Not 10, 20 or 30
1.4.2. Loops
Loops enable the repeated execution of a block of code as long as a specified condition remains
true.
For loop is used to iterate a part of the program several times. If the number of iteration is
fixed, it is recommended to use for loop than while or do-while loops.
Flowchart:
Output:
1
2
3
4
5
6
7
8
9
10
1. #include <iostream>
2. using namespace std;
3.
4. int main () {
5. for(int i=1;i<=3;i++){
6. for(int j=1;j<=3;j++){
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
1. #include <iostream>
2. using namespace std;
3.
4. int main () {
5. for (; ;)
6. {
7. cout<<"Infinitive For Loop";
8. }
9. }
Output:
for: Ideal for scenarios where the number of iterations is known beforehand.
// Print numbers from 1 to 5
for (int i = 1; i <= 5; ++i) {
std::cout << i << " ";
1. while(condition){
2. //code to be executed
3. }
Flowchart:
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int i=1;
5. while(i<=10)
6. {
7. cout<<i <<"\n";
8. i++;
9. }
Output:
1 4 7
2 5 8
3 6 9
10
Let's see a simple example of nested while loop in C++ programming language.
1. #include <iostream>
2. using namespace std;
3. int main () {
4. int i=1;
5. while(i<=3)
6. {
7. int j = 1;
8. while (j <= 3)
9. {
10. cout<<i<<" "<<j<<"\n";
11. j++;
12. }
13. i++;
14. }
15. }
Output:
1 1 2 1 3 1
1 2 2 2 3 2
1 3 2 3 3 3
1. #include <iostream>
2. using namespace std;
3. int main () {
Output:
The C++ do-while loop is executed at least once because condition is checked after loop
body.
1. do{
2. //code to be executed
3. }while(condition);
Flowchart:
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int i = 1;
5. do{
6. cout<<i<<"\n";
7. i++;
8. } while (i <= 10) ;
9. }
Output:
1 5 9
2 6 10
3 7
4 8
C++ Nested do-while Loop
In C++, if you use do-while loop inside another do-while loop, it is known as nested do-
while loop. The nested do-while loop is executed fully for each outer do-while loop.
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int i = 1;
5. do{
6. int j = 1;
7. do{
8. cout<<i<<"\n";
9. j++;
10. } while (j <= 3) ;
11. i++;
12. } while (i <= 3) ;
13. }
Output:
1. do{
2. //code to be executed
3. }while(true);
Output: