if else statement in C Language

In real programming situation we need to control the flow of our program. we have to execute statements according to situation like in one situation we have to execute a set of statements and in another situation we have to execute different set of statements. In this situation we can use Decision Control Instructions.

if statement is useful when we need to execute a set of statements if a particulat condition is true but when we want to execute one block of statements if condition is true and another block of statements if condition is false, only if statement is not enough. In this type of condition we need to use if else statement. In if else statement if given condition is true then if block executed , if condition is false then else block is executed.

Flow of if else statement in C Language


Syntex :
if(condition)
{
 statements;  /∗ statements will execute if the condition is true ∗/
}
else
{
 statements;  /∗ statements will execute if the condition is false ∗/
}
Example :
#include<stdio.h>
#include<conio.h>
int main()
{
int num;
printf("Enter a number = ");
scanf("%d",&num);
if(num>=50)
{
 printf("Number is Greater than or equal to 50"); 
}
else
{
 printf("Number is less than 50"); 
}
getch();
return 0;    
}
Output (first run)
Enter a number = 70
Number is Greater than or equal to 50
Output (second run)
Enter a number = 20
Number is less than 50
Description
When you run above program, it takes an input from user and store it to a variable named num. in first run user provide 70 as input and it stored to variable num. now condition in if statement is tested (num>=50 or 70>=50) and that is true so block of if statement get executed.
in second run user provide 20 as input and condition in if statement is false so block of else statement is get executed.

SHARE THIS
Latest
Next Post