SlideShare a Scribd company logo
บทที่
บทที่ 5
5
ประโยคควบคุม – การเลือก
ดำเนินการ
วัตถุประสงค์
วัตถุประสงค์
 เพื่อให้รู้จักโครงสร้างควบคุมชนิดการเลือกดำเนินการ
 เพื่อให้สามารถเขียนโปรแกรมโดยใช้คำสั่งแบบเลือกดำเนิน
การได้
เนื้อหา
เนื้อหา
 5.1 บล็อก (blocks)
 5.2 ประโยค if…then…else…
 5.3 ประโยค if…then…else… แบบต่อเนื่อง
 5.4 ประโยค switch
บทนำ
บทนำ
 ประโยค หรือ statements คือหน่วยของ
ภาษาโปรแกรมที่ใช้กำหนดการทำงานของ
โปรแกรม
 โปรแกรมคอมพิวเตอร์ประกอบด้วยลำดับ
ของประโยค
 ลำดับของประโยคและความหมายของ
ประโยคเป็นตัวกำหนดผลลัพธ์ที่ได้จากการ
ประมวลผลโปรแกรม
5.1 บล็อก (blocks)
 บล็อก (blocks) คือลำดับประโยคที่อยู่ระหว่าง
{ และ }
 ภาษาโปรแกรมใช้ blocks ในการรวมหลายๆ
ประโยคให้ถูกจัดการเหมือนกับเป็นประโยค
เดียว
 สามารถใช้ blocks แทนที่ตำแหน่งของ
ประโยคหนึ่งประโยคได้
 ภายใน blocks อาจมีการประกาศตัวแปรได้
แต่ไม่สามารถประกาศเมทอดได้
5.2
5.2 ประโยค
ประโยค if…then…else…
if…then…else…
 ประโยค if…then…else… ใช้สำหรับการ
เลือกทำงานอย่างใดอย่างหนึ่งระหว่างทาง
เลือกสองทาง
 ใช้ผลลัพธ์ของนิพจน์ทางตรรกศาสตร์กำหนด
ทางเลือก
 ตัวอย่างของประโยคในลักษณะเงื่อนไข
◦ ถ้าฉันตื่นเช้า ฉันคงมาเรียนได้ทันเวลา แต่ถ้าฉัน
ตื่นสาย ฉันคงมาไม่ทันเวลา หรือ
◦ ถ้าตั้งใจและขยันเรียน ฉันจะได้เกรดเฉลี่ยไม่ต่ำ
กว่า B แต่ถ้าฉันขี้เกียจ ฉันคงได้เกรดเฉลี่ยน้อย
กว่า B
5.2
5.2 ประโยค
ประโยค if…then…else…
if…then…else…
 รูปแบบของคำสั่ง if ...then…else …
◦ การทำงานของคำสั่งนี้เริ่มจากการตรวจสอบค่าในนิพจน์ตรรกศาสตร์ หากค่าในนิพจน์ตรรกศาสตร์เป็นจริงจะ
ประมวลผลคำสั่งหรือกลุ่มคำสั่งที่อยู่ภายใต้ then แต่ถ้าหากเป็นเท็จจะประมวลผลคำสั่งหรือกลุ่มคำสั่งที่อยู่ภายใต้ else
จริ
ง
เท็
จ
if ( นิพจน์
ตรรกศาสตร์ ) {
คำสั่ง;
….
}
else {
คำสั่ง;
….
}
5.2
5.2 คำสั่ง
คำสั่ง if…then…else…
if…then…else…
 หากคำสั่งที่ต้องการให้ทำงานภายใต้คำสั่ง if หรือ else มีเพียงคำสั่งเดียว ไม่จำเป็นต้องเขียน
เครื่องหมายวงเล็บ { }
 คำสั่ง if… then…else.. อาจเขียนคำสั่งในรูปแบบที่มีคำสั่ง if เพียงอย่างเดียวไม่จำเป็นต้องมี else
