if

Estimated reading: 1 minute 93 Views

The if statement is a conditional statement in C++ that allows you to execute code based on a condition. The general syntax of an if statement is as follows:

if (condition) {
  // code to execute if the condition is true
}

The condition can be any Boolean expression. If the condition evaluates to true, the code block inside the if statement is executed. If the condition evaluates to false, the code block inside the else statement is executed.

Here is an example of an if statement:

#include <iostream>

using namespace std;

int main()
{
    int x = 0;
    cin >> x;
    if (x > 0)
        cout << "This number is Positive" << endl;

    return 0;
}

Output:

  • Case 1: if you entered a positive digit like 5, your if condition will be true and the block of code that follows the condition will be executed.
    This number is Positive
  • Case 2: if you entered zero or a negative digit like -5, your if condition will be false and the block of code that follows the condition will not be executed which means that nothing will appear on your result screen.
Share this

if

Or copy link

CONTENTS
English