C++ Language

Simple If Statement

In simple if statement the line of code inside if statement are executed if the condition or expression is true.

Syntax

if (Expression or Condition) 
{
   // statements
}

Example

#include <iostream>
using namespace std;

int main() 
{
    int number;
    cout << "Enter an integer: ";
    cin >> number;
    
    //if number is positive then statements inside if statements are executed.
    if ( number > 0) 
    {
        cout << "You entered a positive integer: " << number << endl;
    }

    cout << "This statement is always executed.";
    return 0;
}

OutPut

Enter an integer: 5
You entered a positive number: 5
This statement is always executed.


Enter a number: -5
This statement is always executed.

 

Leave a Reply

Your email address will not be published. Required fields are marked *