Home » Blog » If…Else Statement In C++

If…Else Statement In C++

In if…else statement, if the condition is true then the statements inside if statements are executed, and if the condition is false then statements inside else are executed.

Syntax

if(Condition or Expression)
{
     //Statements
}
else
{
     //Statements
}

Example

#include <iostream>
using namespace std;

int main() 
{
    int number;

    cout << "Enter an integer value: ";
    cin >> number;

    if ( number >= 0)
    {
        cout << "You entered a positive integer: " << number << endl;
    }
    else
    {
        cout << "You entered a negative integer: " << number << endl;
    }
    cout << "This line is always executed.";

    return 0;
}

OutPut

Enter an integer: -4
You entered a negative integer: -4.
This line is always executed.

 

 

Leave a Reply

Your email address will not be published.