If-else statement
In if-else statement if the condition is true, then the true statement(s), immediately following the if-statement are executed otherwise the false statement(s) are executed. The use of else basically allows an alternative set of statements to be executed if the condition is false.
Syntax:
If (condition)
{
Statement(s);
}
else
{
statement(s);
}
Flow chart:
Syntax:
If (condition)
{
Statement(s);
}
else
{
statement(s);
}
Flow chart:
Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
int y;
clrscr();
printf("Enter a year:");
scanf("%d",&y);
if (y % 4==0)
printf("%d is a leap year.",y);
else
printf("%d is not a leap year.",y);
getch();
}
#include<stdio.h>
#include<conio.h>
void main ()
{
int y;
clrscr();
printf("Enter a year:");
scanf("%d",&y);
if (y % 4==0)
printf("%d is a leap year.",y);
else
printf("%d is not a leap year.",y);
getch();
}
IF -else if statement
It can be used to choose one block of statements from many blocks of statements. The condition which is true only its block of statements is executed and remaining are skipped.
Syntax:
if (condition)
{
statement(s);
}
else if (condition)
{
statement(s);
}
else
{
(statement);
}
Flow chart
Syntax:
if (condition)
{
statement(s);
}
else if (condition)
{
statement(s);
}
else
{
(statement);
}
Flow chart
Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
int n;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
if(n>0)
printf("The number is positive.");
else if (n<=0)
printf("The number is negetive.");
else
printf("The number is zero.");
getch();
}
#include<stdio.h>
#include<conio.h>
void main ()
{
int n;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
if(n>0)
printf("The number is positive.");
else if (n<=0)
printf("The number is negetive.");
else
printf("The number is zero.");
getch();
}