The second statement is a SWITCH statement. The switch statement is a statementused to execute one of the statements of several possible statements, based on the value of an expression and the selectors. Each phrase is expressed by a constantinteger value, such as a value of type byte, short, or char in.
switch (expression)
{
ungkapan1 case:
pernyataan1;
break;
ungkapan2 case:
pernyataan2;
break;
. . . . . . . . . . . . . . . .
default:
pernyataan_x;
}
information
• Ungkapan1, ungkapan2 and so done in sequence starting from the first, the statementthat follows the suitable sekirannya cese will be executed
• Break ditmukan of the switch statement ends execution
• Default will only be executed if the expression in the cese no match.
java.until.Scanner import;
publib class Arithmetic {
public static void main (Sting args []) {
float result;
Data = new Scenner Scenner (System.in);
int bil1, bil2;
String op;
Char operator;
System.out.print ("Enter 1 bil:");
bil1 = data.nextInt ();
System.out.print ("enter bil 2:");
bil2 = data.nextInt ();
System.out.print ("enter the operator:");
op = data.next ();
operaor = op.charAt (0);
switch (operator) {
case '*': result = bil1 * bil2; break;
case '/': result = (float) bil1 / bil2; break;
case '%': result = bil1% bil2; break;
case '+': result + = bil1 bil2; break;
case '-': result = bil1 - bil2; break;
default: result = 0;
}
System.out.println ("result of operation is =" + result);
}
}
switch (expression)
{
ungkapan1 case:
pernyataan1;
break;
ungkapan2 case:
pernyataan2;
break;
. . . . . . . . . . . . . . . .
default:
pernyataan_x;
}
information
• Ungkapan1, ungkapan2 and so done in sequence starting from the first, the statementthat follows the suitable sekirannya cese will be executed
• Break ditmukan of the switch statement ends execution
• Default will only be executed if the expression in the cese no match.
java.until.Scanner import;
publib class Arithmetic {
public static void main (Sting args []) {
float result;
Data = new Scenner Scenner (System.in);
int bil1, bil2;
String op;
Char operator;
System.out.print ("Enter 1 bil:");
bil1 = data.nextInt ();
System.out.print ("enter bil 2:");
bil2 = data.nextInt ();
System.out.print ("enter the operator:");
op = data.next ();
operaor = op.charAt (0);
switch (operator) {
case '*': result = bil1 * bil2; break;
case '/': result = (float) bil1 / bil2; break;
case '%': result = bil1% bil2; break;
case '+': result + = bil1 bil2; break;
case '-': result = bil1 - bil2; break;
default: result = 0;
}
System.out.println ("result of operation is =" + result);
}
}
No comments:
Post a Comment