Software Developer
If else declaration is a conditional announcement. It's far used to check the circumstance and based on the circumstance it executes the loop. Running on the if-else declaration in the c++ language is straightforward. If-else declaration is used when we want to execute the identical piece of code if the given condition is genuine and execute every other piece of the code if the situation is fake. If and the if-else announcement is the same, the best distinction is in if announcement the declaration executes if the condition is real otherwise it stops this system while, an if-else assertion, the declaration is completed if the condition is real otherwise it executes the assertion following the else. In this article, we are going to speak about the conditional announcement in c++ language i.E. If-else announcement.
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.
To understand the concept better, we will discuss some examples to implement the is else in C++:
Program for if-else statement in C++
#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;
}
Right here we've got written an easy application to check whether the number is less than or identical to 50. First, we have got an initialized variable n to 50. If keyword checks the condition i.E. N <= 50. Here we have already initialized n to 50. So right here the circumstance is proper, so it's going to print the given number is much less than or equal to 50.
Program to check even number using if-else statement in C++
#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;
}
Right here we've got written a program to check the even and strange quantity the use of is else statement. Variable n is said and permits the person to enter the fee. Variable n shops the cost entered via the user. If the statement tests the situation n%2 == zero that is declared to check the even quantity. If the wide variety entered through the user satisfies the circumstance, it will print the entered number is even. Else it executes the else assertion and prints entered wide variety is extraordinary.
Program To find the eligibility for voting using if-else statement
#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;
}
Right here we've got written software to test the eligibility for balloting. Variable sn is declared and allows a person to go into their use. Variable stores the age of user into variable n. If assertion exams the circumstance i.E. N >= 18. If the age is more than or identical to 18, it'll print eligible for vote casting. If the age is less than 18, it'll print not eligible for vote casting.