If-else statement in C programming allows you to control and execute the specific block of code if the condition is true, otherwise, execute the else block.
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Enter a number less than or equal to 10: ");
scanf("%d", &num); //user enter value storing in 'num'.
if(num <= 10) //if 'num' value is less then or Equal to 10 then
{
printf("What an obedient servant you are!"); //print this.
}
else //if not mean 'num' value greater than 10 then
{
printf("Invalid Entry, Entered value is Greater than 10"); //print this.
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Enter a number less than 10: ");
scanf("%d", &num); //user enter value storing in 'num'.
if(num > 10) //if 'num' value is greater then 10 then
{
printf("Entered value is greater then 10!"); //print this.
}
else if(num < 10) //if 'num' value is Less then 10 then
{
printf("Entered value is Less then 10!"); //print this.
}
else //if not in above mean equal to 10 then
{
printf("Entered value is Equal to 10"); //print this.
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Enter value from 1 to 4: ");
scanf("%d", &num); //user enter value storing in 'num'.
if(num == 1) //if 'num' value equal to 1 then
{
printf("Entered value is 1!"); //print this.
}
else if(num == 2) //if 'num' value equal to 2 then
{
printf("Entered value is 2!"); //print this.
}
else if(num == 3) //if 'num' value equal to 3 then
{
printf("Entered value is 3!"); //print this.
}
else if(num == 4) //if 'num' value equal to 4 then
{
printf("Entered value is 4!"); //print this.
}
else //if not in above then
{
printf("Invalid Entry!"); //print this.
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Enter value from 1 to 3: ");
scanf("%d", &num); //user enter value saving in 'num'.
if(num <= 3) //if 'num' value Less then or equal to 3 then
{
if(num == 1) //if 'num' value equal to 1 then
{
printf("Entered value is 1"); //print this.
}
else if(num == 2) //if 'num' value equal to 2 then
{
printf("Entered value is 2"); //print this.
}
else if(num == 3) //if 'num' value equal to 3 then
{
printf("Entered value is 3"); //print this.
}
else //if not mean less then 1 then
{
printf("Entered value is Less then 1"); //print this.
}
}
else //if not mean greater then 3 then
{
printf("Invalid Entry!"); //print this.
}
getch();
}