if (condition) { Statement; } |
//Write a Program to Find
Number is Greater than 10.
#include
<stdio.h>
void
main() { int n; printf("Enter a Number : "); scanf("%d",&n); if(n>10) { printf("The number is greater
than 10\n"); printf(" The Number is
=%d",n); } else { printf("The value is less than
10"); } } |
Output:
Enter a number: 12
The
number is greater than 10 The
Number is =12 |
if (condition) { Statement1; Statement2; } else { Statement1; Statement2; } |
/* To check a number is eve or odd */ #include<stdio.h>
void
main() { int
n; printf
(“enter a number:”); sacnf
(“%d”, &n); If
(n%2==0) printf
(“even number”); else printf(“odd
number”); }
|
Output:
enter a number:121
odd number
|
When there are another if else statement in if-block or else-block, then it is called nesting of if-else statement.
Syntax is :-
if(condition)
{
if(condition)
{
Statement 1
}
else
{
Statement 2
}
}
else
{
if(condition)
{
Statement 3
}
else
{
Statement 4
}
}
Example
/*
Write a Program to Find Greatest Number of Three Numbers
#include <stdio.h>
void main() { int
a,b,c;
printf("Enter a Value of a,b,c");
scanf("%d%d%d",&a,&b,&c);
if(a>b) {
if(a>c) {
printf("A is the greatest Number"); } else {
printf("C is the greatest Number"); } } else {
if(b>c) {
printf("B is the greatest Number"); } else {
printf("C is the Greatest Number"); } } } |
Output
Enter
a Value of a,b,c56 36 59 C is
the greatest Number |
In this type of nesting there is an if else statement in every else part except the last part. If condition is false control pass to block where condition is again checked with its if statement.
Syntax is :-
if (condition) |