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

if else statement contains one condition. If  condition evaluates to true, one set of statements will execute. If condition is false other set of statements will execute.

Syntax of if else statement is:

if(Condition-Expression)
{
Statements-1;
}
else
{
Statements-2;
}

Condition-Expression is any conditional expression. If it is true, then Statements-1 will execute otherwise Statements-2 will execute.

Statements-1, Statements-2 are set of valid C Language statements.



 Program 
#include<stdio.h>
main()
{
int a;
printf(“\nEnter a number:=”);
scanf(“%d”,&a);
if(a>0)
printf(“\nPositive”);
else
printf(“\nNot positive”);
getch();
}
Output
Enter a number:= -7
Not positive

In the above program, a has been entered -7.  Condition a>0 is false so printf(“Not positive”) will be executed.

 



Best Books of Computer Science


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