C++ if...else Statement

Video Tutorial
FREE
If Block thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

Overview

If-else statements are conditional statements. They are used to execute codes in a program based on the given situation(s) or condition(s). We use if-else statements when we need to run a piece of code if the given condition is true, and another piece of code if the given condition is false.

What is If-else in C++?

An if-else statement is a conditional statement whose output depends on whether the condition is true or false. We use if-else in C++ when we want to execute a particular code if the given condition is true, and a different code if the given condition is false.

The if-else statements in C++ are pretty similar to the if-else conditions in the real world. For example, if it is raining outside, use an umbrella. Else use sunscreen.

The if-else statements in C++ are a simple yet essential component of most programming languages. From basic codes to advanced algorithms, the C++ if-else statements are used everywhere.

Types of If Statements in C++

Let us now understand the concept of conditional statements in C++. We have four types of if-else statements in C++.

  • If statements
  • If-else statements
  • If-else-if ladder
  • Nested if statements

Let's go through each one of them.

If Statements

The if statement in C++ is the simplest form of a conditional statement. If an if statement in C++ holds true, the code inside its code block gets executed. Otherwise, the program jumps to the code following the if statement.

The flowchart of an if statement

The flowchart of an if statement

Syntax for if statement in C++

Here is an example explaining the use of an if statement.

In the above example, we can see that when x is less than or equal to 10, the if statement will become true, and the code present in the if block will be executed. But if the value of x is greater than 10, then the if statement will be false which means the code inside the if will get skipped.

Output:

If-else Statements

To execute if-else in C++, we need to provide a conditional statement in the if block. If the conditional statement is true, the code inside the if block gets executed. Otherwise, the code inside the else block gets executed.

A flowchart depicting the working of an if-else statement:

The flowchart of an if-else statement

Syntax for if-else in C++:

In cases when we have a single line of code in the if or else block, we can neglect curly braces ({ }). For example:

The above piece of code means the same as:

Here is an example displaying the use of the if-else statement to understand the topic.

Output:

As we can see in the above example, when the salary is greater than 5 lakh, the code inside the if block will print. Otherwise, the code inside the else block will print.

A conditional statement never starts with an else clause. An else statement is always written after either an if statement or an else if statement. Also, it is not necessary for an if statement to be followed by an else statement.

If-else-If Ladder

The if-else-if ladder in C++ is used when we have to specify multiple if statements in C++. Just like if statements, we have to establish a conditional statement in else if statements as well. And just like else statements, else if statements are always written after an if statement. Additionally, we can have multiple else if statements in a single piece of code.

The flowchart of an if-else-if statement looks like:

The flowchart of an if-else-if statement

Syntax:

Here is an example displaying else if statements.

Output:

Explanation of working of the program:

In the above code, the value of variable marks is 73. When the program enters the if-else-if block, the first conditional statement present in the if statement gets checked. As 73 is less than 90, this statement becomes false, and we proceed to the first else if block. As 73 is also less than 80, this condition also becomes false, and we proceed to the second else if block. In the second else if block, the conditional statement holds true (73 > 70). Therefore, the code inside this else if block gets executed, and we see the output: "C grade."

Remember that while using if-else-if statements, code gets executed in the same sequence as it is written in. So, whenever a conditional holds true, the compiler executes its corresponding code and exits the if-else code block. So make sure that the conditionals you write in the if or else if blocks are in the correct sequence.

Here is an example of an incorrect if-else-if statement.

Output:

In the above example, the code inside the else if block will never get executed. When the user enters a number greater than 80, then if conditional will hold true, the code inside it will execute, and the compiler will exit the program. If the user enters a number less than or equal to 80, the code inside the else block will execute, and the compiler will exit the program. The else if block will never get a chance to execute.

Nested If Statements

Nested ifs are used when we want to add one conditional inside another conditional. In simple words, nested if means adding an if statement in the body of another if statement.

The flowchart of a nested if statement looks like this:

The flowchart of nested-if statements

Syntax:

Here is an example of a nested if statement to find the largest number among the three given numbers.

Output:

Explanation of working of the program:

We have three integer variables in the above-written code (num1, num2, and num3). When the program reaches the first or the main if statement, the condition num1 > num2 is checked. Since it holds true, the program enters the if block's code. Inside the main if block, there is another if statement. This is a nested if statement. Once again, another condition is checked. Since the condition num1 > num3 is false, the nested else block gets executed, and we see the output "num3 is the biggest number".

Alternative Conditional Statements

Although the if-else statements are the most commonly used conditional statements in most programming languages, in the C++ programming language, we have conditional statements other than the if-else statements. These are:

  • Ternary Operator: A ternary operator is like a mini version of an if-else statement. It lets you write an if-else statement in a single line of code, and it takes less space compared to the if-else statements.
  • Switch Statements: The switch statements check for equality among the given values. The values are called cases. In situations when no case is true, the default clause defined by the user gets executed.

Conclusion

  • If-else statements are called conditional statements because they are used for handling decisions.
  • Else-if is used when we have more than one if statement to execute.
  • The compiler checks the conditionals from top to bottom while executing the if-else statements until a condition holds true. If no conditional holds true, then the else clause is executed.
  • An if statement inside another if statement is called a nested if.