if-else Statement in C++

if-else Statement in C++
Techiio-author
Written by Sagar RabidasFebruary 8, 2022
6 min read
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss the if-else Statement in C++

if-else Statement in C++

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.

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.

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

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.

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

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.

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

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.

C
C++
ifelseStatement
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance