1. Introduction to C++
C++ is a powerful and versatile programming language used to
develop various applications, including games, web browsers, and
operating systems.
2. Basics of C++ programming language
Data types and
variables
Understanding data types
and variables is
fundamental to writing
efficient and error-free C++
code.
Control flow statements
Learn about the various
control flow statements
such as if, else, switch, and
loops.
Functions in C++
Explore the concept of
functions and how to
create and use them in C++.
3. Data types and variables in C++
1 Integer
Used for whole numbers
2 Boolean
Represents true or false
3 Float
Used for decimal numbers
4 Character
Stores single character
Variables : https://www.w3schools.com/cpp/cpp_variables.asp
Data Types : https://www.w3schools.com/cpp/cpp_data_types.asp
4. Operators
An operator is a symbol that operates on a value to perform specific mathematical or logical
computations. They form the foundation of any programming language. In C++, we have
built-in operators to provide the required functionality.
Operators in C++ can be classified into 6 types
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Ternary or Conditional Operators
5. Arithmetic Operators
Arithmetic Operators can be classified into 2 Types:
A) Unary Operators: These operators operate or work with a single operand. For example:
Increment(++) and Decrement(–) Operators.
a++ is 10
++a is 12
b-- is 15
--b is 13
6. Binary Operators: These operators operate or work with two operands.
For example: Addition(+), Subtraction(-), etc.
7. Relational Operators
These operators are used for the comparison of the values of two operands. For example, ‘>’
checks if one operand is greater than the other operand or not, etc. The result returns a
Boolean value, i.e., true or false
8. Logical Operators
These operators are used to combine two or more conditions or constraints or to
complement the evaluation of the original condition in consideration. The result returns a
Boolean value, i.e., true or false.
9. Bitwise Operators
These operators are used to perform bit-level operations on the operands. The operators are
first converted to bit-level and then the calculation is performed on the operands.
Mathematical operations such as addition, subtraction, multiplication, etc. can be performed
at the bit level for faster processing.
10. Assignment Operators
These operators are used to assign value to a variable. The left side operand of the
assignment operator is a variable and the right side operand of the assignment operator is a
value. The value on the right side must be of the same data type as the variable on the left
side otherwise the compiler will raise an error.
11. Control flow statements in C++
Selection
Using if, else, if-else.
Iterations
Using for, while, and do-while loops
Jump
Using break, and continue statements
12. IF Statement
When you know exactly how many times you want to loop through a block of code
Syntax
if(condition)
{
// Statements to execute if
// condition is true
}
14. Working of if statement in C++
1. Control falls into the if block.
2. The flow jumps to Condition.
3. Condition is tested.
1. If Condition yields true, goto Step 4.
2. If Condition yields false, goto Step 5.
4. The if-block or the body inside the if is executed.
5. Flow steps out of the if block.
15. Example 1
Program demonstrates the use of if statement .
• C++
#include using namespace std;
int main() { int i = 10;
if (i < 15) {
cout << "10 is less than 15 n";
}
cout << "I am Not in if";
}
16. if else Statement
The if statement alone tells us that if a condition is true it will execute a block of statements
and if the condition is false it won’t. But what if we want to do something else if the condition
is false.
Syntax
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
17. Example : Check the greatest of three numbers
Algorithm
Assume that a, b, and c are three given numbers:
If ( a < b ) evaluates to true, then
1. If ( c < b ) is true, then b is greatest.
2. else c is greatest.
If ( a < b ) evaluates to false, then
1. If ( c < a ) is true, then a is greatest.
2. else c is greatest.
18. Implementation
#include using namespace std;
int main() { // Assume that a, b, and c are three given numbers
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
if (a < b) {
if (c < b) {
cout << "The greatest number is: " << b << endl;
} else {
cout << "The greatest number is: " << c << endl;
}
} else {
if (c < a) {
cout << "The greatest number is: " << a << endl;
} else {
cout << "The greatest number is: " << c << endl;
}
}
return 0;
}
20. Working of if-else statement
1. Control falls into the if block.
2. The flow jumps to Condition.
3. Condition is tested.
1. If Condition yields true, goto Step 4.
2. If Condition yields false, goto Step 5.
4. The if-block or the body inside the if is executed.
5. The else block or the body inside the else is executed.
6. Flow exits the if-else block.
21. Example :
Program demonstrates the use of if else statements.
// C++ program to illustrate if-else statement
#include using namespace std;
int main() { int i = 20;
// Check if i is 10
if (i == 10)
cout << "i is 10";
// Since is not 10
// Then execute the else statement
else
cout << "i is 20n";
cout << "Outside if-else block";
return 0;
}
22. Nested if-else Statement
Nested if-else statements are those statements in which there is an if statement inside
another if else. We use nested if-else statements when we want to implement multilayer
conditions(condition inside the condition inside the condition and so on).
Syntax of Nested if-else
if(condition1)
{
// Code to be executed
if(condition2)
{
// Code to be executed
}
else
{
// Code to be executed
}
}
else
{
// code to be executed
}
23. Jump statements
Jump statements are used to manipulate the flow of the program if some conditions are met.
It is used to terminate or continue the loop inside a program or to stop the execution of a
function.
Types of Jump Statements in C++
In C++, there is four jump statement
1. break
2. continue
24. continue in C++
The C++ continue statement is used to execute other parts of the loop while skipping some
parts declared inside the condition, rather than terminating the loop, it continues to execute
the next iteration of the same loop. It is used with a decision-making statement which must
be present inside the loop.
25. // C++ program to demonstrate the
// continue statement
// Driver code
int main() { for (int i = 1; i < 10; i++) {
if (i == 5)
continue;
cout << i << " ";
}
return 0;
}
26. break in C++
The C++ break statement is used to terminate the whole loop if the condition is met. Unlike
the continue statement after the condition is met, it breaks the loop and the remaining part
of the loop is not executed.
The break statement is used with decision-making statements such as if, if-else, or switch
statement which is inside the for loop which can be for loop, while loop, or do-while loop. It
forces the loop to stop the execution of the further iteration.
27. // C++ program to demonstrate the
// break statement
// Driver Code int main() { for (int i = 1; i < 10; i++) {
// Breaking Condition
if (i == 5)
break;
cout << i << " ";
}
return 0;
}
28. Loops
In Programming, sometimes there is a need to perform some operation more than once or
(say) n number of times. Loops come into use when we need to repeatedly execute a block
of statements.
For example: Suppose we want to print “Hello World” 10 times.
// C++ program to Demonstrate the need of loops
#include <iostream>
using namespace std;
int main()
{
cout << "Hello Worldn";
cout << "Hello Worldn";
cout << "Hello Worldn";
cout << "Hello Worldn";
cout << "Hello Worldn";
return 0;
}
30. for Loop
for loop is an entry-controlled loop that is used to execute a block of code repeatedly for
the specified range of values. Basically, for loop allows you to repeat a set of instructions for
a specific number of iterations.
The syntax of for loop in C++ is shown below:
for ( initialization; test condition; updation)
{
// body of for loop
}
32. // C++ program to illustrate for
loop to print numbers from // 1 to
n
#include using namespace std;
int main() {
// initializing n (value upto which you want to print // numbers
int n = 5;
int i; // initialization of loop variable
for (i = 1; i <= n; i++)
{
cout << i << " ";
}
return 0;
}
33. While Loop
While Loop in C++ is used in situations where we do not know the exact number of
iterations of the loop beforehand. The loop execution is terminated on the basis of the test
condition. Loops in C++ come into use when we need to repeatedly execute a block of
statements. During the study of the ‘for’ loop in C++, we have seen that the number of
iterations is known beforehand, i.e. the number of times the loop body is needed to be
executed is known to us.
35. Do/While Loop
Loops come into use when we need to repeatedly execute a block of statements. Like while
the do-while loop execution is also terminated on the basis of a test condition. The main
difference between a do-while loop and a while loop is in the do-while loop the condition is
tested at the end of the loop body, i.e do-while loop is exit controlled whereas the other two
loops are entry-controlled loops.
38. Functions in C++
1
Function Declaration
Declaring the function prototype
2 Function Definition
Implementing the function logic
3
Function Call
Using the function in the program
39. Functions
A function is a set of statements that takes input, does some specific computation, and
produces output. The idea is to put some commonly or repeatedly done tasks together to
make a function so that instead of writing the same code again and again for different
inputs, we can call this function.
In simple terms, a function is a block of code that runs only when it is called.