In this C++ tutorial, you will learn how if, if-else, else if, and nested if statements control which statements execute based on conditions, with syntax, flow diagrams, and examples.

C++ If Else Conditional Statements

C++ conditional statements let a program make decisions. A condition is evaluated, and the program chooses which block of statements to execute based on whether that condition is true or false.

For example, a program can print one message when a number is positive and a different message when it is zero or negative. Conditions commonly use comparison operators such as ==, !=, >, <, >=, and <=.

The main forms covered in this tutorial are:

  • if statement — execute a block only when a condition is true.
  • if-else statement — choose between two blocks.
  • if-else-if statement — choose among several conditions.
  • Nested if statements — test another condition inside an existing conditional block.

C++ If Statement

Use an if statement when a block should execute only when a specified condition is true.

Following is the syntax of simple C++ If statement.

</>
Copy
if (condition) {
  // statement(s)
}

If the condition is true, the statement(s) inside if block are executed. If the condition is false, the statement(s) inside if block are not executed. In either of the cases, the execution continues with the subsequent statements after the completion of if statement.

C++ If Statement Flow Diagram

Following is the flow diagram of if statement in C++.

C++ If Statement

C++ If Statement Example: Check a Positive Number

Following is an example C++ program, where we use if statement to print a message only if the number is positive.

C++ Program

</>
Copy
#include <iostream>  
using namespace std;

int main() {  
   int a = 10;
   if (a>0) {
      cout << a << " is positive.";   
   }
}

Output

10 is positive.

Here, a > 0 evaluates to true because a is 10. Therefore, the statement inside the if block executes. If a were zero or negative, the block would be skipped.

C++ If Else Statement

An if-else statement is useful when exactly one of two alternative blocks should execute. The if block handles the true case, while the else block handles the false case.

Following is the syntax of C++ If Else statement.

</>
Copy
if (condition) {
  // statement(s)
} else {
  // statement(s)
}

If the condition is true, the statement(s) inside if block are executed. If the condition is false, the statement(s) inside else block are executed. After the execution of either if block or else block, program execution continues with next statements after if-else statement.

Else block is optional. So, if you do not provide else block to an if block, it becomes a simple if statement. So, if-else could be considered as an extension to simple if statement.

C++ If Else Flow Diagram

Following is the flow diagram of if-else statement in C++.

C++ If Else Statement

C++ If Else Example: Positive or Not Positive

In the following example C++ program, we use if-else statement to check if a number is positive or not.

C++ Program

</>
Copy
#include <iostream>  
using namespace std;

int main() {  
   int a = -10;
   if (a>0) {
      cout << a << " is positive.";   
   } else {
      cout << a << " is not positive.";   
   }
}

Output

-10 is not positive.

Since -10 > 0 is false, the if block is skipped and the statements in the else block execute.

C++ If Else If Statement

Use an if-else-if chain when a program needs to test multiple conditions in order. Conditions are checked from top to bottom, and the block associated with the first true condition executes.

Following is the syntax of C++ if-else-if statement.

</>
Copy
if (condition1) {
  // statement(s)
} else if (condition2) {
  // statement(s)
} else if (condition3) {
  // statement(s)
} else {
  // statement(s)
}

If condition1 is true, the statements in the first if block execute and the remaining conditions are not tested. If it is false, condition2 is evaluated. This continues until a true condition is found. If every condition is false, the final else block executes.

The final else block is optional. Without it, no block in the chain executes when all conditions are false.

C++ If Else If Flow Diagram

Following is the flow diagram of if-else-if statement in C++.

C++ If Else If Statement

C++ If Else If Example: Positive, Negative, or Zero

In the following example C++ program, we use if-else-if statement to check if a number is positive, negative or zero.

C++ Program

</>
Copy
#include <iostream>  
using namespace std;

int main() {  
   int a = -7;
   if (a>0) {
      cout << a << " is positive.";   
   } else if(a<0) {
      cout << a << " is negaitive.";   
   } else {
      cout << a << " is zero.";   
   }
}

Output

-7 is negaitive.