5.2
5.2 คำสั่ง
คำสั่ง if…then…else…
if…then…else…
if ( นิพจน์
ตรรกศาสตร์ ) {
คำสั่ง;
คำสั่ง;
คำสั่ง;
….
}
จริ
ง
เท็
จ
5.2
5.2 คำสั่ง
คำสั่ง if…then…else…
if…then…else…
//Program If1.java
package chapter5;
public class If1 {
public static void main(String args[ ]) {
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
if (y == 0) {
System.out.println("Program Exit: Divided by zero!");
System.exit(0);
}
System.out.println("args[0]/args[1] = " + x/y);
}
}
5.2
5.2 คำสั่ง
คำสั่ง if…then…else…
if…then…else…
//Program If1WithElse.java
public class If1WithElse {
public static void main(String args[]) {
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
if (y == 0)
System.out.println("Program Exit: Divided by zero!");
else
System.out.println("args[0]/args[1] = " + x/y);
}
}
5.2
5.2 คำสั่ง
คำสั่ง if…then…else…
if…then…else…
//Program IfWeight.java
class Weight{
private int gender;
private float height;
public void setGender(int gender) {
this.gender = gender;
}
public void setHeight(float height) {
this.height = height;
}
public float calculate( ) {
float weight;
if (gender == 1)
weight = height-100;
else
weight = height-110;
return(weight);
}
}
//Program IfWeight.java
import java.util.Scanner;
class IfWeight {
static Scanner console = new Scanner(System.in);
public static void main (String [ ] args) {
int gender; float height, weight;
Weight weightCalculator = new Weight( );
System.out.print("Enter '1' for Male or '2' for female : ");
gender = console.nextInt();
if ((gender == 1) || (gender ==2)) {
System.out.print("Please enter your height : ");
height = console.nextFloat();
weightCalculator.setGender(gender);
weightCalculator.setHeight(height);
weight = weightCalculator.calculate();
System.out.print("Your weight should not more
than "+ weight + " kgs.");
} else System.out.print("Invalid gender.");
}
}
//Program IfNumber.java
class CheckNumber {
private int number;
private boolean even;
public void setNumber(int number) {
this.number = number;
}
public boolean checkEven() {
if (number % 2 == 0)
even = true; //even number
else
even = false; //odd number
return(even);
}
}
//Program IfNumber.java
package chapter5;
import java.util.Scanner;
public class IfNumber {
static Scanner console = new Scanner(System.in);
public static void main ( String [ ] args ) {
int number;
CheckNumber checkNum = new CheckNumber( );
System.out.print("Enter integer number for checking : ");
number = console.nextInt();
checkNum.setNumber(number);
if ( checkNum.checkEven( ) ) {
System.out.println("The no. "+ number + " is an
even number.");
}
else {
System.out.println("The no. "+ number + " is an
odd number.");
}
} //main
}
5.3
5.3 คำสั่ง
คำสั่ง if ..then..else
if ..then..else แบบต่อ
แบบต่อ
เนื่อง
เนื่อง
 มีทางเลือกมากกว่า 2 ทาง เพื่อแยกกรณีของทางเลือก
if ( นิพจน์ตรรกศาสตร์ ) {
คำสั่ง;
คำสั่ง;
….
} else if ( นิพจน์
ตรรกศาสตร์ ) {
คำสั่ง;
คำสั่ง;
….
} else if ( นิพจน์
ตรรกศาสตร์ ) {
คำสั่ง;
คำสั่ง;
….
} else {
คำสั่ง;
คำสั่ง;
….
}
เท็
จ
เท็
จ
จริ
ง
จริ
ง
จริ
ง
เท็
จ
//Program IfGrade.java
public class IfGrade {
public static void main(String args[]) {
int x = Integer.parseInt(args[0]);
char grade;
if (x >= 80) {
grade = 'A';
System.out.println("Score : "+ x + ", Grade = " +
grade);
} else if (x >= 70) {
grade = 'B';
System.out.println("Score : "+ x + ", Grade = " +
grade);
} else if (x >= 60) {
grade = 'C';
System.out.println("Score : "+ x + ", Grade = " +
grade);
} else if (x >= 50) {
grade = 'D';
System.out.println("Score : "+ x + ", Grade = " +
grade);
} else {
grade = 'F';
System.out.println("Score : "+ x + ", Grade = " +
grade);
}
}
}
//Program IfGradeClass.java
package chapter5;
public class IfGradeClass {
public static void main ( String [ ] args ) {
int x = Integer.parseInt(args[0]);
char grade;
Grade gradeCal = new Grade();
gradeCal.setScore(x);
grade = gradeCal.calGrade();
System.out.println("Score : "+ x + ", Grade = " + grade);
}
}
class Grade {
private int score;
private char grade;
public void setScore(int score) {
this.score = score;
}
public char calGrade( ) {
if ( score <= 50 )
grade = 'F';
else if ( score < 60 )
grade = 'D';
else if ( score < 70 )
grade = 'C';
else if ( score < 80 )
grade = 'B';
else
grade = 'A';
return(grade);
}
}
5.3
5.3 คำสั่ง
คำสั่ง if..then..else
if..then..else แบบต่อเนื่อง
แบบต่อเนื่อง
 ตัวอย่างที่ 5.7 ให้พิจารณาข้อมูลนักศึกษา ถ้าเป็นนักศึกษาชั้นปีที่ 1 และมี
เกรดเฉลี่ยไม่ต่ำกว่า 1.75 ให้ขึ้นข้อความแนะนำว่าควรจะลงทะเบียนไม่เกิน
18 หน่วยกิต แต่ถ้าเป็นนักศึกษาชั้นปีอื่น และมีเกรดเฉลี่ยไม่ต่ำกว่า 2.00 ให้
ขึ้นข้อความแนะนำว่าควรจะลงทะเบียนไม่เกิน 21 หน่วยกิต
 if (year == 1 && gpa >= 1.75)
 System.out.println (“Units
 registered should not be
 more than 18.”);
 else if (year >= 2 && gpa >= 2.00)
 System.out.println (“Unit
registered
 should not be
 more than 21.”);
 if (year == 1) {
 if (gpa >= 1.75)
 System.out.println(“Units
 registered should not be
more
 than 18.”);
 }
 else { /* ไม่ใช่ปี 1 */
 if (gpa >= 2.00)
 System.out.println(“Unit
 registered should not be
 more than 21.”);
5.4
5.4 ประโยค
ประโยค switch
switch
 คำสั่ง switch ใช้สำหรับการเลือกทำงานเช่นเดียวกับคำ
สั่ง if…then…else… โดยใช้ในกรณีที่มีหลายทางเลือก
switch ( นิพจน์เลขจำนวนเต็ม )
{
case <ค่าคงที่> : <ลำดับคำ
สั่ง>;
case <ค่าคงที่> : <ลำดับคำ
สั่ง>;
case <ค่าคงที่> : <ลำดับคำ
สั่ง>;
break;
…………………………...
default : <ลำดับคำสั่ง>;
5.4
5.4 ประโยค
ประโยค switch
switch
 ผลลัพธ์ของนิพจน์เลขจำนวนเต็มที่ตามหลัง
switch ต้องมีชนิดข้อมูลเป็นจำนวนเต็ม (byte,
short, int, long) หรือ ตัวอักษร (char) เท่านั้น
 ระบุค่าคงที่สำหรับแต่ละ case ได้ case ละ 1 ค่า
เท่านั้น ไม่สามารถระบุเป็นช่วงของข้อมูลได้
 ค่าคงที่ของแต่ละ case ต้องไม่ซ้ำกัน
 มี default ได้หนึ่ง default เท่านั้น
 ทำงานในลำดับคำสั่งที่ระบุไว้สำหรับ case นั้น
ๆ แล้วทำต่อที่ลำดับคำสั่งของ case ต่อไป เรียง
กันไปเรื่อยจนกระทั่งพบคำสั่ง break หรือเมื่อ
จบ บล็อก switch
//Program SwitchBreak.java
class SwitchBreak {
public static void main(String args[ ]) {
String input = args[0].toUpperCase();
char x = input.charAt(0);
switch(x) {
case 'A' : System.out.println("Hello!");
System.out.println("You entered " + x);
break;
case 'B' : System.out.println("Good Morning");
System.out.println("You entered " + x);
break;
case 'C' : System.out.println("Good Afternoon");
System.out.println("You entered " + x);
break;
case 'D' : System.out.println("Good Evening");
System.out.println("You entered " + x);
break;
case 'E' : System.out.println
("Good Night");
System.out.println
("You entered " + x);
break;
case 'F' : System.out.println
("Good Day");
System.out.println
("You entered " + x);
break;
default : System.out.println
("Good Bye!");
System.out.println
("You entered " + x);
}
}// main
}// class
class SwitchGreeting1 {
public static void main(String args[]) {
int x = Integer.parseInt(args[0]);
switch(x) {
case 1 : System.out.println("Action is better than word."); break;
case 2 : System.out.println("Action is better than word."); break;
case 3 : System.out.println("Action is better than word."); break;
case 4 : System.out.println("Beauty is in the eyes of beholder."); break;
case 5 : System.out.println("Beauty is in the eyes of beholder."); break;
case 6 : System.out.println("Beauty is in the eyes of beholder."); break;
case 7 : System.out.println("Life is a long journey."); break;
case 8 : System.out.println("Life is a long journey."); break;
case 9 : System.out.println("Life is a long journey."); break;
case 10 : System.out.println("Life is a long journey."); break;
default : System.out.println("All the best.");
}
}
}
 class SwitchGreeting2 {
 public static void main(String args[]) {
 int x = Integer.parseInt(args[0]);
 switch(x) {
 case 1 :
 case 2 :
 case 3 : System.out.println("Action is better than word.");
 break;
 case 4 :
 case 5 :
 case 6 : System.out.println("Beauty is in the eyes of beholder.");
 break;
 case 7 :
 case 8 :
 case 9 :
 case 10 : System.out.println("Life is a long journey.");
 break;
 default : System.out.println("All the best.");
 }
 }
 }
 class SwitchIfGreeting {
 public static void main(String args[]) {
 int x = Integer.parseInt(args[0]);
 if (x == 1 | x == 2 | x == 3)
 System.out.println("Action is better than
word.");
 else if (x == 4 | x == 5 | x == 6)
 System.out.println("Beauty is in the eyes of
 beholder.");
 else if (x == 7 | x == 8 | x == 9 | x == 10)
 System.out.println("Life is a long journey.");
 else System.out.println("All the best.");
 }
 }
class Payment {
private int custType;
private float price, discountRate,
private float discountAmount, pay;
public void setType(int type) {
custType = type;
}
public void setPrice(float price) {
this.price = price;
}
public float getPrice( ) {
return(price);
}
public float getDiscount( ) {
return(discountAmount);
}
public float getPay( ) { return(pay); }
public void totalPay( ) {
switch (custType) {
case 1 : discountRate = 0.05f;
break;
case 2 : discountRate = 0.10f;
break;
}
discountAmount = price * discountRate;
pay = price - discountAmount;
} totalPay()
} //class
The end.
The end.

More Related Content

PPTX
Computer Programming 3
PPT
Control structure
PDF
javabasic
PDF
Know009
PDF
Tech30101 ch8
PPT
บทที่ 3 คำสั่งควบคุม ส่วนที่ 1
PPTX
การเขียนคำสั่งควบคุมแบบมีทางเลือก
PDF
chapter 3 คำสั่งควบคุม
Computer Programming 3
Control structure
javabasic
Know009
Tech30101 ch8
บทที่ 3 คำสั่งควบคุม ส่วนที่ 1
การเขียนคำสั่งควบคุมแบบมีทางเลือก
chapter 3 คำสั่งควบคุม

Similar to Program Statement Structure พื้นฐานโครงสร้างประโยคคำสั่งภาษาโปรแกรมคอมพิวเตอร์ (20)

PDF
Java-Chapter 09 Advanced Statements and Applications
PDF
Java-Answer Chapter 05-06
PDF
ฟังก์ชั่น If หลายทางเลือก
PPT
การเขียนคำสั่งควบคุมแบบมีทางเลือก กลุ่ม 3
PDF
บทที่ 3 คำสั่งควบค
PDF
งานคอม 1
PPTX
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม
PDF
ฟังก์ชั่น Switch
PDF
ฟังก์ชัน27
PDF
02 controlflow php
PPT
Java Programming [3/12]: Control Structures
PPTX
การเขียนคำสั่งควบคุมแบบมีทางเลือก
PPT
04 conditional
PDF
PPTX
งาน อ.ทรงศักดิ์
PDF
บทที่ 3 คำสั่งควบคุมโปรแกรม
PDF
ฟังก์ชั่น If ทางเลือกเดียว
PDF
คำสั่งควบคุมโปรแกรม
Java-Chapter 09 Advanced Statements and Applications
Java-Answer Chapter 05-06
ฟังก์ชั่น If หลายทางเลือก
การเขียนคำสั่งควบคุมแบบมีทางเลือก กลุ่ม 3
บทที่ 3 คำสั่งควบค
งานคอม 1
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม
ฟังก์ชั่น Switch
ฟังก์ชัน27
02 controlflow php
Java Programming [3/12]: Control Structures
การเขียนคำสั่งควบคุมแบบมีทางเลือก
04 conditional
งาน อ.ทรงศักดิ์
บทที่ 3 คำสั่งควบคุมโปรแกรม
ฟังก์ชั่น If ทางเลือกเดียว
คำสั่งควบคุมโปรแกรม
Ad

Program Statement Structure พื้นฐานโครงสร้างประโยคคำสั่งภาษาโปรแกรมคอมพิวเตอร์

  • 1. บทที่ บทที่ 5 5 ประโยคควบคุม – การเลือก ดำเนินการ
  • 3. เนื้อหา เนื้อหา  5.1 บล็อก (blocks)  5.2 ประโยค if…then…else…  5.3 ประโยค if…then…else… แบบต่อเนื่อง  5.4 ประโยค switch
  • 4. บทนำ บทนำ  ประโยค หรือ statements คือหน่วยของ ภาษาโปรแกรมที่ใช้กำหนดการทำงานของ โปรแกรม  โปรแกรมคอมพิวเตอร์ประกอบด้วยลำดับ ของประโยค  ลำดับของประโยคและความหมายของ ประโยคเป็นตัวกำหนดผลลัพธ์ที่ได้จากการ ประมวลผลโปรแกรม
  • 5. 5.1 บล็อก (blocks)  บล็อก (blocks) คือลำดับประโยคที่อยู่ระหว่าง { และ }  ภาษาโปรแกรมใช้ blocks ในการรวมหลายๆ ประโยคให้ถูกจัดการเหมือนกับเป็นประโยค เดียว  สามารถใช้ blocks แทนที่ตำแหน่งของ ประโยคหนึ่งประโยคได้  ภายใน blocks อาจมีการประกาศตัวแปรได้ แต่ไม่สามารถประกาศเมทอดได้
  • 6. 5.2 5.2 ประโยค ประโยค if…then…else… if…then…else…  ประโยค if…then…else… ใช้สำหรับการ เลือกทำงานอย่างใดอย่างหนึ่งระหว่างทาง เลือกสองทาง  ใช้ผลลัพธ์ของนิพจน์ทางตรรกศาสตร์กำหนด ทางเลือก  ตัวอย่างของประโยคในลักษณะเงื่อนไข ◦ ถ้าฉันตื่นเช้า ฉันคงมาเรียนได้ทันเวลา แต่ถ้าฉัน ตื่นสาย ฉันคงมาไม่ทันเวลา หรือ ◦ ถ้าตั้งใจและขยันเรียน ฉันจะได้เกรดเฉลี่ยไม่ต่ำ กว่า B แต่ถ้าฉันขี้เกียจ ฉันคงได้เกรดเฉลี่ยน้อย กว่า B
  • 7. 5.2 5.2 ประโยค ประโยค if…then…else… if…then…else…  รูปแบบของคำสั่ง if ...then…else … ◦ การทำงานของคำสั่งนี้เริ่มจากการตรวจสอบค่าในนิพจน์ตรรกศาสตร์ หากค่าในนิพจน์ตรรกศาสตร์เป็นจริงจะ ประมวลผลคำสั่งหรือกลุ่มคำสั่งที่อยู่ภายใต้ then แต่ถ้าหากเป็นเท็จจะประมวลผลคำสั่งหรือกลุ่มคำสั่งที่อยู่ภายใต้ else จริ ง เท็ จ if ( นิพจน์ ตรรกศาสตร์ ) { คำสั่ง; …. } else { คำสั่ง; …. }
  • 8. 5.2 5.2 คำสั่ง คำสั่ง if…then…else… if…then…else…  หากคำสั่งที่ต้องการให้ทำงานภายใต้คำสั่ง if หรือ else มีเพียงคำสั่งเดียว ไม่จำเป็นต้องเขียน เครื่องหมายวงเล็บ { }  คำสั่ง if… then…else.. อาจเขียนคำสั่งในรูปแบบที่มีคำสั่ง if เพียงอย่างเดียวไม่จำเป็นต้องมี else
  • 9. 5.2 5.2 คำสั่ง คำสั่ง if…then…else… if…then…else… if ( นิพจน์ ตรรกศาสตร์ ) { คำสั่ง; คำสั่ง; คำสั่ง; …. } จริ ง เท็ จ
  • 10. 5.2 5.2 คำสั่ง คำสั่ง if…then…else… if…then…else… //Program If1.java package chapter5; public class If1 { public static void main(String args[ ]) { int x = Integer.parseInt(args[0]); int y = Integer.parseInt(args[1]); if (y == 0) { System.out.println("Program Exit: Divided by zero!"); System.exit(0); } System.out.println("args[0]/args[1] = " + x/y); } }
  • 11. 5.2 5.2 คำสั่ง คำสั่ง if…then…else… if…then…else… //Program If1WithElse.java public class If1WithElse { public static void main(String args[]) { int x = Integer.parseInt(args[0]); int y = Integer.parseInt(args[1]); if (y == 0) System.out.println("Program Exit: Divided by zero!"); else System.out.println("args[0]/args[1] = " + x/y); } }
  • 13. //Program IfWeight.java class Weight{ private int gender; private float height; public void setGender(int gender) { this.gender = gender; } public void setHeight(float height) { this.height = height; } public float calculate( ) { float weight; if (gender == 1) weight = height-100; else weight = height-110; return(weight); } }
  • 14. //Program IfWeight.java import java.util.Scanner; class IfWeight { static Scanner console = new Scanner(System.in); public static void main (String [ ] args) { int gender; float height, weight; Weight weightCalculator = new Weight( ); System.out.print("Enter '1' for Male or '2' for female : "); gender = console.nextInt(); if ((gender == 1) || (gender ==2)) { System.out.print("Please enter your height : "); height = console.nextFloat(); weightCalculator.setGender(gender); weightCalculator.setHeight(height); weight = weightCalculator.calculate(); System.out.print("Your weight should not more than "+ weight + " kgs."); } else System.out.print("Invalid gender."); } }
  • 15. //Program IfNumber.java class CheckNumber { private int number; private boolean even; public void setNumber(int number) { this.number = number; } public boolean checkEven() { if (number % 2 == 0) even = true; //even number else even = false; //odd number return(even); } }
  • 16. //Program IfNumber.java package chapter5; import java.util.Scanner; public class IfNumber { static Scanner console = new Scanner(System.in); public static void main ( String [ ] args ) { int number; CheckNumber checkNum = new CheckNumber( ); System.out.print("Enter integer number for checking : "); number = console.nextInt(); checkNum.setNumber(number); if ( checkNum.checkEven( ) ) { System.out.println("The no. "+ number + " is an even number."); } else { System.out.println("The no. "+ number + " is an odd number."); } } //main }
  • 17. 5.3 5.3 คำสั่ง คำสั่ง if ..then..else if ..then..else แบบต่อ แบบต่อ เนื่อง เนื่อง  มีทางเลือกมากกว่า 2 ทาง เพื่อแยกกรณีของทางเลือก if ( นิพจน์ตรรกศาสตร์ ) { คำสั่ง; คำสั่ง; …. } else if ( นิพจน์ ตรรกศาสตร์ ) { คำสั่ง; คำสั่ง; …. } else if ( นิพจน์ ตรรกศาสตร์ ) { คำสั่ง; คำสั่ง; …. } else { คำสั่ง; คำสั่ง; …. } เท็ จ เท็ จ จริ ง จริ ง จริ ง เท็ จ
  • 18. //Program IfGrade.java public class IfGrade { public static void main(String args[]) { int x = Integer.parseInt(args[0]); char grade; if (x >= 80) { grade = 'A'; System.out.println("Score : "+ x + ", Grade = " + grade); } else if (x >= 70) { grade = 'B'; System.out.println("Score : "+ x + ", Grade = " + grade); } else if (x >= 60) { grade = 'C'; System.out.println("Score : "+ x + ", Grade = " + grade); } else if (x >= 50) { grade = 'D'; System.out.println("Score : "+ x + ", Grade = " + grade); } else { grade = 'F'; System.out.println("Score : "+ x + ", Grade = " + grade); } } }
  • 19. //Program IfGradeClass.java package chapter5; public class IfGradeClass { public static void main ( String [ ] args ) { int x = Integer.parseInt(args[0]); char grade; Grade gradeCal = new Grade(); gradeCal.setScore(x); grade = gradeCal.calGrade(); System.out.println("Score : "+ x + ", Grade = " + grade); } }
  • 20. class Grade { private int score; private char grade; public void setScore(int score) { this.score = score; } public char calGrade( ) { if ( score <= 50 ) grade = 'F'; else if ( score < 60 ) grade = 'D'; else if ( score < 70 ) grade = 'C'; else if ( score < 80 ) grade = 'B'; else grade = 'A'; return(grade); } }
  • 21. 5.3 5.3 คำสั่ง คำสั่ง if..then..else if..then..else แบบต่อเนื่อง แบบต่อเนื่อง  ตัวอย่างที่ 5.7 ให้พิจารณาข้อมูลนักศึกษา ถ้าเป็นนักศึกษาชั้นปีที่ 1 และมี เกรดเฉลี่ยไม่ต่ำกว่า 1.75 ให้ขึ้นข้อความแนะนำว่าควรจะลงทะเบียนไม่เกิน 18 หน่วยกิต แต่ถ้าเป็นนักศึกษาชั้นปีอื่น และมีเกรดเฉลี่ยไม่ต่ำกว่า 2.00 ให้ ขึ้นข้อความแนะนำว่าควรจะลงทะเบียนไม่เกิน 21 หน่วยกิต  if (year == 1 && gpa >= 1.75)  System.out.println (“Units  registered should not be  more than 18.”);  else if (year >= 2 && gpa >= 2.00)  System.out.println (“Unit registered  should not be  more than 21.”);  if (year == 1) {  if (gpa >= 1.75)  System.out.println(“Units  registered should not be more  than 18.”);  }  else { /* ไม่ใช่ปี 1 */  if (gpa >= 2.00)  System.out.println(“Unit  registered should not be  more than 21.”);
  • 22. 5.4 5.4 ประโยค ประโยค switch switch  คำสั่ง switch ใช้สำหรับการเลือกทำงานเช่นเดียวกับคำ สั่ง if…then…else… โดยใช้ในกรณีที่มีหลายทางเลือก switch ( นิพจน์เลขจำนวนเต็ม ) { case <ค่าคงที่> : <ลำดับคำ สั่ง>; case <ค่าคงที่> : <ลำดับคำ สั่ง>; case <ค่าคงที่> : <ลำดับคำ สั่ง>; break; …………………………... default : <ลำดับคำสั่ง>;
  • 23. 5.4 5.4 ประโยค ประโยค switch switch  ผลลัพธ์ของนิพจน์เลขจำนวนเต็มที่ตามหลัง switch ต้องมีชนิดข้อมูลเป็นจำนวนเต็ม (byte, short, int, long) หรือ ตัวอักษร (char) เท่านั้น  ระบุค่าคงที่สำหรับแต่ละ case ได้ case ละ 1 ค่า เท่านั้น ไม่สามารถระบุเป็นช่วงของข้อมูลได้  ค่าคงที่ของแต่ละ case ต้องไม่ซ้ำกัน  มี default ได้หนึ่ง default เท่านั้น  ทำงานในลำดับคำสั่งที่ระบุไว้สำหรับ case นั้น ๆ แล้วทำต่อที่ลำดับคำสั่งของ case ต่อไป เรียง กันไปเรื่อยจนกระทั่งพบคำสั่ง break หรือเมื่อ จบ บล็อก switch
  • 24. //Program SwitchBreak.java class SwitchBreak { public static void main(String args[ ]) { String input = args[0].toUpperCase(); char x = input.charAt(0); switch(x) { case 'A' : System.out.println("Hello!"); System.out.println("You entered " + x); break; case 'B' : System.out.println("Good Morning"); System.out.println("You entered " + x); break; case 'C' : System.out.println("Good Afternoon"); System.out.println("You entered " + x); break; case 'D' : System.out.println("Good Evening"); System.out.println("You entered " + x); break; case 'E' : System.out.println ("Good Night"); System.out.println ("You entered " + x); break; case 'F' : System.out.println ("Good Day"); System.out.println ("You entered " + x); break; default : System.out.println ("Good Bye!"); System.out.println ("You entered " + x); } }// main }// class
  • 25. class SwitchGreeting1 { public static void main(String args[]) { int x = Integer.parseInt(args[0]); switch(x) { case 1 : System.out.println("Action is better than word."); break; case 2 : System.out.println("Action is better than word."); break; case 3 : System.out.println("Action is better than word."); break; case 4 : System.out.println("Beauty is in the eyes of beholder."); break; case 5 : System.out.println("Beauty is in the eyes of beholder."); break; case 6 : System.out.println("Beauty is in the eyes of beholder."); break; case 7 : System.out.println("Life is a long journey."); break; case 8 : System.out.println("Life is a long journey."); break; case 9 : System.out.println("Life is a long journey."); break; case 10 : System.out.println("Life is a long journey."); break; default : System.out.println("All the best."); } } }
  • 26.  class SwitchGreeting2 {  public static void main(String args[]) {  int x = Integer.parseInt(args[0]);  switch(x) {  case 1 :  case 2 :  case 3 : System.out.println("Action is better than word.");  break;  case 4 :  case 5 :  case 6 : System.out.println("Beauty is in the eyes of beholder.");  break;  case 7 :  case 8 :  case 9 :  case 10 : System.out.println("Life is a long journey.");  break;  default : System.out.println("All the best.");  }  }  }
  • 27.  class SwitchIfGreeting {  public static void main(String args[]) {  int x = Integer.parseInt(args[0]);  if (x == 1 | x == 2 | x == 3)  System.out.println("Action is better than word.");  else if (x == 4 | x == 5 | x == 6)  System.out.println("Beauty is in the eyes of  beholder.");  else if (x == 7 | x == 8 | x == 9 | x == 10)  System.out.println("Life is a long journey.");  else System.out.println("All the best.");  }  }
  • 28. class Payment { private int custType; private float price, discountRate, private float discountAmount, pay; public void setType(int type) { custType = type; } public void setPrice(float price) { this.price = price; } public float getPrice( ) { return(price); } public float getDiscount( ) { return(discountAmount); } public float getPay( ) { return(pay); } public void totalPay( ) { switch (custType) { case 1 : discountRate = 0.05f; break; case 2 : discountRate = 0.10f; break; } discountAmount = price * discountRate; pay = price - discountAmount; } totalPay() } //class