What is an If else statement in C++ programming
If else statement in C++ programming language is generally used for testing the different conditions of the program. To make some decisions in c++ programming, programmers get the ideas of the decision from these statements and execute the next block of the code. In this blog post, we'll take a closer look at how if else statements work and explore how they are implemented in various programming contexts. Whether you're just getting started in programming or want to brush up on your skills, understanding the basics of if else will undoubtedly prove helpful!
Types of If else statements in c++ language
The types of If else statements in C++ are
- if statement in C++ programming
- if else statements in C++ programming
- nested if else statements in C++ programming
- if else if ladder in C++ programming
If statements in C++
If statements in c++ are one of the most simple statements for making a decision in a program. It is used for deciding whether a certain block of the statement of a single statement will be executed or not. If the conditions are turned out to be true then it will execute the statements otherwise not.
Syntax
if(condition)
{
// Statements to execute if
// condition is true
}
Example
// C++ program to understand If statement
#include <iostream>
using namespace std;
int main()
{
int i = 11;
if (i > 15)
{
cout << "11 is greater than 15";
}
cout << "I am Not in if";
}
Output
I am Not in if
If else statements in C++
If the value of If statements returned to be a false statement then it needs an else statement to execute the program. The programmers can use else statements with If statements for executing a single code of statement or any block of code of the statements when the condition is false.
Syntax
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Example
// C++ program to understand the if-else statement
#include <iostream>
using namespace std;
int main()
{
int A = 16;
if (A < 15)
cout << "A is smaller than 15";
else
cout << "A is greater than 15";
return 0;
}
Output
A is greater than 15
Nested If else statement in C++
A nested if else statement in c++ programming language is mainly targeted at another if statement which means an if statement inside another if statement. This is how nested if else statements execute any block of codes in the c++ programming language.
Syntax
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
Example
// C++ program to understand nested-if statement
#include <iostream>
using namespace std;
int main()
{
int A = 10;
if (A == 10)
{
// First if statement
if (A < 15)
cout << "A is smaller than 15\n";
// Nested - if statement
// Will only be executed if
// statement above is true
if (A < 12)
cout << A is smaller than 12 too\n";
else
cout << "A is greater than 15";
}
return 0;
}
Output
A is smaller than 15
A is smaller than 12 too
If else if ladder statements in C++
This if else if ladder statements in c++ give the user multiple options to execute the program. If the program is executed in a top-down format and one of the conditions control is turned out to be true then it is associated with the if statement. Then the other rest of the program will use the else if ladder to execute the program. After that, if none of the conditions are turned out to be true then it will execute the final else statement of the code block in the c++ programming language.
Syntax
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
Example
// C++ program to understand if-else-if ladder
#include <iostream>
using namespace std;
int main()
{
int A = 20;
if (A == 10)
cout << "A is 10";
else if (A == 15)
cout << "A is 15";
else if (A == 20)
cout << "A is 20";
else
cout << "A is not present";
}
Output
A is 20
What are switch statements in C ++?

switch(expression)
{
case value1:
//code to be executed;
break;
case value2:
//code to be executed;
break;
......
default:
//code to be executed if all cases are not matched;
break;
}
#include <iostream>
using class std;
int main ()
{
int num;
cout<<"Enter a number to check grade:";
cin>>num;
switch (num)
{
case 20: cout<<"It is 20"; break;
case 30: cout<<"It is 30"; break;
case 40: cout<<"It is 40"; break;
default: cout<<"Not 20, 30 or 40"; break;
}
}
Enter a number:
20
It is 20
Enter a number:
68
Not 20, 30 or 40
Summary
This article gives a vast idea about if else statements in C++ language including its various types with syntax and examples. If you've understood everything we went over in this blog post, congratulations! C++ if-else and switch statements provide you with different options to control the flow of a program based on user input or other conditions. If-Else Statements are executed from top to bottom, one after the other. Switch Statements test for a particular value and execute the corresponding case. You're now ready to start using if-else statements like a pro. Just remember the basic format and key points we covered, and you'll be writing if-else statements in your sleep in no time.
Take our free skill tests to evaluate your skill!

In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.