if else statement in C language

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(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





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