C has the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.
If else is a Keyword used to implement conditional construct. Construct means a block.
Syntax:
if(condition)
{
// control comes here when the condition is true
}
else
{
// control comes here when the condition is false
}
- Accept a number, output will be less than 50 or greater than 50.
#include<stdio.h>
int main()
{
int x;
Printf(“Enter a number:”);
Scanf(“%d”,&x);
if(x<50)
{
Printf(“Less than 50”);
}
else
{
if(x>50)
{
Printf(“Greater than 50”);
}
else
{
Printf(“Equal to 50”);
}
}
return 0;
}
The above program depicts the if-else ladder concept.
There is another way to write the same program by using only if.
#include<stdio.h>
int main()
{
int x;
Printf(“Enter a number:”);
Scanf(“%d”,&x);
if(x<50)
{
Printf(“Less than 50”);
}
if(x>50)
{
Printf(“Greater than 50”);
}
if(x==50)
{
Printf(“Equal to 50”);
}
return 0;
}
Also read,
Jonathan Livingston Seagull Book Summary
Your Brain on PORN Book Summary by Gary Wilson
THE IMMORTALS OF MELUHA By Amish Tripathi full summary
Eleven Minutes by Paulo Coelho Summary
Hyperfocus by Chris Bailey Summary
Black Beauty by Anna Sewell Summary
You Can Heal Your Life by Louise Hay Summary
ATTITUDE IS EVERYTHING BY JEFF KELLER SUMMARY
THE BIOGRAPHY OF SWAMI VIVEKANANDA
THE SHAWSHANK REDEMPTION EXPLAINED
Turtles All the Way Down by John Green Story Explained
Essentialism The Disciplined Pursuit of Less by Greg McKeown Summary
The One from the Stars by Keshav Aneel
The Miracle Morning Guaranteed to Transform Your Life (Before 8AM)
The Little Prince Book Summary by Antoine de Saint-Exupery
The Courage to Be Disliked Book Summary