C++ if-else

Last Updated : 10 Jan 2026

Programming languages rely on conditional statements to implement execution control through specific code blocks dependent on conditions. C++ programming offers the if-else statement because it is a fundamental conditional construct for making decisions.

The if statement checks a provided condition to choose between running different sections of code based on truth or false.

C++ includes multiple iterations of the if statement to manage different program requirements efficiently. In C++ programming, an if statement is used to test the condition.

Types of if Statements in C++

There are various types of if statements in C++. Some of them are as follows:

  • if statement
  • if-else statement
  • if-else-if ladder
  • nested if statement

Each decision-making structure within C++ provides unique functionalities that boost programming flexibility because they perform distinct tasks.

1. C++ if Statement:

The most basic form of conditional statement in C++ requires only an if statement. The C++ if statement tests the condition. It is executed if the condition is true. When the condition proves false, the program omits the subsequent if block.

Syntax:

It has the following syntax:

Flowchart of if statement

php if-else statement flowchart

C++ If Example:

Let us take an example to illustrate the if statement in C++.

Example

Compile and Run

Output:

It is even number

Explanation:

  • The program checks if (number%2 = 0), the program displays "It is an even number" when the condition proves to be true.
  • When false conditions occur, the program execution continues without changing.

2. C++ If-else Statement:

The C++ if-else statement also tests the condition. It executes if the block if condition is true otherwise else block is executed.

Syntax:

It has the following syntax:

Flowchart of if-else statement

php if-else statement flowchart

C++ If-else Example:

Let us take an example to illustrate the if-else statement in C++.

Example

Compile and Run

Output:

It is odd number

Explanation:

  • The expression number % 2 == 0 functions to verify if a number exhibits evenness.
  • Under this condition, the program displays "It is an even number".
  • The else block runs when the previous conditions are false and prints "It is an odd number".

3. C++ if-else-if ladder Statement:

The C++ if-else-if ladder statement executes one condition from multiple statements.  Multiple cases can be handled through an if-else if conditional sequence in this structure.

Syntax:

It has the following syntax:

Flowchart of if-else-if ladder statement

php if-else statement flowchart

C++ if-else-if ladder Example:

Let us take an example to illustrate the if-else-if ladder statement in C++.

Example

Compile and Run

Output:

Enter a number to check grade:66
C Grade

Output:

Enter a number to check grade:-2
wrong number

Explanation:

  • The program evaluates conditions sequentially.
  • The program follows one condition before running its associated block and skipping any additional conditional blocks.

4. Nested if Statement:

The if statement structure includes another if statement within its body. Consecutive levels of conditional checks require the utilization of this statement.

Syntax:

It has the following syntax:

Flowchart of nested if statement

php if-else statement flowchart

Nested if Example:

Let us take an example to illustrate the nested if statement in C++.

Example

Compile and Run

Output:

Enter your age:
18
Do you have an ID? (1 for Yes, 0 for No):
1
You are allowed to enter.

Explanation:

  • The outer block first verifies the user's age whether it exceeds 18 years.
  • The internal if statement requires verification of user identity through ID possession.
  • The system permits entry when both conditions match but denies entry if any condition fails to be satisfied.

Conclusion

In conclusion, C++ programs use if statements as basic decision tools, which trigger conditional responses based on different circumstances. Different variations consist of simple if alongside if-else, if-else-if ladder, and nested if along with the ternary operator to adapt to numerous conditions. A programmer's selection of if statement type determines both the readability and efficiency as well as the maintainability of their code.


Next TopicC++ switch