If Else Switch Conditional Statements in C++

Conditional statements are used to make decisions based on a given condition, also called selection statements.

In C++ there are following conditional statements:

  • The if Statement
  • The if-else Statement
  • Nested if-else Statement
  • The if-else-if ladder
  • The switch Statement

The if Statement

The if statement is used to specify a block of code to be executed if a condition is true.

Syntax

if (condition) {
  statement1;
  statement2;
}
Example

#include <iostream>

int main()
{
    using namespace std;

    int a = 74, b = 74;

    if (a == b)
    {
        cout << "a is equal to b" << endl;
    }
    if (a > b)
    {
        cout << "a is greater than b" << endl;
    }
    return 0;
}
Output
a is equal to b

In the above example the condition a == b is true so, the statement in the body of the if statement got executed. The Curly braces { } represents the body of if. Anything that is written inside those braces is the part of the if statement.

The if-else Statement

The if-else statement has two parts, if and else. If the condition is true, the if code part will be executed and if the condition is false, the else code part will be executed.

Syntax

if (condition) {
  statement1;
  statement2;
} else {
  statement3;
}
Example

#include <iostream>
using namespace std;

int main()
{
    int a = 96, b = 97;
    if (a == b)
    {
        cout << "a is equal to b" << endl;
    }
    else
    {
        cout << "a is not equal to b" << endl;
    }
    return 0;
}
Output
a is not equal to b

In the above example, a is not equal to b, so the else code part is executed.

Nested if-else Statement

This Nested if-else statement is if-else statement inside another if-else statement.

Syntax

if (condition1) 
{
    if(condition2) 
    {
      //statement;
    }  
    else
    {
      //statement;
    }  
}
else 
{
  //statement;
}    
Example

#include <iostream>
using namespace std;

int main()
{
    int x = 4, y = 3, z = 5;
    if (x > y)
    {

        if (x > z)
        {
            cout << "x is the greatest integer";
        }
        else
        {
            cout << "x is not the greatest integer";
        }
    }
    else
    {
        cout << "x is not the greatest integer";
    }
    return 0;
}
Output
x is not the greatest integer

The if-else-if ladder

In the C++ programming language, the if else ladder statement is used to check a sequence of conditions.

Example

#include <iostream>
using namespace std;

int main()
{
    char ch;
    cout << "Enter an alphabet:";
    cin >> ch;
    if ((ch >= 'A') && (ch <= 'Z'))
    {
        cout << "The alphabet is in upper case";
    }
    else if ((ch >= 'a') && (ch <= 'z'))
    {
        cout << "The alphabet is in lower case";
    }
    else
    {
        cout << "It is not an alphabet";
    }
    return 0;
}
Output
Enter an alphabet: $
It is not an alphabet

The switch Statement

In C++, the switch statement is the best replacement for the lengthy if statements. Here, the value of the expression enclosed in the brackets ( ) following switch is checked. If the value of the expression matches the value of the constant in case, the statement corresponding to that case will be executed.

Syntax

switch(expression)
{
    case constant1:
        statement(s);
        break;
    case constant2:
        statement(s);
        break;
/* you can give any number of cases */
    default:
        statement(s);
}
Example

#include <iostream>
using namespace std;

int main()
{
    int day = 4;
    switch (day)
    {
    case 1:
        cout << "Monday";
        break;
    case 2:
        cout << "Tuesday";
        break;
    case 3:
        cout << "Wednesday";
        break;
    case 4:
        cout << "Thursday";
        break;
    case 5:
        cout << "Friday";
        break;
    case 6:
        cout << "Saturday";
        break;
    case 7:
        cout << "Sunday";
        break;
    }
    return 0;
}
Output
Thursday