The first condition, a > 0, is false. The next condition, a < 0, is true, so its block executes and the remaining else block is skipped.

Nested If Else Statements in C++

An if or if-else statement can appear inside another conditional block. This is called a nested if statement. It is useful when a second condition should be checked only after the first condition has already been satisfied.

</>
Copy
if (condition1) {
    if (condition2) {
        // statements when both conditions are true
    }
}

C++ Nested If Example: Check Positive Even Number

In this example, the second condition is tested only when the number is positive.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int number = 12;

    if (number > 0) {
        if (number % 2 == 0) {
            cout << "The number is positive and even.";
        }
    }

    return 0;
}

Output

The number is positive and even.

The outer condition verifies that number is greater than zero. Only then does the inner condition test whether the remainder from division by 2 is zero.

Using Logical Operators in C++ If Else Conditions

When related conditions do not need separate nested blocks, logical operators can combine them into one expression. C++ provides && for logical AND, || for logical OR, and ! for logical NOT.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int age = 22;
    bool hasTicket = true;

    if (age >= 18 && hasTicket) {
        cout << "Entry allowed.";
    } else {
        cout << "Entry not allowed.";
    }

    return 0;
}

Output

Entry allowed.

The && operator requires both conditions to be true. Here, age >= 18 is true and hasTicket is also true, so the if block executes.

How C++ Evaluates Conditions in If Else Statements

A condition in an if statement is contextually converted to a Boolean value. Expressions that evaluate to true enter the associated block; expressions that evaluate to false do not.

Comparison expressions are the clearest way to write most conditions:

  • x == y checks whether two values are equal.
  • x != y checks whether two values are different.
  • x > y and x < y compare relative values.
  • x >= y and x <= y include equality in the comparison.

Be careful not to confuse the assignment operator = with the equality operator ==. For example, x == 10 compares x with 10, whereas x = 10 assigns the value 10 to x.

C++ If Else Inside a For Loop

An if-else statement can be used inside a loop when each iteration requires a decision. The following program checks whether each number from 1 through 5 is even or odd.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        if (i % 2 == 0) {
            cout << i << " is even\n";
        } else {
            cout << i << " is odd\n";
        }
    }

    return 0;
}

Output

1 is odd
2 is even
3 is odd
4 is even
5 is odd

The loop supplies a new value of i on each iteration. The if-else statement then selects the appropriate output for that value.

Common Mistakes with C++ If Else Statements

  • Using = instead of ==: assignment and equality testing are different operations.
  • Writing conditions in the wrong order: in an else if chain, an earlier broad condition can prevent a later, more specific condition from ever being reached.
  • Adding a semicolon after the condition: writing if (condition); creates an empty statement and changes the intended control flow.
  • Forgetting braces in nested logic: braces make it clear which statements belong to each condition and help avoid ambiguous-looking code.
  • Testing overlapping conditions without considering precedence: only the first true branch of an if-else-if chain executes.

C++ If Else Practice Examples

After understanding the examples above, try writing C++ programs for these conditional problems:

  1. Check whether an integer is even or odd.
  2. Find the larger of two numbers using if-else.
  3. Find the largest of three numbers using an if-else-if chain.
  4. Check whether a number is positive, negative, or zero.
  5. Check whether a given year satisfies the leap-year conditions.
  6. Assign a grade based on a numeric score using an if-else-if chain.
  7. Use a nested if to check two related requirements in sequence.

Choosing Between If, If Else, Else If, and Nested If in C++

  • Use if when an action is required only for the true case.
  • Use if-else when one of two alternatives must be selected.
  • Use an if-else-if chain when several mutually exclusive conditions must be tested in sequence.
  • Use nested if when a second decision logically depends on the result of the first decision.
  • Use logical operators to combine related conditions when doing so keeps the condition clear and readable.

Summary of C++ If Else Statements

In this C++ Tutorial, we learned how C++ uses if, if-else, and if-else-if statements for conditional execution. We also covered nested if statements, logical operators in conditions, conditional statements inside loops, and common mistakes to avoid.