if statement in c

  • One of the simplest ways to control program flow is by using if selection statements. Whether a block of code is to be executed or not to be executed can be decided by this statement.
  • The syntax for if selection statement in C could be as follows:
  • Syntax:

     statement(s); /*to be executed, on condition being true*/
  • 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.
  • If the Boolean expression evaluates to true, then the block of code inside the 'if' statement will be executed.
  • As a general rule, we express a condition using C's relational operators.
  • The relational operator allow us to compare two values to see whether they are equal to each other, unequal, or whether one is greater thanthe other.
  • this expression is true if

    x == y

    x != y

    x < y

    x > y

    x <= y

    x >= y

    x is equal to y

    x is not equal to y

    x is less than y

    x is greater than y

    x is less than or equal to y

    x is greater than or equal to y

    Flow Diagram of if statement

    Fig- Flow Diagram of if statement


#include <stdio.h>
int main()
    int number;

    printf("Enter an integer: ");
    scanf("%d", &number);

    // Test expression is true if number is less than 0
    if (number < 0)
        printf("You entered %d.", number);
    printf("The if statement is easy.");
    return 0;


Enter an integer: -2
You entered -2.
The if statement is easy.