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 

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 7Condition n>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