งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Lecture 6: ทางเลือกแบบมีโครงสร้างซ้อนใน

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Lecture 6: ทางเลือกแบบมีโครงสร้างซ้อนใน"— ใบสำเนางานนำเสนอ:

1 Lecture 6: ทางเลือกแบบมีโครงสร้างซ้อนใน
หลักการโปรแกรม 1 Lecture 6: ทางเลือกแบบมีโครงสร้างซ้อนใน

2 Nested if Nested Branches
คือการที่มีคำสั่ง if ซ้อนอยู่ใน block ของ if หรือ block ของ else เช่น T F

3 Tax brackets for single filers: from $0 to $32,000 above $32,000
then tax depends on income Tax brackets for married filers: from $0 to $64,000 above $64,000 then tax depends on income

4

5

6

7

8

9 const double RATE1_SINGLE_LIMIT = 32000;
#include <iostream> #include <string> using namespace std; int main() { const double RATE1 = 0.10; const double RATE2 = 0.25; const double RATE1_SINGLE_LIMIT = 32000; const double RATE1_MARRIED_LIMIT = 64000; double tax1 = 0; double tax2 = 0; double income; cout << "Please enter your income: "; cin >> income; cout << "Please enter s for single, m for married: "; string marital_status; cin >> marital_status;

10 if (marital_status == "s")
{ if (income <= RATE1_SINGLE_LIMIT) tax1 = RATE1 * income; } else tax1 = RATE1 * RATE1_SINGLE_LIMIT; tax2 = RATE2 * (income - RATE1_SINGLE_LIMIT);

11 แบบฝึกหัด จงเขียนโปรแกรมรับตัวเลือก c จาก keyboard ถ้า c<10 ให้แสดงค่า 2c; ถ้า 10<=c<100 ให้แสดงค่า c2; ถ้า c>=100 ให้แสดงค่า c

12 start stop read c c<10 print 2*c T F print c*c c<100 print c

13 int main() { int c; cout << “Input an integer : ”; cin >> c; if ( c < 10 ) cout << 2*c << endl; else { if ( c < 100 ) cout << c*c << endl; cout << c << endl; } return 0;

14 พิจารณา else x = 3; } else เป็นคู่กับ if ตัวไหน ? a == 10 c > 100
T F a == 10 c > 100 x = 2 x = 3 x = 4 b < 20 x = 1 if (a == 10) { if (b < 20) x = 1; if (c > 100) x = 2; else x = 3; } else x = 4;

15 พิจารณา if (x) else เป็นคู่กับ if ตัวไหน ? if (y) statement1;
else statement2; else เป็นคู่กับ if (y) เพราะต้องคู่กับ if ที่อยู่ใกล้ที่สุด

16 พิจารณา if (x) else เป็นคู่กับ if ตัวไหน ? { if (y) statement1; }
else statement2; else เป็นคู่กับ if (x) เพราะภายใน block คร่อมเฉพาะ if (y) ซึ่งไม่มี else

17 แบบฝึกหัด if (a < b) if (c > d) if (d == e) a = a + b; else b = c + d; d = a + b;

18 แบบฝึกหัด ให้เขียนโปรแกรมสำหรับตรวจสอบค่าจุด (x, y) ที่รับเข้ามาว่าอยู่ใน quadrant ใด Q1, Q2, Q3 หรือ Q4 (ดังรูป) หรืออยู่บนแกน x (x-axis), แกน y (x-axis) หรือ เป็น Origin point (0, 0) หมายเหตุ ให้รับค่า x และ y เป็นจำนวนเต็ม


ดาวน์โหลด ppt Lecture 6: ทางเลือกแบบมีโครงสร้างซ้อนใน

งานนำเสนอที่คล้ายกัน


Ads by Google