C++ if else Statement
In this article, we will learn how decision-making is done in C++ by using conditional statements. These statements work on the logic that a block of code will be executed only if a certain condition is true, else not.
We will be covering C++ if else Statement sections today:
- C++ if Statement
- C++ if… else Statement
- C++ if…else if… else Statement
- C++ Nested if… else Statements
C++ if Statement Syntax
Here’s the syntax for a simple if statement in C++:
if (test expression) { statement(s)}
The test expression, or condition, is evaluated and the statement(s) is/are executed if the condition is True. In case the condition is False, the statement(s) is/are not executed.
In C++, we use curly bracket { } to indicate the start and end of the loop body.
NOTE:
- In C++, non-zero values are interpreted as True, whereas None and 0 are interpreted as False.
- C++ is case-sensitive. The if is in lowercase letters, using If or IF will result in an error.
Here, we have illustrated the if Statement flowchart –
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
C++ if Statement Implementation Examples
Example 1:
#include <iostream>using namespace std; int main() { int x = 18; if (x%2 == 0) { cout << "The number is even "; } return 0;}
Output 1:
The condition (x % 2) == 0 is true because (18 % 2) would give 0 (as remainder). So, the body of the statement is executed, and the output is displayed.
Example 2:
#include <iostream>using namespace std; int main() { int x = 32; int y = 50; if (x > y) { cout << "First number is greater "; } return 0;}
No Output.
The condition x > y is false because 32 is not greater than 50. So, the body of the statement is not executed, and no output is displayed.
C++ if… else Statement Syntax
Here’s the syntax for the if…else statement in C++:
if (test expression) { Body of if}else { Body of else}
The if…else statement in C++ evaluates the condition and executes the body of if only when the condition holds True. In case the condition returns False, the body of else will be executed.
Let’s look at the if…else Statement flowchart –
C++ if… else Statement Implementation Examples
Example 1:
#include <iostream>using namespace std; int main() { int age = 17; if (age > 18) { cout << "The person is an adult "; } else { cout << "The person is not an adult "; } return 0;}
Output 1:
The given test expression age > 18 is not true because the input age is 17. Hence, the body of the else statement is executed.
Example 2:
#include <iostream>using namespace std; int main() { int a = 2; if (a >= 0) { cout << "This is a whole number "; } else { cout << "Not a whole number "; } return 0;}
Output 2:
We are already aware that whole numbers are integer values > = 0. The given input is 2. So, the condition holds true. Therefore, the body of the if statement is executed.
C++ if… else if… else Statement Syntax
Here’s the syntax for the if… else if… else statement in C++:
if (test expression 1) { Body of if}else if (test expression 2) { Body of else-if}else { Body of else}
- The if… else if… else statement first checks the condition for the if statement and executes the body of if only when its condition holds True.
- Otherwise, it moves on to the condition of the else if statement. In case the else if condition is True, the body of else if is executed. Note that, there can be multiple else if statements.
- Lastly, in case all the conditions return False, the body of else will be executed.
Let’s look at the if… else if… else Statement flowchart –
C++ if… else if… else Statement Implementation Example
Example:
#include <iostream>using namespace std; int main() { int age = 11; if (age >= 13 && age < 20) { cout << "Person is a teenager "; } else if (age >= 0 && age < 13) { cout << "Person is a prepubescent "; } else { cout << "Person is an adult "; } return 0;}
Output:
As we can see from the code above, the input age is 11. So, the else if condition holds true. Therefore, the body of the else if statement is executed.
C++ Nested if… else Statements
First let us understand what nesting means – when you use any form of conditional statements or loops inside another set of statements or loops, it is known as nesting in the programming world.
In this case, any number of if…else statements can be nested inside one another.
But because nesting increases the complexity of the codes, it is avoided unless necessary.
Example Syntax
Here’s an example syntax for nested if…else statements in C++:
if (test expression 1) { //outer if statement(s) //inner if-else statements if (test expression 1.1) { Body of nested if } else if (test expression 1.2) { Body of nested else-if } else { Body of nested else }}else { //outer if statement(s) }
- In the above snippet, the outer if statement consists of nested if…else if…else statements.
- If the condition for the outer if statement is True, only then will the nested if…else if…else statements be executed.
- The body of nested if will be executed in case the nested if condition is True.
- Otherwise, the body of nested else if will be executed in case the nested else if condition is True.
- Otherwise, the body of nested else will be executed.
- In case the condition for the outer if statement is False, the body of outer else will be executed.
Nested if… else Statements Implementation Example
Example:
Consider the following example of a basketball camp being held for girls. Only people who identify as females under the age of 18 are allowed to join the camp and learn swimming free of cost.
Let us see how we will use the nested if…else statements in C++ to filter the people who are interested in attending the camp:
//Basketball Camp #include <iostream>using namespace std; int main() { int gen; int age; cout << "Enter gender (0 for Male, 1 for Female): "; cin >> gen; cout << "Enter age: "; cin >> age; if (gen == 1) { if (age >= 13 && age < 19) { cout << "Person is a teenager: allowed in group 1 "; } else if (age >= 0 && age < 13) { cout << "Person is a prepubescent: allowed in group 2 "; } else { cout << "Person is an adult: not allowed "; } } else { cout << "Person does not identify as female. Not allowed. "; } return 0;}
Output:
As we can see from the code above, the if condition evaluates whether the person is female or not. Since this condition returns True, we move on to the nested if…else if…else statements.
Then, we evaluate the age of the female candidate, since the input age is greater than 13 but less than 18, the female is clearly a teenager and hence, the body of the nested if is executed.
Endnotes
Hope this article was helpful for you to understand the different variations of the if… else statements and how they are used to perform decision-making in C++. If you want to learn more about C++ and solidify your basics, you can explore our articles on C++.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio