Tuesday 3 November 2015

Java Selection | Switch


Switch is a reserved word or keyword that is used to make selections in Java just as IF... ELSE IF... ELSE.

The switch has a unique way of doing its things first. It has a default and an optional break statement.

Take a look at this chunk of code



switch (variable){
case value1:
statements;
break;
case value2:
statements;
break;
case value3:
statements;
break;
default:
statements;
System.exit(0);
}

Now in the above chunk of code there are so many things you haven't seen before but do not worry I am about to explain them all.

First, switch that is the keyword that you use to tell the computer there is a selection to be made among values.

Then the variable which must always be in parentheses contains the value which is to be selected from the cases. This variable can be of the datatypes char, byte, short, int.

Then case, this is also a keyword that is used to ask the computer is this the one we want?

value1, value2, valueN are the values of which one has to be chosen and if none fits then the computer carries out the commands in default.

break is a keyword that is mainly used to exit a loop or a switch block. This keyword is optional. Therefore, it depends on the application you want to build. Note that the use of it alone is a statement.
In a situation where I do want the computer to carry out more than one case statements I do not make use of the break keyword and that situation is called FALL THROUGH.

default is also a keyword. The commands or sets of instructions in the default are carried out when none of the values is the right one or equals the value in the variable encompassed by the switch.

System.exit(0); - This is a statement that is used to terminate or exit the program. It is also known as THE END.

Now let's try an exercise.

Write a program that displays five numbers and tell the user to enter one of those numbers. Now when the user presses enter, the ASCII character associated with that number is displayed.

Program:

package nicholas;
import java.util.Scanner;
public class DisplayASCII{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.println("Choose A Character From This Five: 65, 89, 120, 111, 49");
System.out.print("Enter the number you chose here: ");
int choice = input.nextInt();
switch (choice){
case 65:
char valueA = (char)choice;
System.out.print("65 represents "+valueA+" in the ASCII table.");
break;
case 89:
char valueB = (char)choice;
System.out.print("89 represents "+valueB+" in the ASCII table.");
break;
case 120:
char valueC = (char)choice;
System.out.print("120 represents "+valueC+" in the ASCII table.");
break;
case 111:
char valueD = (char)choice;
System.out.print("111 represents "+valueD+" in the ASCII table.");
break;
case 49:
char valueE = (char)choice;
System.out.print("49 represents "+valueE+" in the ASCII table.");
break;
default:
System.out.print("Wrong Input");
System.exit(0);
}
}
}


Here is the screenshot of my code:



Here is my output



Assignment:

Try to solve any problem at all in using switch block for your selections and post the problem your solved in the comment section.


Peace!!

No comments:

Post a Comment