pemrograman java
75 / 100

ConditionSWITCH CASE What we are going to study can be seen as an alternative to writing fromkondisi logika IF ELSE IF on the previous Java tutorial. However, not all IF ELSE IF conditions can be converted into SWITCH CASE form, which we will discuss in more detail.

Advertisements

Understanding SWITCH CASE Java Language

ConditionSWITCH CASE is a branch of program code where we compare the contents of a variable with several values. If the comparison process results intrue, then the program code block will be processed.

ConditionSWITCH CASE consist of 2 part, i.e. commandSWITCH where there is the name of the variable to be checked, as well as 1 or more ordersCASE for each value to be checked.

The following is the basic format for writing conditionsSWITCH CASE in Java:

switch (nama_variabel) {case 'nilai_1':// Kode program yang dijalankan jika nama_variabel == nilai_1break;case 'nilai_2':// Kode program yang dijalankan jika nama_variabel == nilai_2break;case 'nilai_3':// Kode program yang dijalankan jika nama_variabel == nilai_3break;......default:// Kode program yang dijalankan jika tidak ada kondisi yang terpenuhi}

At the beginning of the program code, there is an orderSWITCH to input the variable to be checked. Then there are some CASE commands followed by a value.

If the contents ofvariable_name equal to one of these values, then the program code block will be executed. If it turns out that none of the CASE conditions are met, blokdefault in the bottom row is what will be run.

In each block case, ends with an order break; order structure CASE immediately stop once the conditions are met. Let's see a practical example.

Java Language SWITCH CASE Program Code Example

In the previous tutorial, there is a program code to display values ​​with structureIF ELSE IF. We will try to convert it to formSWITCH CASE.

Previously, the following code displays the value with the structureIF ELSE IF:

1234567891011121314151617181920212223242526272829303132import java.util.Scanner; class BelajarJava {public static void main(String args[]){ char nilai;Scanner input = new Scanner(System.in); System.out.print("Input Nilai Anda (A - E): ");nilai = input.next().charAt(0); if (nilai == 'A' ) {System.out.println("Pertahankan!");}else if (nilai == 'B' ) {System.out.println("Harus lebih baik lagi");}else if (nilai == 'C' ) {System.out.println("Perbanyak belajar");}else if (nilai == 'D' ) {System.out.println("Jangan keseringan main");}else if (nilai == 'E' ) {System.out.println("Kebanyakan bolos...");}else {System.out.println("Maaf, format nilai tidak sesuai");} }}

The same program can be converted into the formSWITCH CASE the following:

123456789101112131415161718192021222324252627282930313233import java.util.Scanner; class BelajarJava {public static void main(String args[]){ char nilai;Scanner input = new Scanner(System.in); System.out.print("Input Nilai Anda (A - E): ");nilai = input.next().charAt(0); switch (nilai) {case 'A':System.out.println("Pertahankan!");break;case 'B':System.out.println("Harus lebih baik lagi");break;case 'C':System.out.println("Perbanyak belajar");break;case 'D':System.out.println("Jangan keseringan main");break;case 'E':System.out.println("Kebanyakan bolos...");break;default:System.out.println("Maaf, format nilai tidak sesuai");} }}

Program code results:

Input Nilai Anda (A - E): APertahankan! Input Nilai Anda (A - E): DJangan keseringan main Input Nilai Anda (A - E): EKebanyakan bolos... Input Nilai Anda (A - E): FMaaf, format nilai tidak sesuai

In line 10 there is a command to ask the user to input one of the letters between ‘A’ - ‘E’. The value of this letter is then stored into a variablescore which has been set with a data typechar.

ConditionSWITCH CASE starting on line 12. Here is the orderswitch (score) which means we want to check the contents of the variablescore. The entire SWITCH block is in parentheses starting with a row 12 until 30.

In line 13 there is an ordercase ‘A’: This means that if the variablescore contains the character 'A‘, then run the contents of the CASE block, i.e. commandSystem.out.println(“Keep!”). Then there is the orderbreak in line 15 so that other CASE structures do not need to be processed again.

In line 16 there is an orderCASE second, that iscase ‘B’:. Same as before, this block will be executed if the variablescore contains the letters ‘B‘. So on until‘E’ case : in line 25.

If it turns out that there is no suitable value, so block default in line 28 which will be executed.

Advertisements

For more, see source https://www.duniailkom.com/tutorial-belajar-java-percabangan-kondisi-switch-case-bahasa-java/

Advertisements

 1,013 total views,  4 views today

By admin

Writer

Leave a Reply

Your email address will not be published. Required fields are marked *