Open In App

C++ if else Statement

Last Updated : 12 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false, it won’t. But what if we want to do something else if the condition is false. Here comes the C++ if else statement. We can use the else statement with if statement to execute a block of code when the condition is false.

Let’s take a look at an example:

C++
#include <iostream> 
using namespace std; 
  
int main()  { 
    int i = 10; 
  
    // If statement
    if (i < 15) { 
        cout << "10 is less than 15"; 
    }
  
      // Else statement with the above if
      else {
          cout << "10 is not less than 15";
    }
  
    return 0;
}

Output
10 is less than 15

Explanation: The condition in the if block checks if 10 is less than 15. Its true so the statement inside if block, “10 is less than 15” gets printed and else block is skipped. If the condition was false, all the statements inside the if block will be skipped and else block will be executed.

Syntax

if (condition) {
// Executes this block if
// condition is true
}
else {
// Executes this block if
// condition is false
}

The if statement condition can be anything that evaluates to a boolean value or a boolean converted value. We generally use relational and equality operator to specify the condition.

Working of if-else statement

  1. Control falls into the if block.
  2. The flow jumps to Condition.
  3. Condition is tested.
    1. If Condition yields true, goto Step 4.
    2. If Condition yields false, goto Step 5.
  4. The if-block or the body inside the if is executed.
  5. The else block or the body inside the else is executed.
  6. Flow exits the if-else block.

Flowchart of if-else in C++

Examples of if else Statement in C++

The following are a few basic examples of the if-else statement that shows the use of the if-else statement in a C program.

Check for Odd and Even Number

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n = 7;

    // if block executed when the number is even
    if (n % 2 == 0)
        cout << "Even";
  
    // else block executed when the number is not even
    else
        cout << "Odd";

    return 0;
}

Output
Odd

You may have noticed that we skipped using the braces for the body of the if statement. If we do not provide the curly braces ‘{‘ and ‘}’ after if( condition ) then by default if statement will consider the immediate one statement to be inside its block.

Find Largest Among Three Numbers

C++
#include <iostream>
using namespace std;

int main() {
    int a = 1, b = 2, c = 11;

    // Finding the largest by comparing using
    // relational operators with if-else
    if (a >= b) {
        if (a >= c)
            cout << a;
        else
            cout << c;
    }
    else {
        if (b >= c)
            cout << b;
        else
            cout << c;
    }

    return 0;
}

Output
11

Explanation: Finding largest element requires users to compare multiple condition. We can compare multiple conditions by nesting one condition inside other. There is also another way of combining multiple conditions.

The above program can also be written by using compound expressions as:

C++
#include <iostream>
using namespace std;

int main() {
    int a = 1, b = 2, c = 11;

    // Finding largest using compound expressions
    if (a >= b && a >= c)
        cout << a;
    else if (b >= a && b >= c)
        cout << b;
    else
        cout << c;

    return 0;
}

Output
11

In this code, we are grouping multiple conditions in a single if statement using logical AND operator. (&&)



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg