Trending October 2023 # If Else Statement In C++ # Suggested November 2023 # Top 13 Popular | Nhunghuounewzealand.com

Trending October 2023 # If Else Statement In C++ # Suggested November 2023 # Top 13 Popular

You are reading the article If Else Statement In C++ updated in October 2023 on the website Nhunghuounewzealand.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 If Else Statement In C++

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

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:

Output:

Example #2

Program to check even number using if else statement in C++

Code:

using namespace std; int main() { int n; cout << “Enter a number “; 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:

using namespace std; int main() { int n; cout << “Enter a age “; { cout << “Eligible for voting”; } else { cout << “Not eligible for voting”; } return 0; }

Explanation 

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–

You're reading If Else Statement In C++

Update the detailed information about If Else Statement In C++ on the Nhunghuounewzealand.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!