In this tutorial, we’ll explore the various different types of conditional statements that are available in C++ and demonstrate their use with the help of examples.

Conditional statements allow us to build programs that can make decisions based on a set of parameters. With the help of conditional statements, you can write code that executes an action depending on whether a specific condition evaluates to true or false.

Similar to other programming languages, C++ also offers the basic conditional statements; if, else if and else. Note that every conditional statement operates on an expression that should evaluate to a Boolean result; true or false. Failure to comply with this will obviously result in an error. 

Without further ado, let’s take a look at examples of C++ conditional statements.

IF Statement

We use if statement in the scenarios where a certain block of code needs to be executed if and only if a given condition evaluates to true. The basic syntax of if statement in C++ is as follows:

if (your_condition)
{
  // the code chunk which will execute if your_condition is true.
}

Let’s look at an example to understand it. 

if (55 > 45)
{
  cout << "I am in the <if> block of code" << endl;
}

In the above example, since 55 is greater than 45, the code will print I am in the <if> block of code as the output.

If the expression above is written as if (55 < 45), the code will not output anything because the expression evaluates to false.

Note: we can use multiple if statements one after another. Each statement will run independently.

if (0 < 10)
{
  cout << "I will study." << endl;
}
if (20 < 10)
{
  cout << "I will party." << endl;
}
if (30 > 20)
{
  cout << "I will go for a fine dine." << endl;
}

The above code running in sequence will result in the following statements as output. 

I will study.
I will go for a fine dine.

The statement I will party. is not printed because its expression evaluates to false.

Ever heard of nested code segments? We can also use if statements as a nested code block under another code block. Basically, an if statement within another if statement.

ELSE IF Statement

The else if statement is a kind of if statement but with optional execution. The else if statement only runs when the preceding conditional statements evaluate to false.

if (55 < 45)
{
  cout << "I am in the <if> block of code." << endl;
}
else if (55 > 45)
{
  cout << "I am in the <else if> block of code." << endl;
}

The above code snippet will print I am in the <else if> block of code. only because the preceding if statement evaluates to false. If the preceding if statement had already evaluated to true, the else if code chunk would not have been executed.

Just like multiple if statements, you can use multiple else if statements in sequence as well. Also, note that else if statement cannot be written stand-alone. It must only be written when there exists at least one preceding if statement.

ELSE Statement

The else statement is a resting statement and executes only when all the preceding if and else if statements evaluate to false.

if (55 < 45)
{
  cout << "I am in the <if> block of code." << endl;
}
else if (55 == 45)
{
  cout << "I am in the <else if> block of code." << endl;
}
else
{
  cout << "I am in the <else> block of code." << endl;
}

The above code prints I am in the <else> block of code. as output. Just like else if statements, the else statements cannot be written stand-alone. 

I hope it was fun learning about if, else if and else conditional statements in C++. Now you can write your own C++ code and use conditional statements to generate dynamic outputs through your program. 

If you wish to learn more about C++, you can check out our collection of C++ tutorials.