if else statement in C language
It is a two way decision making statement. In if else statement, there is only one condition. If condition is true, one set of statements will be executed otherwise another set of statements will be executed.
Syntax of if else statement is:
if(TestCondition)
{
Block1;
}
else
{
Block2;
}
TestCondition is the condition made using relational or logical operators.
Initially TestCondition is checked. If it is true, Block-1 of statements will get executed. If it is false Block2 of statements gets executed.
Block1 and Block2 are set of statements, which will be executed depending upon various Conditions. These blocks of statements must contain valid statements of C language.
Program to demonstrate the use of if else statement in C. |
#include<stdio.h> int main() { int n; printf("\nEnter a number:="); scanf("%d",&n); if(n>0) printf("\nnumber is positive"); else printf("\nNumber is not positive"); return 0; } |
Output |
Enter a number:= 7 Number is not positive |
In the above program, variable n has been entered 7. Condition n>0 is true so printf(“Number is positive”) will be executed.