C++ if, if...else, if else if and Nested if...else

C++ provides several ways to make decisions in a program, such as if, if...else, if else if, and nested if...else statements. These statements are used to control the flow of a program based on certain conditions. In this article, we will discuss these statements in detail with examples and their output.

If Statement

The "if" statement is the simplest form of decision-making in C++. It allows you to execute a block of code only if a certain condition is met. The basic syntax of the if statement is as follows:

1if (condition) 2{ 3 // code to be executed if the condition is true 4}

Here, the condition is evaluated. If the condition is true, the code inside the curly braces is executed. If the condition is false, the code inside the braces is skipped and the program continues to execute the next statement.

Let's take an example to understand the if statement:

1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int num = 10; 8 9 if (num > 5) 10 { 11 cout << "num is greater than 5" << endl; 12 } 13 14 return 0; 15}

In the above example, the condition num > 5 is true, so the output will be:

1num is greater than 5

If...Else Statement

The "if...else" statement is an extension of the "if" statement. It allows you to execute a block of code if the condition is true, and another block of code if the condition is false. The basic syntax of the if...else statement is as follows:

1if (condition) 2{ 3 // code to be executed if the condition is true 4} 5else 6{ 7 // code to be executed if the condition is false 8}

Let's take an example to understand the if...else statement:

1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int num = 3; 8 9 if (num > 5) 10 { 11 cout << "num is greater than 5" << endl; 12 } 13 else 14 { 15 cout << "num is not greater than 5" << endl; 16 } 17 18 return 0; 19}

In the above example, the condition num > 5 is false, so the output will be:

1num is not greater than 5

If...Else if...Else Statement

The "if...else if...else" statement is used when there are multiple conditions to be checked. It allows you to execute a different block of code for each condition. The basic syntax of the if...else if...else statement is as follows:

1if (condition1) 2{ 3 // code to be executed if condition1 is true 4} 5else if (condition2) 6{ 7 // code to be executed if condition1 is false and condition2 is true 8} 9else if (condition3) 10{ 11 // code to be executed if condition1 and condition2 are false and condition3 is true 12} 13... 14else 15{ 16 // code to be executed if all conditions are false 17}

Let's take an example to understand the if...else if...else statement:

1#include <iostream 2using namespace std; 3 4int main() 5{ 6 int num = 8; 7 8 if (num > 10) 9 { 10 cout << "num is greater than 10" << endl; 11 } 12 else if (num == 10) 13 { 14 cout << "num is equal to 10" << endl; 15 } 16 else 17 { 18 cout << "num is less than 10" << endl; 19 } 20 21 return 0; 22}

In the above example, the condition num > 10 is false, the second condition num == 10 is also false, so the code in the else block is executed, and the output will be:

1num is less than 10

Nested If...Else Statement

The "nested if...else" statement is used when you want to check multiple conditions within another condition. It allows you to check for multiple conditions within a single if...else statement. The basic syntax of the nested if...else statement is as follows:

1if (condition1) 2{ 3 // code to be executed if condition1 is true 4 5 if (condition2) 6 { 7 // code to be executed if condition1 is true and condition2 is true 8 } 9 else 10 { 11 // code to be executed if condition1 is true and condition2 is false 12 } 13} 14else 15{ 16 // code to be executed if condition1 is false 17}

Let's take an example to understand the nested if...else statement:

1#include <iostream> 2using namespace std; 3 4int main() 5{ 6 int num1 = 5, num2 = 10; 7 8 if (num1 < num2) 9 { 10 cout << "num1 is less than num2" << endl; 11 12 if (num1 == 5) 13 { 14 cout << "num1 is equal to 5" << endl; 15 } 16 } 17 else 18 { 19 cout << "num1 is not less than num2" << endl; 20 } 21 22 return 0; 23}

In the above example, the first condition num1 < num2 is true, so the code in the first if block is executed. Then, the second condition num1 == 5 is also true, so the code in the second if block is executed. The output will be:

1num1 is less than num2 2num1 is equal to 5