if … if else Statement in C++

In this lesson, you will learn about the ‘if‘ statement, ‘if-else‘ statements, nested ‘if’ statements and the ‘if-else if’ ladder in C++.

C++ ‘if’ Statement

The ‘if’ statement is a decision-making statement used to decide when the compiler will execute a statement or not, based on a condition.

More like this:

Syntax of ‘if’ statement

if (condition) {
  DoSomething(); // Statements to execute if the condition is true
}

Flowchart

Example of ‘if’ statement in C++

#include <iostream>

using namespace std;

int
main() {

  int number;

  if (5 > 2) {

    cout << "5 is greater than 2";

  }

  return 0;

}

Output

5 is greater than 2

C++ if…else Statement

The if..else statement is a regular ‘if’ statement with an optional else statement, which gets executed if the condition is false.

Syntax of if..else statement

if (expression)
  {
// exexue if expression is true
  }
else
  {
// execute if expression is false
  }
 

Example of if …else statement in C++

#include <iostream>
using namespace std;

int main ()
{
  int a = 12;
  int b = 15;
  if (a > b)
    {
      cout << "a is greater than b";
    }
  else
    {
      cout << "b is greater than a";
    }


  return 0;
}

Output

b is greater than a

C++ Nested ‘if’ Statements

In C++, a nested ‘if’ statement is an if statement inside another if statement.

int main()
{
   if (expression1) // main if statement
   {
       if(expression2) // nested if statement
       {
       // nested if body
       }
   }
}

Example of Nested ‘if’ Statements in C++

#include<iostream>
using namespace std;
int main ()
{
  int a = 20;
  int b = 30;
  int c = 40;

  if (a < b)
    {
      if (b < c) // Nested if statement
    cout << "c is greater than b";
    }

  else
    cout << "a is greater than b";

  return 0;
}

Output

c is greater than b

if-else Ladder Statement in C++

An ‘if-else‘ ladder statement in C++ is used to check a set of conditions in sequence.

if (expression1)
  {
    statement1;
  }
else if (expression2)
  {
    statement2;
  }
else if (expression3)
  {
    statement3;
  }
else if (expression4)
  {
    statement4;
  }
else
  {
    statement5;
  }

The compiler checks an expression and goes to next if found to be false. Once a true expression is found, the code inside that expression is executed, and the if statement is terminated. If none of the expressions are true, the else part is executed.

Example of if-else Ladder Statement in C++

#include<stdio.h>

int main() {

  int marks;

  printf("Please enter a mark between 0-100:\n");

  scanf("%d", & marks);

  if (marks >= 90) {

    printf("Your grade is: A\n");

  } else if (marks >= 70 && marks < 90) {

    printf("Your grade is: B\n");

  } else if (marks >= 50 && marks < 70) {

    printf("Your grade is: C\n");

  } else {

    printf("Your grade is: F\n");

  }

  return (0);

}

Output

Please enter a mark between 0-100:

76

Your grade is: B

Points to remember

  • ‘if’ statement is used to make decisions.
  • An ‘if’ statement body is enclosed within curly brackets {}
  • An ‘else’ statement body is executed in the main ‘if’ expression is false.
  • Nested if statements are if statements inside each other.
Back to: Learn C++ Programming > Control Statements in C++

Leave a Reply

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