if else statement in C language

Preview
You must first complete Simple if statement in C language before viewing this Lesson

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(Condition)
{
Statement-Block1;
}
else
{
Statement-Block2;
}

Condition is any relational or logical expression If this condition is true, then Statement-Block1 will be executed Otherewise if condition is false Statement-Block2 will be executed.



 Program to demonstrate the use of if else statement.
#include<stdio.h>
#include”conio.h”
#include<math.h>
main()
{
int a;
clrscr();
printf(“\nEnter a number:=”);
scanf(“%d”,&a);
if(a>0)
printf(“\nnumber is positive”);
else
printf(“\nNumber is not positive”);
getch();
}
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

 



Lesson tags: if else in c, if else statement of c, two way decision making statement of c
Back to: C Programming Language