ใน Java มีหลายวิธีควบคุม flow ของโปรแกรม Control flow statements เปลี่ยนหรือหยุด flow ของการ execute โดย implement decision making statements
Decision making statements ใน Java มี:
ifstatementif...elsestatementswitchstatement
โพสต์นี้ให้คำอธิบายและโค้ดตัวอย่างของ Java control flow statements
Java if Statement
Syntax ของ if statement:
if(condition) { // execute some code only if the condition evaluates to true }
Keyword if ใช้เช็คว่า condition เป็น true หรือไม่ หาก true โค้ดใน curly braces จะ execute
ตัวอย่าง:
if(month == 'December') { System.out.println("Winter Season"); }
หมายเหตุ: Condition ใน parentheses ต้องเป็น boolean expression คือ ผลลัพธ์ต้อง evaluate เป็น true หรือ false เท่านั้น
เราใช้ mathematical operators ปกติ evaluate condition:
- Less than -
a < b - Less than or equal to -
a <= b - Greater than -
a > b - Greater than or equal to -
a >= b - Equal to -
a == b - Not Equal to -
a != b
เราสามารถใช้ condition เดียวหรือหลาย conditions แต่ผลลัพธ์ต้องเป็น boolean เสมอ
ตอนใช้ multiple conditions เราใช้ logical AND && และ logical OR || operators
ตัวอย่างใช้ logical OR ใน if statement:
if(month == 'December' || month == 'January') { System.out.println("Winter Season"); }
ตัวอย่างใช้ logical AND ใน if statement:
if(month == 'December' && day == '25') { System.out.println("Christmas Day!"); }
หมายเหตุ: Logical AND && คืน true หาก ทั้งสอง statements เป็น true Logical OR || คืน true หาก อย่างน้อยหนึ่ง statement เป็น true
Java else Statement
หากผลลัพธ์ของ if statement evaluate เป็น false และ เราอยาก action ต่อผลลัพธ์ เราใช้ else statement
else statement ต่อทันทีหลัง closing braces ของ if statement
ตัวอย่าง:
int temperature; if(temperature <= 0) { System.out.println("Water in solid state"); } else { System.out.println("Water in liquid state"); }
ในตัวอย่างข้างต้น หาก temperature เป็น 0 หรือน้อยกว่า 0 จะพิมพ์ "Water in solid state" ไปคอนโซล else statement จะไม่ execute
แต่ถ้า temperature มากกว่า 0 จะพิมพ์ "Water in liquid state" ไปคอนโซล
Short Hand if…else Statement
เราสามารถใช้ shorthand notation สำหรับ if...else statement ที่รู้จักว่า ternary operator
Syntax สำหรับ ternary operator:
variable = (condition) ? expressionTrue : expressionFalse;
ก่อนหน้า evaluate condition ใน () หาก operation evaluate เป็น true execute expression ระหว่าง ? และ : มิฉะนั้น execute condition หลัง :
วิธีช่วยจำ: (condition) ? true : false
Java else if Statement
เราสามารถใช้ multiple if และ else statements ได้ตราบใดที่ condition ยังไม่ถูกตอบสนอง
Syntax สำหรับ else if:
if(condition1) { // execute some code only if condition1 evaluates to true } else if(condition2) { // execute some code if condition2 evaluates to true } else { // execute code if both conditions evaluate to false }
หมายเหตุ: เราสามารถมี else if statements หลายตัว แต่จบด้วย else statement เสมอ
ตัวอย่าง:
int temperature; if(temperature <= 0) { System.out.println("Water in solid state"); } else if(temperature >= 100){ System.out.println("Water in gas state"); } else { System.out.println("Water in liquid state"); }
Java switch Statement
อีกวิธีควบคุม flow ของโปรแกรมคือ switch statement switch statement ใช้เมื่อเรามี options หลายตัว และแต่ละ case execute โค้ดต่างกัน
มันทำงานคล้าย multiple if...else statements
The switch Syntax
Syntax ของ switch statement:
switch(expression) { case a: // execute some code break; case b: // execute some other code break; default: // execute the default code }
ก่อนหน้า evaluate expression ผลลัพธ์ของ expression ถูกเปรียบเทียบกับแต่ละ case หากผลลัพธ์ตรงกับ case ใดๆ บล็อกโค้ดที่เกี่ยวข้องจะ execute
Keyword break ใช้เพื่อออกจาก switch block นี่สำคัญเพราะเมื่อเจอ match แล้ว เราไม่อยาก evaluate case conditions อื่นๆ ต่อ
Keyword default execute หากไม่มี case ไหน match ค่าของ switch expression
ทั้ง break และ default เป็น optional แต่แนะนำสำหรับ good coding practice
Example switch Statement
โค้ดด้านล่างใช้ switch statement ดูว่า language รองรับหรือไม่
String lang = "en"; switch (lang) { case "en": System.out.println("English"); break; case "fr": System.out.println("French"); break; case "de": System.out.println("Deutsch"); break; default: System.out.println("Language not supported"); }
Output:
English
สรุป
ในบทความนี้ เราครอบคลุม Java control flow statements คือ if, else if และ switch statements