C++ if Keyword

The if keyword in C++ is used to implement conditional statements, allowing the program to execute specific blocks of code based on whether a condition evaluates to true.

By using the if keyword, you can make decisions in your code and branch execution paths depending on conditions. Additional constructs like else and else if can be combined with if to handle multiple conditions.


Syntax

</>
Copy
if (condition) {
    // Code to execute if condition is true
}
if
The keyword used to define a conditional statement.
condition
A boolean expression that is evaluated to determine whether the block of code should execute.
Code block
The block of code enclosed in curly braces that executes if the condition evaluates to true.

Examples

Example 1: Simple if Statement

In this example, we will learn how to use a basic if statement to check if a number is positive.

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

int main() {
    int number = 10;

    if (number > 0) {
        cout << "The number is positive." << endl;
    }

    return 0;
}

Output:

The number is positive.

Explanation:

  1. The variable number is initialized with the value 10.
  2. The if statement checks if number > 0. Since the condition is true, the code block executes.
  3. The message “The number is positive.” is printed to the console.

Example 2: if-else Statement

In this example, we will learn how to use an if-else statement to check whether a number is even or odd.

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

int main() {
    int number = 7;

    if (number % 2 == 0) {
        cout << "The number is even." << endl;
    } else {
        cout << "The number is odd." << endl;
    }

    return 0;
}

Output:

The number is odd.

Explanation:

  1. The variable number is initialized with the value 7.
  2. The if statement checks if number % 2 == 0. Since the condition is false, the else block executes.
  3. The message “The number is odd.” is printed to the console.

Example 3: if-else if-else Statement

In this example, we will learn how to use an if-else if-else statement to classify a number as positive, negative, or zero.

</>
Copy
#include <iostream>
using namespace std;
int main() {
    int number = 0;
    if (number > 0) {
        cout << "The number is positive." << endl;
    } else if (number < 0) {
        cout << "The number is negative." << endl;
    } else {
        cout << "The number is zero." << endl;
    }
    return 0;
}

Output:

The number is zero.

Explanation:

  1. The variable number is initialized with the value 0.
  2. The first if condition checks if number > 0, which is false.
  3. The else if condition checks if number < 0, which is also false.
  4. The else block executes, printing “The number is zero.”

Key Points about if Keyword

  1. The if keyword allows conditional execution of code blocks based on boolean expressions.
  2. It can be combined with else and else if to handle multiple conditions.
  3. Curly braces {} are recommended even for single-line statements to enhance code readability and avoid errors.
  4. The condition in the if statement must evaluate to true or false.
  5. Nested if statements allow for more complex decision-making.