if statement in c++


  • If the conditional expression is true, the target of the if will be executed; otherwise, the target of the else, if it exists, will be executed. At no time will both be executed.
  • The conditional expression controlling the if may be any type of valid C++ expression that produces a true or false result.
  • The if statement allows you to put some decision making into your programs.

Syntax

    if (condition)
     statement; 
  • If the expression is true (nonzero) the statement will be executed.
  • If the expression is zero, the statement will not be executed.

Flow Diagram

    if-statement in c++ language

Example

    // ifdemo.cpp
    // demonstrates IF statement
    #include using namespace std;
    
    int main()
      {
        int x;
    
        cout > x;
        if( x > 100 )
        cout << “That number is greater than 100 ”; return 0;
      }
    
    

Output

    Enter a number: 2000
    That number is greater than 100