C++ if Statement

The if is a flow control statement used to make a decision based on a condition.

In c++, the if statement is used to make decisions based on a condition. The if statement verifies the given condition and decides whether a block of statements to be executed or not based on the condition result. The if statement has the following forms.

  • if statement
  • if-else Statement
  • if-else if Statement

if Statement

The if statement checks the given condition and executes the block of code if the condition is evaluated to true. In other words, the if statement is used to execute a block of code if the given condition is true.

Syntax - if Statement
if (condition) {
  // block of code to be executed if the condition is true
}

The condition must be evaluated to either true or false. In C++, any non-zero value is considered to be true and zero as false.

if statement syntax
Example - Code to illustrate the if statement and condition
#include <iostream>

using namespace std;

int main()
{
    int a = 10;

    if(a < 100) {
        cout << a << " is smaller than 100" << endl;    // Executed
    }

    if(5) {
        cout << "Value 5 is true" << endl;          // Executed
    }

    if(0) {
        cout << "Value 0 is false" << endl;         // Not executed
    }

    if(a = 30) {
        cout << "Value 30 is true" << endl;         // Executed
    }

    if(a = 30, 0) {
        cout << "Value 0 is false" << endl;         // Not executed
    }

    if(a = 30, 0, 9) {
        cout << "Value 9 is true" << endl;         // Executed
    }

    return 0;
}
Output
if statement example program in c++

if-else Statement

The if-else statement checks the given condition and executes a block of code if the condition is true otherwise, executes another block of code defined under else.

  • Always else must follow the if statement.
  • else can not be used allown, however if can be.
Syntax - if-else Statement
if (condition) {
    // block of code to be executed if the condition is true
} else {
    // block of code to be executed if the condition is false
}

The condition must be evaluated to either true or false. In C++, any non-zero value is considered to be true and zero as false.

if statement syntax
Example - Code to find largest of two numbers
#include <iostream>

using namespace std;

int main()
{
    int a = 10, b = 30;

    if(a < b) {
        cout << a << " is smaller than " << b << endl;
    } else {
        cout << a << " is larger than " << b << endl;
    }

    return 0;
}
Output
if-else statement example program in c++

if-else if Statement

The if-elseif statement used to check a new condition in the elseif if the first condition is evaluated to false. The elseif statement must follw the if statement.

Syntax - if-elseif Statement
if (condition_1) {
    // block of code to be executed if the condition_1 is true
} else if (condition_2) {
    // block of code to be executed if the condition_2 is true
}

The condition_2 is varified only when condition_1 is false.

Example - Code to find largest of three numbers
#include <iostream>

using namespace std;

int main()
{
    int a = 10, b = 30, c = 20;

    if(a > b && a > c) {
        cout << a << " is larger than " << b << " and " << c << endl;
    } else if( b > c) {
        cout << b << " is larger than " << a << " and " << c << endl;   // executed
    } else {
        cout << c << " is larger than " << a << " and " << b << endl;
    }

    return 0;
}
Output
if-else if statement example program in c++
  • The if allows to define nested if statements. (if within another if)