Java - Decision Making
คำสั่งตัดสินใจ เป็นประโยคคำสั่งที่ใช้ควบคุมให้โปรแกรมเลือกดำเนินไปในเส้นทางใดเส้นทางหนึ่ง โดยขึ้นอยู่กับผลการตรวจสอบเงื่อนไข
คำสั่งตัดสินใจในภาษา Java ได้แก่
- คำสั่ง if
- คำสั่ง switch
คำสั่ง if..
Syntax
if(Boolean_expression)
{
//Statements will execute if the Boolean expression is true
}
การทำงาน : เริ่มต้นจะทำการตรวจสอบ condition ในวงเล็บ ผลการตรวจสอบจะได้ true หรือ false ถ้าเป็น true จะทำ statement แต่ถ้าเป็น false จะไปทำคำสั่งอื่นๆถัดไป
ตัวอย่าง
public class Test {
public static void main(String args[]){
int x = 10
if( x < 20)
{
System.out.print("This is if statement");
}
}
}
ผลการรัน
This is if statement
คำสั่ง if..else
Syntax
if(Boolean_expression){
//Executes when the Boolean expression is true
}else{
//Executes when the Boolean expression is false
}
ตัวอย่าง
public class Test {
public static void main(String args[]){
int x = 30;
if( x < 20)
{
System.out.print("This is if statement");
}else{
System.out.print("This is else statement");
}
}
}
ผลการรัน
This is else statement
คำสั่ง if.. else if.. else
if..else if..else เป็นคำสั่งในภาษา Java โดย if...else if...else ใช้สร้างเงื่อนไข โดยสามารถ else if กำหนดและสร้างทางเลือกได้หลายทาง และหลาย block โดยในแต่ล่ะ block สามารถสร้างเงื่อนไขของตัวเองได้
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.
}
ตัวอย่าง
public class Test {
public static void main(String args[]){
int x = 30;
if( x == 10)
{
System.out.print("Value of X is 10");
}else if( x == 20){
System.out.print("Value of X is 20");
}else if( x == 30){
System.out.print("Value of X is 30");
}else{
System.out.print("This is else statement");
}
}
}
ผลการรัน
Value of X is 30
คำสั่ง Nested if...else
Nested if...else เป็นคำสั่งในภาษา Java โดย Nested if...else ใช้สร้างเงื่อนไขซ้อนเงื่อนไข โดยเงื่อนไขทั้งสอง จะต้องเป็นจริง ถึงจะทำงานใน Statement ที่ต้องการ
Syntax
if(Boolean_expression 1){
//Executes when the Boolean expression 1 is true
if(Boolean_expression 2){
//Executes when the Boolean expression 2 is true
}
}
ตัวอย่าง
public class Test {
public static void main(String args[]){
int x = 30;
int y = 10;
if( x == 30){
if( y == 10){
System.out.print("X = 30 and Y = 10");
}
}
}
}
ผลการรัน
X = 30 and Y = 10
คำสั่ง switch
Switch Case คือ คำสั่งสำหรับการสร้างเงื่อนไขแบบทำหลายทิศทาง ซึ่งจะไม่เหมือนกับ If - Then - Else Statement เพราะ Switch Statement นั้นจะสามารถทำได้หลายเงื่อนไขที่เป็นจริง ซึ่งสามารถทำงานกับชนิดข้อมูลต่าง ๆ เช่น byte, short, char, และ int primitive data types
ภาพรวมของ Switch Statement
1. ประกอบไปด้วย 3 คำสั่งหลัก ๆ คือ switch, case และ break
2. switch ใช้สำหรับระบุค่าที่ต้องการสร้างเงื่อนไข
3. case ใช้สำหรับระบุขั้นตอนการดำเนินงานในแต่ละทิศทาง
4. break ใช้สำหรับจบขั้นตอนการดำเนินงานในเหตุการณ์นั้น ๆ
5. ใช้คำสั่ง default เพื่อระบุถึงขั้นตอนการดำเนินงานที่ไม่พบทิศทางใด ๆ
ตัวอย่าง
public class Test {
public static void main(String args[]){
//char grade = args[0].charAt(0);
char grade = 'C';
switch(grade)
{
case 'A' :
System.out.println("Excellent!");
break;
case 'B' :
case 'C' :
System.out.println("Well done");
break;
case 'D' :
System.out.println("You passed");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
}
}
ผลการรัน
Well done
Your grade is a C
จะเห็นว่าเงือนไขในการตัดสินใจไม่ยากเลยใช่ไหมครับ ศึกษาให้แม่นเพราะ เราจะอยู่กับมันไปตลอดในการเขียนโปรแกรมกันเลย