C++ if, if-else and Nested if…else | Examples

With the help of examples, we will learn how to use the if-else statement to write decision-making programs in this tutorial. In previous we taught for loop and while loop with example.

if-else

The if..else statement is used in computer programming to execute one block of code under specific circumstances and a different block of code under other circumstances.

You are already aware that C++ supports the standard mathematical logical conditions:

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b

These conditions can be used to perform various actions based on various choices.

In C++, the following conditional statements are applicable:

  • If you want a section of code to run only when a certain condition is true, use the if statement.
  • If the same condition is false, use else to specify that a different block of code should be run.
  • When testing a new condition if the original one is false, use the else if statement.
  • To specify a variety of different code blocks to be executed, use switch.

There are three different kinds of if…else statements in C++.

  1. if statement
  2. if...else statement
  3. if...else if...else statement

C++ if Statement

To specify a block of C++ code to be executed if a condition is true, use the if statement.

if (condition) {
  // body of if statement
}
  • The body of the if statement’s code is executed if the condition evaluates to true.
  • If the condition evaluates to false, the code in the body of the if statement is skipped.

Note: The word “if” is written in lowercase. If or IF will produce an error if they are written in uppercase.

// Program to print positive number entered by the user
//geektocode.com

#include <iostream>
using namespace std;

int main() {
  int number;
  cout << "Enter an integer: ";
  cin >> number;
  // checks the number is positive
  if (number > 0) {
    cout << "Entered a positive integer: " << number << endl;
  }
  cout << "This statement is executed.";

  return 0;
}
OUTPUT:
Enter an integer: 44
Entered a positive integer: 44
This statement is executed.

The statement inside the body of if is executed when the user types in 44, evaluating the condition number > 0 to true.

OUTPUT:
Enter an integer: -66
This statement is executed.

The condition number > 0 is assessed to false when the user enters -66, and the statement inside the body of the if is not carried out.


C++ if…else Statement

Use the else statement to define which code block should be executed if the condition is false.

if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

If the test results are positive,

  • The body of the if statement’s code is run.
  • The execution of the code included in the else body is skipped.

Suppose that the assumption is incorrect.

  • The code included in else’s body is run.
  • Execution of the if’s body of code is skipped.
// Program to check whether an integer is positive or negative
// This program considers 0 as a positive number

#include <iostream>
using namespace std;

int main() {
  int number;
  cout << "Enter an integer: ";
  cin >> number;

  if (number >= 0) {
    cout << "You entered a positive integer: " << number << endl;
  }
  else {
    cout << "You entered a negative integer: " << number << endl;
  }

  return 0;
}
OUTPUT:
Enter an integer: 77
You entered a positive integer: 77
OUTPUT:
Enter an integer: -99
You entered a negative integer: -99

C++ if..else if…else Statement

If the first criterion is untrue, a different criterion should be stated in the otherwise if clause.

if (condition1) {
  // code block 1
}
else if (condition2){
  // code block 2
}
else {
  // code block 3
}

Here,

  • The code block in question 1 is run if condition 1 evaluates to true.
  • Condition 2 is assessed if condition 1 evaluates to false.
  • Block 2 of the code is run if condition 2 is true.
  • The code block 3 is run if condition 2 is true.
// Program to check whether an integer is positive, negative or zero

#include <iostream>
using namespace std;

int main() {

  int number;

  cout << "Enter an integer: ";
  cin >> number;

  if (number > 0) {
    cout << "You entered a positive integer: " << number << endl;
  } 
  else if (number < 0) {
    cout << "You entered a negative integer: " << number << endl;
  } 
  else {
    cout << "You entered 0." << endl;
  }

  return 0;
}
OUTPUT:
Enter an integer: 99
You entered a positive integer: 99
OUTPUT 2:
Enter an integer: -99
You entered a negative integer: -99
OUTPUT 3:
Enter an integer: 0
You entered 0.

C++ Nested if..else Statement

The use of an if statement inside of another if statement is occasionally necessary. This is referred to as a “nested if statement.”

See it as a series of if statements. A first, outer if statement is present, and an additional, inner if statement is contained within it. The syntax is:

// outer if statement
if (condition1) {

  // statements

  // inner if statement
  if (condition2) {
    // statements
  }
}
  • If necessary, we can supplement the inner if statement with else and else if statements.
  • The outer otherwise or else if statements can likewise be put inside the inner if statement (if they exist).
  • Several layers of if statements can be nested.
// C++ program to find if an integer is positive, negative or zero
// using nested if statements

#include <iostream>
using namespace std;

int main() {

  int num;
    
  cout << "Enter an integer: ";  
   cin >> num;    

  // outer if condition
  if (num != 0) {
        
    // inner if condition
    if (num > 0) {
      cout << "The number is positive." << endl;
    }
    // inner else condition
    else {
      cout << "The number is negative." << endl;
    }  
  }
  // outer else condition
  else {
    cout << "The number is 0 and it is neither positive nor negative." << endl;
  }

  return 0;
}
OUTPUT:
Enter an integer: 67
The number is positive.
OUTPUT 2:
Enter an integer: -8
The number is negative.
OUTPUT 3:
Enter an integer: 0
The number is 0 and it is neither positive nor negative.

Note: As you can see, nested if…else complicates your reasoning. Nested if…else should always be avoided, if at all possible.

Leave a Reply

Your email address will not be published. Required fields are marked *