C++ If, else, else if Statements

C++ if-else structure: Flowchart illustration for decision-making with alternative paths.

Decision-making statement in C++

  • To execute a statement or block of statements if a certain condition(s) is true, you may use the C++ if statement.
  • As a condition is true in the if statement, C++ will execute the given statement(s).
  • In order to execute one or more statements if the condition is false, you may use the else statement.
  • If you have more than one conditions to check, in case the first condition is false, you may use the else if statements.
  • In the following sections, we will show you examples of using if, if with else, and if..else if..else statements.

For beginners to get this:

You may compare decision-making statements (if..else and switch..case) in C++ or other programming languages just like the decision-making in real life.

So, if you have to choose which color shirt to buy, which place to travel to this year etc. you have options to choose from.

To make a decision, you go through certain checks/conditions e.g. Do I like this color? Yes/No. Do I have already this color shirt? Yes/No and so on.

Based on these criteria, you make a decision.

Syntax of using the if statement in C++

The general way of writing the C++ if statement:

if(condition) {

   // statement(s) to execute if condition is true

}

An example of using a simple if statement

In this example, we only used the C++ if statement.

So, the statement inside the if will execute only if the condition is true. Otherwise, nothing will display.

We will check the value of the variable x against y as follows:

#include <iostream>

using namespace std;

int main() {
int x = 5;
int y = 1;

    if (x > y) {
      cout << "Value of x is greater than y";
    }
  return 0;
}

Output:

Value of x is greater than y

What if the condition is false? Using C++ else statement

As mentioned in the introductory section, the statement inside the if statement executes if the condition is true.

To execute one or more statements if the condition is false, we use C++ else statement.

General way of using the else statement in C++:

if (condition) {

  // Statement(s) to execute of condition is true

} else {

  // Statement(s) to execute of condition is false

}

The example of the else statement

  • In the following example, we assigned a value to the variable x.
  • Then we will check if the variable value is greater than or equal to 5.
  • If the value is greater or equal to 5 then the statement inside the if statement will execute.
  • Otherwise, the else statement will execute:

The code:

#include <iostream>

using namespace std;

int main() {
int x = 3;

    if (x > 4) {
      cout << "Value of x is equal or greater than 5";
    }
    else{
        cout << "Value of x is less than 5";
    }
  return 0;
}

Output:

Value of x is less than 5

C++ if..else if..else statement

The else if statement is used when you have multiple conditions to check – if the first condition is false.

The syntax of using the else if statement is as follows:

if (condition1) {

  // Statement(s) to execute of condition1 is true

} else if (condition2) {

  // Statement(s) to execute of condition2 is true

} else if (condition3) {

  // Statement(s) to execute of condition3 is true

} else {

  // If none of the condition is true, this part will execute

}

A simple example of using if..else if..else statements

In this example, a variable marks is declared and assigned a value. Then we used if..else if..else statements.

See the code and output:

#include <iostream>

using namespace std;

int main() {
int marks = 70;

if (marks > 90) {
  cout << "A+";
} else if (marks >=70) {
  cout << "B+";
} else {
  cout << "Improve";
}

  return 0;
}

Output:

B+

The example of else if with string variable

In the following example, we will check the color value stored in the variable using if..else if, and else statements.

  • So, the first condition checks if the color is red, it will display its message.
  • The second statement (else if) checks if the color is green.
  • Third check if the color is black.
  • Finally, use the else statement that will execute if all conditions are false.

Have a look:

#include <iostream>

using namespace std;

int main() {
string color = "green";
if (color == "red") {
  cout << "The color is Red.";
} else if (color == "green") {
  cout << "The color is Green.";
} else if (color == "Black") {

  cout << "The color is Black.";

} else {

  cout << "Some other color";

}
  return 0;

}

Output:

The color is Green.

An example of using if statement with for loop

The example below shows using an if statement with a C++ for loop.

We will check the value of a variable used in the for loop. If the value of variable x is 5 – we will check in the if statement (within the for loop) then for loop will terminate by using the break statement.

#include <iostream>

using namespace std;




int main() {

  for (int x = 1; x <= 10; ++x) {

  if (x == 5) {

    break;

  }

     cout << "Current value of x= " << x << endl;

  }

  return 0;

}

Output:

Current value of x= 1
Current value of x= 2
Current value of x= 3
Current value of x= 4

More complex example with user input and multiple conditions

In this example, we will use the logical operator && for implementing multiple conditions in the if statement.

#include <iostream>
using namespace std;

int main()
{
cout << "Enter a number: ";
int num;
cin >> num;

if (num >= 0 && num <= 100)
cout << "You entered the number between 0 and 100";
else
cout << "The number is less or beyond";

return 0;
}

As you run this program (copy/paste the code into your editor),  it will ask you to enter a number.

If the number is between 0 to 100, then if condition will be evaluated as true.

There we used a logical operator && to check the minimum and maximum numbers. If the entered number is between 0 to 100 (inclusive) then if condition will be true and it will display a message.

Otherwise, the else statement will execute:

Output as I entered 75:

CPP-if-else-multiple

Output as I entered number 105

CPP-if-else-multiple-2

How to use nested if statement in C++

C++ allows you to use an if statement inside another statement. This is called a nested if statement.

This is how we can use it:

// outer if statement

if (condition_outer_if) {

  // statements to execute here

  // inner if statement

  if (condition_inner_if) {

    // statements to execute here

  }

}

So, where we write the statements to execute inside curly braces, we initiate another if statement.

The first statement is called outer if while the second that we used inside, is called inner if statement.

An example of nested if statement

Let us see the nested if statement in the action below:

#include<iostream>

using namespace std;

int main()

{

int x = 5; // Declare and assign values to variables

int y = 10;

if (x > 4) { // Outer if

cout << " Outer if condition is true: " << x << endl;

if ( y < 15) { // Inner if

cout << " Inner if condition is true: " << y << endl;

}

}

return 0;

}

Output:

CPP-nested-if

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!