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

 It is two way decision making  statement. In if else statement, there is only one condition and depending upon the condition, a particular set of statements will be executed.

If  condition is true, one set of statements will be executed otherwise another set of statements will be executed

Syntax of simple if statement is:

if(Condition)
{
Statement-Block1;
}
else
{
Statement-Block2;
}

Statement-X;

Condition is any relational or logical expression which will be checked to see whether block of statements specified by “Statement-Block1” will be executed or Statement-Block2 will be executed.

If this condition is true, then Statement-Block1 will be executed.

Otherewise if condition is false Statement-Block2 will be executed.

Statement-Block1 is a set of statements which will be executed if condition is true.

Statement-Block2 is a set of statements which will be executed if condition is false.

Statement-X may be a single statement or group of statements which will be executed in both the cases of condition to be true as well as false.

if else statement can be represented with the help of a flowchart as :



 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 +ve”);
else
printf(“\nNumber isn’t +ve”);
printf(“\nBye”);
getch();
}
Output
Enter a number:= -7
Number isn’t +ve
Bye

Description of Above Program

In the above program, a has been entered -7. 

Condition a>0 is false so very next statement after else would be executed i.e. printf(“Number isn’t +ve”) followed by printf(“\nBye”).

 




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