If-else Ladder in Java




Useful for if-else kind of conditional execution when we have more than two related conditional statements.

Syntax :

if(boolean_expression 1) {
   /* Executes when the boolean expression 1 is true */
}else if( boolean_expression 2) {
   /* Executes when the boolean expression 2 is true */
}else if( boolean_expression 3) {
   /* Executes when the boolean expression 3 is true */
} else  {
   /* executes when the none of the above condition is true. */
}               

Block Diagram :

prowessapps.in

Example:

WAP to give choice to user from menu to perform action.
import java.util.Scanner;
class Test
{
 public static void main(String []arg)
 {
  Scanner sc;
  sc = new Scanner(System.in);
  int x ;
  System.out.println("MENU\n----------");
  System.out.println("1. for Hello");
  System.out.println("2. for Bye");
  System.out.println("-----------");
  System.out.print("Enter Your Choice: ");
  x = sc.nextInt();
 if(x==1) {
  System.out.print("HELLO USER!!");
 }
 else if(x==2) {
   System.out.print("BYE USER!!");
 }
 else {
   System.out.print("INVALID CHOICE");
  }
 }//main closed
}//class closed
OUTPUT:
MENU
-----------
1. for Hello
2. for Bye
-----------
Enter Your Choice: 3
INVALID CHOICE

Next topic is nested if statement in Java




 







Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017, prowessapps.in, All rights reserved