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 else if in a very simple way.
Let's start
- It is used to test the condition.
- If executes only one condition at a time.
- The condition which is true first from the top will execute.
- if else if statement is used where we have to deal with only one condition at a time.
import java.util.Scanner; class Easy { public static void main(String[] args) { Scanner in=new Scanner(System.in); int no=5; if(no>2)//condition true { System.out.println("Hello1"); } else if(no<2)//condition false { System.out.println("Hello2"); } else if(no==5)//condition true { System.out.println("Hello3"); } else { System.out.println("Hello4"); } } } /* ### Output ### Hello1 because it executes only one condition which becomes true first from top. */
This is very interesting example of this topic here we are going to show result according to obtained percent.
Suppose that
First division is in between 60 to 100
Second division is in between 45 to 59
Third division is in between 33 to 44
Fail < 33
you can change this range as per your requirements, i am just telling how to code for this program.
To make this program we will take a single variable of type float and after taking user input we will apply condition on it and print result according to percent.
import java.util.Scanner; class Easy { public static void main(String[] args) { Scanner in=new Scanner(System.in); float p;//declaring variable for percent System.out.println("Enter your percent"); p=in.nextFloat(); if(p>=60) System.out.println("First div"); else if(p>=45) System.out.println("Second div"); else if(p>=33) System.out.println("Third div"); else System.out.println("Sorry!! you are fail!!"); } } /* ### Output ### Enter your percent 50 Second div */
Request:-If you found this post helpful then let me know by your comment and share it with your friend.
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.
If you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other.
Thanks.😊
0 Comments