If else if Statement in C programming with examples

 


Hello friends how are you, today in this blog i will teach you what is if else if statement, syntax of if else if, how if else if statement works and many programs using if statement in a very simple way.

Let's start

Syntax


1.It is a part of conditional statement that executes only one condition at a time.
2.If all conditions are false then else part executes.
3.It executes that condition that becomes first true from the top.
4.In the case of if in the place of condition always zero and non-zero value is checked in which zero means condition false and non-zero means condition true

Example 1 

#include<stdio.h>
int main()
{
//Assigning value to the variable
int x=10;
if(x>5)//checking the condition
{
printf("x is greater than 5");
}
else if(x<8)//checking the condition
{
printf("x is less than 8");
}
else if(x==10)//checking the condition
{
printf("x is equal to 10");
}
else
{
printf("No one condition is true");
}
}
### output ###
x is greater than 5

As we can see from the above program there are three conditions in which first and third condition are true but it executes only one condition that becomes first true from the above so the output is x is greater than 5

Program: Show result according to percent

/*
Suppose
First div  >=65
Second div between 64 and 45
Third div  between 44 and 33
Fail       <33
*/
#include<stdio.h>
int main()
{
//variable declaration
float p;
//taking user input of percentage
printf("Enter your percentage:");
scanf("%f",&p);
if(p>=65)
printf("First Division");
else if(p>=45)
printf("Second Division");
else if(p>=33)
printf("Third Division");
else
printf("Sorry! You are fail!!!");
}
/*
### Output ###
Run1
Enter your percentage:85
First Division
Run2
Enter your percentage:30
Sorry! You are fail!!!
*/


Here I am requesting if you found this post was helpful for you then let me know by your comment in below comment box and share it with your friend. Also you can contact me on Instagram @rajusunagar_

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thank you!!.......

Post a Comment

0 Comments