if statement - Devbhoomi

FREE JOB ALERT & ONLINE TUTORIALS

Hot

Post Top Ad

Sunday 5 November 2017

if statement

It is used to execute an instruction or sequence / block of instruction only if condition is fulfilled. if statement, expression is evaluated first and then, depending on whether the value of the expression (relation or condition) is true or false, transfers the control to the particular statement or group of statements.
Different forms of implementation of if-statement are :
  • Simple if statement
  • if-else statement
  • Nested if-else statement
  • Else if statement
Simple if statement
This is used to executed the statements only if the condition is true.
Syntax :
    if(condition)
    {
        block of statement;
    }
Example :

    #include <stdio.h>
    int main()
    {
    int number = 0;
    printf("\nEnter an integer : ");
    scanf("%d",&number);

    if (number > 10)
    {
    printf("\nYou entered %d which is greater than 10\n", number);
    }

    if (number < 10)
    {
    printf("\nYou entered %d which is less than 10\n", number);
    }

    return 0;
    }

Output :
 Enter an integer : 8
 You entered 8 which is less than 10

No comments:

Post a Comment

Post Top Ad