The if and else statements in C++

Suppose we may want to execute a particular block of code based on a specific condition and another block if the condition is false. This is where if-else statements are so important and this article describes some of the basic principles of if-else statements.

The if-else statement:

The syntax for if-else statement is :

if(condition 'R')
block of code(executed when R is true)
else
block of code(executed when R is false)

Basically, the block of code under the if statement is executed when the condition is true. Or else the block under the else statement is executed.

The following diagram illustrates the operation of the if-else statement:

flow of if and else in C++

The following code servers as an example:

#include <iostream>
int main()
{
  int a;
  std::cout << "enter a number: ";
  std::cin >> a;
  if (a > 10) //a>10 is the condition to be checked
  {
    std::cout << "The number is greater than 10" << std::endl;
  }
  else
  {
    std::cout << "The number is not greater than 10" << std::endl;
  }
}
Output:
enter a number: 50
The number is greater than 10

Nested if else statement:

Nested if else statements are putting in an if-else statement inside the if block or else blocks of another if-else statement.

The code then has three blocks and is executed based on both the conditions in two if statements (the outer one and the nested one).  If the outer condition is false the inner condition is not checked and the else statement is executed just like a normal if-else statement.

The following code illustrates the point:

#include <iostream>
int main()
{
  int a;
  std::cout << "enter a number: ";
  std::cin >> a;
  if (a >= 10) //a>=10 is the condition to be checked
  {
    if (a == 10) // a==10 is the condition 
    {
      std::cout << "The number is equal to 10" << std::endl;
    }
    else
    {
      std::cout << "The number is greater than 10" << std::endl;
    }
    
  }
  else
  {
    std::cout << "The number is less than 10" << std::endl;
  }
}
Output:
Run 1:
enter a number: 50
The number is greater than 10
Run 2:
enter a number: 5
The number is less than 10
Run 3:
enter a number: 10
The number is equal to 10

Some additional features:

Sometimes it may be necessary to use certain variables in the if statement and not anywhere else. In variable can be initialized in the condition block of the if statement using the following syntax:

if(initialization;condition)

The following code illustrates the above-mentioned concept  and a little advanced example of nesting if-else statements :

#include <iostream>
int main()
{
  int a;
  std::cout << "enter a number: ";
  std::cin >> a;
  if (int k; a > 10) // initializing k
  {
    
    std::cout << "enter another number: ";
    std::cin >> k;
    if (k > 10)
    {
      std::cout << "Both numbers are greater than 10" << std::endl;
    }
    else
    {
      std::cout << "The first is greater than 10 and the other is not greater than 10" << std::endl;
    }
  }
  else
  {
    std::cout << "enter another number: ";
    std::cin >> k;
    if (k < 10)
    {
      std::cout << "Both numbers are not greater than 10" << std::endl;
    }
    else
    {
      std::cout << "The first is not greater than 10 and the other is greater than 10" << std::endl;
    }
  }

}
Output:
enter a number: 20
enter another number: 5
The first is greater than 10 and the other is not greater than 10

Leave a Reply

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