If statement with example


In C programming language or any other programming language when we need to execute a fix block of code if the given condition is true otherwise skip this block at the time execution. Then we need the "If statement".


Syntax of if statement in c:

The statement inside the body of if statement only execute if the given condition is true.  
if (condition)
{
     //Code Block
     //These statements will only execute if the condition is true
}


Flow chart "if" statement:


Flow Chart for if statement 


C-IF Statement:

First example:

problem statement: EVEN NUMBER

suppose we has a number.we need to know the given number is  even number. if the given number is a even number then the output is "Given number is a EVEN " otherwise print nothing.

#include <stdio.h>
int main()
{
    int num = 10;
    
    if (num%2=0)
    {
        printf("Given number is EVEN");
    }
    return 0;
}


As we all know the number 10 is a even number because when we divide 10 by 2 then the reminder is the 0 so OUTPUT for this code is "Given number is EVEN"

SECOND EXAMPLE:

problem statement: basic calculator 

In this problem statement we need to make make a basic calculator by using the "if statement".
#include <stdio.h>
int main()
{
    int x, y;
    printf("enter the value of x:");
    scanf("%d", &x);
    printf("enter the value of y:");
    scanf("%d", &y);
    if (x>y)
    {
	printf("x is greater than y\n");
    }
    if (x<y)
    {
	printf("x is less than y\n");
    }
    if (x==y)
    {
	printf("x is equal to y\n");
    }
    printf("End of Program");
    return 0;
}