if...else statement in C++

Posted on December 8, 2023 by Vishesh Namdev
Python C C++ Java
C++ Programming

If   is a control statement used to make decisions in your program. It is part of the conditional statements that allow you to execute different blocks of code based on whether a certain condition is true or false.

In real life, the "if" statement in C can be likened to decision-making scenarios, like<

If it's a sunny day, then you might decide to wear shorts and a t-shirt.

Syntax of if statement:

if (condition) {
  // Code to be executed if the condition is true
}

Example of if statement in C++:

#include <stdio.h> Copy Code
using namespace std;

int main() {
    int number;
    // Get user input
    cout << " Enter a number: ";
    cin >> number;

    // Check if the number is positive
    if (number > 0) {
        cout << " The number is positive." << endl;
    }

    return 0;
}

In this C++ example, the program checks if a user-inputted number is positive. If the number is greater than 0, it outputs "The number is positive." The if statement allows the program to conditionally execute this message based on the user's input.

else

The "else" statement is like a backup plan in an "if" statement. When you use "if," you specify what to do when a condition is met. But sometimes, you also want to specify what to do when that condition isn't met. That's where "else" comes in. It's like saying, "If this is true, do this; otherwise, do something else." It allows your program to make two different choices based on whether a condition is true or false.

Syntax of else statement

if (condition) {
  // Code to be executed if the condition is true
} else {
 // Code to be executed if the condition is false
}

Example of else statement in C:

#include <stdio.h> Copy Code
using namespace std;

int main() {
    int number;

    // Get user input
    cout << " Enter a number: ";
    cin >> number;

    // Check if the number is positive or non-positive
    if (number > 0) {
        cout << " The number is positive." << endl;
    } else {
        cout << " The number is non-positive (zero or negative)." << endl;
    }

    return 0;
}

In above C++ example, the program checks if a user-inputted number is positive. If the number is greater than 0, it outputs "The number is positive." Otherwise, it outputs "The number is non-positive (zero or negative)" using an if-else statement. This illustrates how the program takes different actions based on whether a condition is true or false.

Ladder in if...else:

In C++, an "if...else" ladder is like a series of checkpoints you encounter on a path. You have a list of conditions, and you check each one in order. As soon as you find a condition that's true, you take a specific action associated with that condition and then stop checking the rest. It's like finding the first open door in a hallway of many doors. If none of the conditions are met, you have a default action to take, just like a "no entry" sign at the end of the hallway.

Here's the syntax of the "if...else ladder" statement

if (condition1) {
  // Code to be executed if condition1 is true
} else if (condition2) {
  // Code to be executed if condition2 is true
} else if (condition3) {
 // Code to be executed if condition3 is true
} else {
 // Code to be executed if none of the above conditions are true
}

Example of if...else ladder statement

#include <stdio.h> Copy Code
using namespace std;

int main() {
  int score = 85;

  if (score >= 90) {
    printf("A grade\n");
  } else if (score >= 80) {
    printf("B grade\n");
  } else if (score >= 70) {
    printf("C grade\n");
  } else if (score >= 60) {
    printf("D grade\n");
  } else {
    printf("F grade\n");
  }
  return 0;
}

Nested if...else:

Nested "if...else" like making decisions within decisions. It's like having a box inside another box. When you open the outer box (the first "if" or "else"), you may find another box (another "if...else") inside. This lets you make decisions based on conditions within conditions. It's like solving a puzzle where each decision depends on the previous one, allowing you to create more complex choices in your program.

Here's the syntax of the "Nested if...else " statement

if (condition1) {
  // Code to execute when condition1 is true
if (condition2) {
   // Code to execute when condition2 is true
 } else {
   // Code to execute when condition2 is false
 }
}
else {
 // Code to execute when condition1 is false
}

Example of Nested if...else statement

#include <stdio.h> Copy Code
using namespace std;

int main() {
    int age;
    cout << " Enter your age: ";
    cin >> age;

    // Nested if...else statements to determine eligibility
    if ( age >= 18) {
        cout << "You are eligible to vote." << endl;
        // Nested if...else for additional eligibility criteria
        if (age >= 60) {
            cout <<"You are also eligible for senior citizen benefits."<<endl;
        } else {
            cout << " You are not eligible for senior citizen benefits."<<endl;
        }
    } else {
        cout << " You are not eligible to vote." <<endl;
    }

    return 0;
}