Switch
Switch is used as an alternative of if else series of statements. It is more efficient and easy to use than if else if ladder.Syntax
Switch value{body.. }
Test application
I am going to create a test application in Java using switch that will allow the user to enter any integer numbers from 0 to 10 and the program will produce output in alphabets against any number entered.import java.util.Scanner; public class SwitchDemo { public static void main(String arg[]) { System.out.println("Enter the Number to convert " + "in alphabets"); Scanner in = new Scanner(System.in); int a; do{ a = in.nextInt(); switch(a){ case 0: System.out.println("Zero"); break; case 1: System.out.println("One"); break; case 2: System.out.println("two"); break; case 3: System.out.println("Three"); break; case 4: System.out.println("Four"); break; case 5: System.out.println("Five"); break; case 6: System.out.println("Six"); break; case 7: System.out.println("Seven"); break; case 8: System.out.println("Eight"); break; case 9: System.out.println("Nine"); break; case 10: System.out.println("Ten"); break; default: System.out.println("Wrong input"); break; } }while(a<=10); } }






0 comments: