if-else statement in C


  • In if statement we observed that the statements present inside if block is executes only when condition is true. It does nothing when the condition is false. if-else statement take care of true as well as false condition.
  • if-else statement contains two blocks if block and else block. If condition is true then statements present inside if block gets executed and if condition is false statements present inside the else block gets executed.
  • Here is a syntax of if-else statement is as follows:
  • Syntax

    if (condition)

    {

    Statements;

    }

    else

    {

    Statements;

    }
  • If the condition is true then the statements present inside the if block will be executed and if the condition is false then the statements present inside the else block will be executed. You can understand the concept of if-else statement more clearly with the help of following diagram:
  • decision-making
  • Here is an example of if-else statement as follows:

Example

#include<stdio.h>

#include<conio.h>

int main()

{

int a,b;

printf("\n Enter the value of a and b:");

scanf("%d%d",&a,&b);

if(a==b)

{

printf("value of a and b are equal");

}

else

{

printf("value of a and b are not equal");

}

getch();

return 0;

}

Output

The output of the above program is as follows:

Enter the value of a and b:4

3

value of a and b are not equal