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 one of the Decision Control Instruction. if we want to execute a block of statements in a particular condition then we can use if statement.
Flow of if statement in C Language
Syntex :
if(condition)
{
statements; /∗ statements will execute if the condition is true ∗/
}
Example :
#include<stdio.h> #include<conio.h> int main() { int a=10; if(a==10) { printf("First if block \n"); } if(a>20) { printf("Second if block \n"); } if(a<20) { printf("Third if block \n"); } getch(); return 0; }Output
First if block Third if blockIn above program an integer variable is a is initilized to 10.then in first if block condition is a==10 that is true and it returns 1 and a non zero value in if considered true and statements in this block executed and it printf "First if block". in next if statement a>20 condition is tested that is false and returns 0 to if and second if block is not executed. in third if block a<20 condition is tested that is true and returns 1 so block executed.