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
}
(1998-03-06) The Java Tutorial: Object-Oriented Programming for the Internet (2n
Current Bid: $10.89
JAVA PROGRAMMING CBTs Video Training Tutorials 9+ Hours
Current Bid: $9.95
The Java Tutorial : Object-Oriented Programming for the Internet by Kathy…
Current Bid: $14.95
The Java Tutorial : Object-Oriented Programming for the Internet by Kathy…
Current Bid: $14.95
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. 😀 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:
See all 5 photos
See all 5 photos
See all 5 photos
See all 5 photos
See all 5 photos
Related Java Tutorials:
5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java Using Netbeans: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Tutorial Examples
Other Java Source Code Samples:
Java Program: Palindrome Test Java Source Code
Java Program: Reverse String in Java Using For Loop
Java Program: Count the Number of String Characters in Java
Java Program: How to Parse a String into Integer in Java Programming
Java Program: How to Use If Statement in Java Programming
Java Program: How to Use Switch Statement in Java
Class in Java: Learn More about Java Classes with Sample Java Codes
Java Source Code: Sort Numbers in Selection Sort
Java Source Code Recursion: Recursive Koch Snow Flakes
Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
Java Source code: Reverse String in Java Using Recursion
Java Source code: How to Add numbers inside an Array Using Recursion
Java Source Code in a Recursive Linear Search
Java Source Code: A Recursive Asterisk Diamond Shape Program
Java Code Sample: Sort Numbers in Bubble Sort
Java Source Code Determine Person’s Salutation and Current Age
Java source code in Recursive function for X to the power Y
Java Source Code: Binary Search in Recursion
Java Source code sample: How to Add Numbers inside an Array using For Loop
Java source code Sample: Print Different Asterisk Shapes in Recursion