ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

Java Program: Using Multi If and Else Statement in Java Programming

Updated on January 30, 2013

Learn Java: How to use the multi if and else statements in Java Programming

The multi if and else statements in Java programming is also known as the multi branching mechanism in Java. It is called as the multi branching mechanism because in a sense it makes branches of if and else if statements. Usually the multi if and else is used if there are many conditions that needs to be meet by the program. The programming syntax of this branching mechanism is given below:

if(condition)

{

...Write your code here

}

else if(condition)

{

...Write your code here

}

else if(condition)

{

...Write your code here

}

else

{

...Write your code here

}


Using multi if and else statements in java is always helpful to meet the program’s goal and scope. Note that you can write many else if statements as you want as long as it required by the program. For another Java source code example using multi branching, we will have an algorithm that states like this:

Let us try the basics of bird’s identifier, just note that this program is not exactly the birds identifier, we will just assume that it is. LOL, anyway we will still use int data type here. See, what shall we do to accomplish that. For example, we will identify whether the user has a duck, a parrot, a chicken or an eagle. If the user has a duck then she will choose it can swim but cannot fly, if the user has a chicken then she will choose that it cannot swim and cannot fly also, if she has a parrot, she will choose that it can fly and can talk, and if she has an eagle, she will choose that it can fly and cannot talk. LOL, never mind the choices, our goal here is to use the multi if and else statements no matter what the method is. :D See the Free Java Source code below.

package birds_identifier;

import java.util.Scanner;

public class Main {

   
    public static void main(String[] args)
    {
       Scanner input =  new Scanner(System.in);

       System.out.println("Choices:");
       System.out.println("0: means no");
       System.out.println("1: means yes");
       System.out.println("2: means not a characteristic");

System.out.println();

       System.out.println("Does your pet can fly?");
       int choice1 = input.nextInt();
       System.out.println("Does your pet can swim? ");
       int choice2 = input.nextInt();
       System.out.println("Does your pet can talk? ");
       int choice3 = input.nextInt();

       if(choice1 == 0 && choice3 == 1 && choice2==2)
       {
           System.out.println("Your pet is cannot fly and can talk, therefore...");
           System.out.println("You have a PARROT");
       }
        else if(choice1==1 && choice3==0 && choice2==2)
       {
           System.out.println("Your pet is can fly and cannot talk, therefore...");
           System.out.println("You have an EAGLE");
       }
        else if(choice1==0 && choice2==0 && choice3==2)
       {
           System.out.println("Your pet is cannot fly and cannot talk, therefore...");
           System.out.println("You have a CHICKEN");
       }
        else if(choice1==0 && choice2==1 && choice3==2)
       {
           System.out.println("Your pet is cannot fly and cannot talk, therefore...");
           System.out.println("You have a DUCK");
       }
       else
       {
           System.out.println("Program Error... LOL");
       }
   }


}


Sample Output:

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.