The control structure in java is similar as that in c even the syntax is also same.lets understand this with example.
There are 4 types of triangle. equilateral, isosceles, scalene and right angled triangle. so take input from user as 3 angles and state what type of triangle it is.
class findTriangle { public static void main(String[] args) { int a=-90; int b=90; int c=180; int sum; sum=a+b+c; if (a>0 && b>0 && c>0 && sum==180) { System.out.println("Triangle is"); if(a==b && b==c) { System.out.println("Equilateral triangle"); } else if(a==90||b==90||c==90) { System.out.println("Right angled triangle"); } else if((a==b && b!=c) || (a==c && b!=a) || (b==c && c!=a)) { System.out.println("isosceles triangle"); } else if(a!=b && b!=c ) { System.out.println("scalene triangle"); } } else System.out.println("This is not a triangle"); } }
Above program is giving us a perfect output for all kinds of inputs. so basically we are using if else and if else if.
lets see one more example
we will take students percentage marks as input and will display its result. whether he is pass,fail , first class etc.
class Marks { public static void main(String[] args) { int m=66; System.out.println("student is "); if(m<40) System.out.println("Fail"); else if(m>=40 && m<=50) System.out.println("Pass"); else if(m>=50 && m<60) System.out.println("Second-class"); else if(m>=60 && m<65) System.out.println("First-class"); else if(m>65) System.out.println("First class-with Distinction"); } }