Introduction to if else Statement in C++
If else statement is a conditional statement. It is used to check the condition and based on the condition it executes the loop. Working of if else statement in C++ language is easy. if-else statement is used when we need to execute the same piece of code, if the given condition is true and execute another piece of the code if the condition is false. if and the if-else statement is the same, the only difference is in if statement the statement executes if the condition is true or else it stops the program whereas, an if-else statement, the statement is executed if the condition is true or else it executes the statement following the else. In this article, we are going to discuss the conditional statement in C++ language i.e. if else statement.
Syntax of if else Statement in C++ Language
if(condition)
{
statement;
}
else
{
statement;
}
if and else are the two keywords used to declare the if else statement. condition is a parameter that is used to evaluate the decision. if statements are declared inside the parenthesis of if and else statement is declared inside the parenthesis of else.
Flowchart of if else statement in C++
Below is the flow chart that defines the working of the if-else statement in a stepwise manner:
Here the condition is defined by using the diamond sign. The flow chart states that first it checks the condition and if the condition is true it transfers the flow control to the if statement and if the condition is false, it transfers the flow control to else statement.
How if else statement works in C++?
As we have discussed earlier the concept is easy to understand. In if else statement, first, it checks the condition and if the condition is true, the code inside the if the body is executed and else statement is skipped. and if the condition is false then it skips the if statement and executes the else body.
Examples
To understand the concept better, we will discuss some examples to implement the is else in C++:
Example #1
Program for if else statement in C++
Code:
#include <iostream>
using namespace std;
int main()
{
int n = 50;
if(n <= 50)
{
cout << "Given number is less than or equal to 50";
}
else
{
cout << "Given number is greater than 50";
}
return 0;
}
Explanation
Here we have written a simple program to check whether the number is less than or equal to 50. First, we have an initialized variable n to 50. if keyword checks the condition i.e. n <= 50. here we have already Initialized n to 50. So here the condition is true, so it will print the Given number is less than or equal to 50.
Output:
if we change the value of n to 75, the condition becomes false and it will execute the else statement And then it will print the given number is greater than 50.
Output:
Example #2
Program to check even number using if else statement in C++
Code:
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a number ";
cin >> n;
if(n%2 == 0)
{cout << "Entered number is even";
}
else
{
cout << "Entered number is odd";
}
return 0;
}
Explanation
Here we have written a program to check the even and odd number using is else statement. variable n is declared and allows the user to enter the value. variable n stores the value entered by the user. If statement checks the condition n%2 == 0 that declared to check the even number. if the number entered by the user satisfies the condition, it will print the Entered number is even. Else it executes the else statement and prints Entered number is odd.
Output:
Example #3
Program To find the eligibility for voting using if else statement
Code:
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a age ";
cin >> n;
if(n >= 18)
{
cout << "Eligible for voting";
}
else
{
cout << "Not eligible for voting";
}
return 0;
}
Explanation
Here we have written a program to check the eligibility for voting. Variable sn is declared and allow a user to enter their use. Variable stores the age of user into variable n. if statement checks the condition i.e. n >= 18. If the age is greater than or equal to 18, it will print Eligible for voting. if the age is less than 18, it will print Not eligible for voting.
Output:
Recommended Articles
This is a guide to if else Statement in C++. Here we discuss how if else statement works in C++? along with the flowchart, and respective examples. You can also go through our other related articles to learn more–