if else statement in C language
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(Test)
{
Block1;
}
else
{
Block2;
}
Test is any conditional expression If it is true, then Block1 of statements gets executed otherwise Block2 of statements gets executed.
Program to demonstrate the use of if else statement in C. |
#include<stdio.h> int main() { int a; printf("\nEnter a number:="); scanf("%d",&a); if(a>0) printf("\nnumber is positive"); else printf("\nNumber is not positive"); } |
Output |
Enter a number:= 7 Number is not positive |
In the above program, a has been entered 7. Condition a>0 is true so printf(“Number is positive”) will be executed
Popular Books of Computer Science