Overview of if Statement

An if statement consists of a boolean expression followed by one or more statements. C uses the keyword if to execute a set of command lines or one command line when logical condition is true. It has only one option. 

An if statement consists of a Boolean expression followed by one or more statements.

If the Boolean expression evaluates to true, then the block of code inside the 'if' statement will be executed.

Syntax of if statement:

 

if (condition/boolean expression)
{
     //Block of C statements here
     //These statements will only execute if the condition is true
}

In this statement, if condition is true then statement are execute and if is false, then statement are not execute.

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

Flow Diagram of if statement

Fig- Flow Diagram of if statement

Example:-

#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;
}

Output:

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