Skip to main content

Java if-else ladder with a example program.

Introduction:-

  Today we shall check about the if-else ladder it is simlar to the if-else statement only the difference is that we can combine a if statement with the else we should have a example program with the syntax and whole bunch of stuff . We also provide a download link in bellow . You are free to change the code and if you wish to share the modified code share with us in the comments.


What is the Syntax of the if -else ladder or if -else if ?

  Syntax:-
   if(Condition){
    Statement 1
    Statement 2
    Statement 3
  ..............   
}
else if(condition){
  Statement 1
  Statement 2
  Statement 3
  .................
}
else{
   Statement 1
   Statement 2
   Statement 3
  ................
}
  

How to write the java program code for the ladder ?

Code Statement :- Program to find greatest of the three number using if -else ladder.

Code:-
 class condition1{
  public static void main(String[] args){
int a = 5;
int b = 6;
int c = 7;
if(a>b && a>c){
System.out.println("The Greatest number is "+a);
}
else if(b>c){
System.out.println("The Greatest number is "+b);
}
else{
System.out.println("The Greatest number is "+c);
}
}
}

OUTPUT:-
  The Greatest number is 7

Here is the download link of the Source code.

  So , if your found this post usefull try to share it with your friends who are learing java . As "knowledge increases by Sharing " . Do check other post posted on this blog about the java programing and setup on the local setup. 

Comments

Popular posts from this blog

Program to calculate the room area and volume to illustrate the concept of single inheritance.

CODE: class Roomarea   { protected double l, b; Roomarea(double len, double br) { l=len; b=br; } void putdata() { System.out.println("Area of Room: "+(l*b)); } } class Roomvol extends Roomarea   { double h; Roomvol(double len, double br, double he) { super(len, br); h=he;   } void putdata() { super.putdata(); System.out.println("Volume of Room: "+(l*b*h)); } } public class EXP18Q4 { public static void main(String[] args) { Roomvol r1 = new Roomvol(10, 20, 10); r1.putdata();   } }

Develop a program to accept a Password from the user and throw “Authentication Failure” exception if the password is incorrect

import java.util.*; class Test {   public static void main(String args[])   {     try     {       System.out.println("Enter Password: ");       Scanner obj=new Scanner(System.in);       String pass= obj.nextLine();       if(!pass.equals("1234"))          System.out.println("Authentication Failure");       else          System.out.println("Welcome....!!");      }      catch (Exception e)      {        System.out.println("");      }      finally      {        System.out.println("Thank You.........!!!!!");    ...

Define a package named myInstitute include class named as Department with one method to display the staff of the department. Develop a program to import this package in a java application and call the method define in the package.

Code: package myInstitute; public class Department {   public void display()   {    int sr=1;    String Name="Ajay";    long no=987654321;    String desig="H.O.D";    System.out.println("Details of Staff are");    System.out.println("Srno = "+sr);    System.out.println("Name = "+Name);    System.out.println("Designation = "+desig);    System.out.println("Phone Number = "+no);   } } import myInstitute.*; class JavaApplication {   public static void main(String args[])   {     Department obj = new Department();     obj.display();   } } Output: