SlideShare a Scribd company logo
1 of 11
Download to read offline
!

                                              บทที่ 3
                                            การเลือกทํา

♦! วามหมายของการเลอกทํา
 ค                ื
        การเลือกทํา หมายถึง การกาหนดใหโปรแกรมทางานอยางหนึงเมือเงือนไข (Condition) เปน
                                  ํ                 ํ         ่ ่ ่                   
จริงและทา (หรือไมตองทํางานใด ๆ ) เมือเงือนไข เปนเท็จ งานโปรแกรมคอมพิวเตอรมการกําหนดให
         ํ                          ่ ่                                     ี
เลือกทาอยูมากมายการถามผูใช เชน Save (Y/N) _ Continue(Y/N) _ เปนตน
      ํ                         


♦! ารเลอกทําแบบ if
 ก ื
        การเลือกทําแบบ if เริ่มดวยการทดสอบเงื่อนไขที่ไดกาหนดไวกอน อาจมีมากกวา 1 เงอนไขก็
                                                           ํ                       ่ื
ได ผลการตรวจสอบเงือนไข ถาผล
                      ่
        - เปนจริง statement ตอจาก if จะถูกทํางาน
        - เปนเท็จ statement ตอจาก if จะไมถูกทํางานหรือไมตองทางานใด ๆ
                                                                 ํ

     รปแบบ 1 แบบ statement เดียว มีรูปแบบ ดังนี้
      ู
                         if (เงือนไขการเปรียบเทียบ)
                                ่
                                  statement;

                  รปแบบ 2 แบบหลาย statement มีรูปแบบ ดังนี้
                    ู
                              if (เงือนไขการเปรียบเทียบ)
                                     ่
                                 { statement;
                                    statement;
                                    statement;
                                    …
     !            }




ศิริชัย นามบุรี                                                                     การเลือกทํา
!                                             34

           •! ตัวอยางโปรแกรม test_if1.cpp แสดงการรับขอมูลจํานวนเต็ม 2 จานวน และเปรียบเทียบ
                                                                         ํ
              กันโดยใช if แบบประโยค(statement) เดยว
                                                  ี

/*Program : test_if1.cpp
  Process : copare 2 integer
*/
#include <iostream.h>
#include <conio.h>
void main()
{ int x,y;
  int result = 100;
  //start statement
  clrscr();
  cout<< "Please enter integer number : n";
  cout<< "Finst number : ";
  cin>>x;
  cout<< "nSecond number : ";
  cin>>y;
  //condition of if
  if (x>y) //condition
     cout<<"nFirst number is greater than second numberan";
  if (x+y>result) //condition
     cout<<"nFirst number + Seconde number is greater than an"<<result;
  getch();
}
          •! โปรแกรม test_if2.cpp แสดงการเปรยบเทยบจํานวนตัวเลข 2 จานวน และใช if แบบ
                                            ี ี                   ํ
              หลาย ประโยค(Compound statement)
/*Program : test_if2.cpp
 Process : copare 2 integer*/
#include <iostream.h>
#include <conio.h>
void main()
{ int x,y;
 int result = 100;
 //start statement
 clrscr();
 cout<< "Please enter integer number : n";
 cout<< "Finst number : ";
 cin>>x;
 cout<< "nSecond number : ";
 cin>>y;
 //condition of if
ศิริชัย นามบุรี                                                                      การเลือกทํา
!                                                  35
    if (x>y)
       { clrscr();
         cout<<"nFirst number is : "<<x;
         cout<<"nSecond number is : "<<y;
         cout<<"nFirst number is greater than second numberan";
        }
    if (x+y<=result)
       { clrscr();
         cout<<"nFirst number is : "<<x;
         cout<<"nSecond number is : "<<y;
         cout<<"nFirst number + Seconde number <= an"<<result;
       }
    getch();
}

♦! ารเลอกทําแบบ if…else
 ก ื
      การเลือกทําแบบ if …else มีหลักการทางาน คอเรมดวยการทดสอบเงอนไขทกําหนดไว ถาผล
                                              ํ      ื ่ิ     ่ื   ่ี
การตรวจสอบเงอนไข เปนดงน้ี
              ่ื       ั
      - เปนจริง statement ที่อยูตอจาก if จะถูกทํางาน
      - เปนเท็จ statement ที่อยูตอจาก else จะถูกทํางาน
            รปแบบท่ี 1 if…else แบบ statement เดียว มีรูปแบบ ดังนี้
               ู
                           if(เงือนไขการเปรียบเทียบ)
                                 ่
                                      statement;
                           else
                                       statement;
            รปแบบท่ี 2 if…else แบบหลาย statement มีรูปแบบ ดังนี้
             ู
                           if(เงือนไขการเปรียบเทียบ)
                                   ่
                             { statement;
                                     statement;
                                     statement;
                               }
                           else
                                { statement;
                                       statement;
 !                                   }


ศิริชัย นามบุรี                                                             การเลือกทํา
!                                               36

           •! ตัวอยางโปรแกรม test_if3.cpp แสดงการใช if…else แบบ statement เดยว
                                                                              ี

/*Program : test_if3.cpp
 Process : copare 2 integer and use if...else one statement
*/
#include <iostream.h>
#include <conio.h>
void main()
{ int x,y;
//start statement
  clrscr();
  cout<< "Please enter integer number : n";
  cout<< "Finst number : ";
  cin>>x;
  cout<< "nSecond number : ";
  cin>>y;
  if (x>y) //condition of if
     cout<<"nFirst number is greater than second numberan";
  else
     cout<<"nSecond number is greater than or equal first numberan";
  getch();
}

           •! ตัวอยางโปรแกรม test_if4.cpp แสดงการใช if…else แบบหลาย statement
/*Program : test_if3.cpp
 Process : copare 2 integer and use if...else
*/
#include <iostream.h>
#include <conio.h>
void main()
{ int x,y;
  //start statement
 clrscr();
 cout<< "Please enter integer number : n";
 cout<< "Finst number : ";
 cin>>x;
 cout<< "nSecond number : ";
 cin>>y;
//condition of if
  if (x>y)

ศิริชัย นามบุรี                                                                    การเลือกทํา
!                                                37
      { clrscr();
        cout<<"You enter 2 number :";
        cout<<"nFirst number is : "<<x;
        cout<<"nSecond number is : "<<y;
        cout<<"nFirst number is greater than second numberan";
        getch();
      }
    else
      { clrscr();
        cout<<"You enter 2 number :";
        cout<<"nFirst number is : "<<x;
        cout<<"nSecond number is : "<<y;
        cout<<"nSecond number is greater than or equal first numberan";
      }
    getch();
}
♦! ารเลอกทําแบบ if ซอนกน (nested if)
 ก ื                 ั
       เราสามารถใช if…else if ซอนกัน เพอตรวจสอบเงอนไขในโปรแกรมใหมทางเลอกหลาย ๆ
                                         ่ื        ่ื              ี   ื
ทางได มรปแบบดงน้ี
        ีู     ั
          if (เงือนไขที่ 1)
                 ่
!                      { statement;
                          statement;
                        }
                     else if (เงือนไขที่ 2)
                                 ่
                               { statement;
                                 statement;
                     !        }
                     ! else if (เงือนไขที่ 3)
                                         ่
                     !                 { statement;
                               !         statement;
                               !       }
                               !    else
        !                               { statement;
           •! ตัวอยางโปรแกรม grade1.cpp แสดงการคํานวณเกรด โดยการกรอกคะแนนรวมทาง
        !                                  statement;
                   คียบอรด โดยมีเงื่อนไขการตัดเกรดจากคะแนนรวม ดังนี้
        !                            }
ศิริชัย นามบุรี                                                               การเลือกทํา
!                                              38

                 คะแนนรวม 0-49                 ไดเกรด          F
                 คะแนนรวม 50-59                ไดเกรด          D
                 คะแนนรวม 60-69                ไดเกรด          C
                 คะแนนรวม 70-79                ไดเกรด          B
                 คะแนนรวม 80-100               ไดเกรด          A
                 คะแนนอน ๆ (ไมถูกตอง)
                           ่ื                  ไดเกรด          *
           โดยแสดงการใช if…else…if แบบ statement เดยว ดังนี้
                                                    ี
/*Program: grade1.cpp
 Process: calculate grade from total score*/
#include <iostream.h>
#include <conio.h>
void main()
{ int score;
  char grade;
  //begin statement
  clrscr();
  cout<< "Program calculate grade";
  cout<< "nnPlease enter your score : ";
  cin>>score; //input score
  if (score<0) //calculate grade use if...else if..
     grade='*';
  else if (score<=49)
         grade='F';
      else if(score<=59)
           grade='D';
            else if(score<=69)
                  grade='C';
                 else if(score<=79)
                        grade='B';
                       else if(score<=100)
                             grade='A';
                            else
                                grade='*';
   cout<< "You get grade : a"<<grade<<'n'; //show get grade
   if (grade=='*')
      cout<< "Your score = "<<score<< " is error range !!!n"; getch();
}




ศิริชัย นามบุรี                                                           การเลือกทํา
!                                             39

           •! ตัวอยางโปรแกรม grade2.cpp แสดงการคํานวณการตัดเกรดโดยใช Logical Operator ได
              แก || (OR) && (AND) มาใชในการกําหนดเงอนไข ซงมผลลพธการทํางานเหมือน
                                                     ่ื     ่ึ ี ั 
              โปรแกรม grade1.cpp

/*Program: grade2.cpp
 Process: calculate grade from total score
*/
#include <iostream.h>
#include <conio.h>
void main()
{ int score;
  char grade;
  //begin statement
  clrscr();
  cout<< "Program calculate grade";
  cout<< "nnPlease enter your score : ";
  cin>>score; //input score
  //calculate grade use if...else if..
  if (score<0 || score>100) //check enter error score
      grade='*';
  else if (score>=0 && score<=49)
          grade='F';
       else if(score>=50 && score<=59)
              grade='D';
           else if(score>=60 && score<=69)
                 grade='C';
                else if(score>=70 && score<=79)
                        grade='B';
                      else
                          grade='A'; // end of if command
   cout<< "You get grade : a"<<grade<<'n'; //show get grade
   if (grade=='*')
      cout<< "Your score = "<<score<< " is error range !!!n";
   getch();
}




ศิริชัย นามบุรี                                                                      การเลือกทํา
!                                                  40

♦! ารใช Conditional Operator ?:
 ก
       ใน C++ มีการกําหนดเงือนไขทีทํางานไดเหมือน if…else เรยกวา conditional operator ใช
                            ่     ่                         ี 
สัญลักษณ ?: แทน มีรูปแบบ คือ
           ตัวแปรเก็บผลลัพธ = (เงื่อนไขเปรียบเทียบ) ? คาที่ 1 : คาที่ 2;
                หลักการทางานเมอเงอนไขใหคาเปน จริง ตัวแปรผลลัพธจะมีคาเปน คาที่ 1 แตถา
                          ํ    ่ื ่ื     
เงอนไขใหคาเปน เทจ ตัวแปรผลลัพธจะมีคาเปน คาที่ 2
  ่ื        ็
       พิจารณาประโยค if…else ตอไปน้ี
                                  
       if (a>b) c=a; else c=b;
       หรือ
       if (a>b)
          c= a;
       else
           c=b;
       จากประโยค if…else ดังกลาว สามารถใช conditional operator แทนไดดงน้ี
                                                                      ั
       c=(a>b)? a : b;

♦! ารเลอกทําแบบ switch … case
 ก ื
        ในกรณีที่การเลือกทํามีหลายเงื่อนไข แตละเงื่อนไขขึ้นอยูกับ ตัวแปร (variable) ตัวเดียวกัน ที่
เปนประเภท int หรือ char สามารถใชการเลือกทาแบบ switch…case แทนการเลอก ทําแบบ if
                                             ํ                                 ื
ซอนกัน (nested if ) ได โดยมีรปแบบดังนี้
                               ู
              switch(ตัวแปรชนิด int หรอ char)
                                         ื
                     { case คาคงที่ชนิด int หรอ char:
                                               ื
                           statement;
                               statement;
                               break;
                        case คาคงที่ชนิด int หรอ char:
                                                 ื
                               statement;
                               statement;
                               break;
                        default:
                               statement;
                               break;
            !        }
ศิริชัย นามบุรี                                                                              การเลือกทํา
!                                               41

         หลักการทางานของ switch ....case คือ การตรวจสอบเงือนไขของ switch จะนําคาของตวแปร
                        ํ                                        ่                      ั
ที่กําหนดไวในวงเล็บหลัง switch (เปนตัวแปรชนิด int หรือ char เทานน) ไปตรวจสอบกับคาคงที่ที่
                                                                        ้ั
กําหนดไวในแตละกรณหรอ case ตาง ๆ (ซึ่งตองเปนคาคงที่ชนิด int หรือ char เหมอนกบตวแปรหลง
                          ี ื                                                ื ั ั        ั
switch) ถาตรงกบกรณใดเปนอนดบแรก จะทํา statement หลัง case นัน แลวก็ออกจากการทางานดวยคีย
                 ั       ี  ั ั                                   ้                  ํ
เวิรด break ไปทางาน statement อน ๆ ที่อยูหลัง } ตอไป แตถาคาตัวแปรที่กาหนดไวไมตรงกบกรณี
                     ํ               ่ื                                      ํ      ั
ใด ๆ เลย จะทํา statement หลัง default (ถากาหนด default: ไว)
                                              ํ                
         •! ตัวอยางโปรแกรม switch.cpp แสดงการใช switch โดยกําหนดตัวแปรหลง switch เปน
                                                                                 ั         
              ประเภท char ดงนนคาคงททกาหนดไวหลัง case จะตองเปน char โดยกําหนดไวในเครื่อง
                                 ั ้ั  ่ี ่ี ํ
              ' ' ดวย เพื่อใหสามารถนําคาคงที่ประเภทเดียวกันเปรียบเทียบกันได
/*Program : switch.cpp
  Process : test statement switch...case
*/
#include <iostream.h>
#include <conio.h>
void main()
{ int first,second;
  char choice;
  //begin statement
  clrscr();
  cout<<"Program Calcurate Arean";
  cout<<"1. Circlen";
  cout<<"2. Squaren";
  cout<<"3. Trianglen";
  cout<<"Please select your choice <1-3>: ";
  cin>>choice;
  //begin switch statement
  switch(choice)
 { case '1':
      cout<<"nYou select choice "<<choice<< " calculate Circle Arean";
      cout<<"Press any key to end programn";
      break;
    case '2':
      cout<<"nYou select choice "<<choice<< " calculate Square Arean";
      cout<<"Press any key to end programn";
      break;
   case '3':
      cout<<"nYou select choice "<<choice<< " calculate Triangle Arean";
      cout<<"Press any key to end programn";
      break;
   default:
ศิริชัย นามบุรี                                                                       การเลือกทํา
!                                                   42
      cout<<"nYou select Another choice aan";
      cout<<"Press any key to end programn";
  }
 getch();
}

♦! บบฝกหัดทายบท
 แ

1. ในการคํานวณภาษเงินไดบุคคลธรรมดา (ภงด.91) มีการคํานวณภาษเี งนไดในอตรากาวหนาจากเงน
                                                               ิ  ั                ิ
ได สุทธิ ดังอัตราตามตารางตอไปนี้
       เงินไดสุทธิตั้งแต  จํานวนเงนไดสงสดของ อัตราภาษี ภาษีเงินไดใน ภาษีสะสมสูงสุด
                                     ิ  ู ุ
                                      ขั้น           รอยละ
                                                                แตละขั้น      ของขน   ้ั
 1 ถึง 100,000                      100,000              5          5,000         5,000
 100,001-500,000                    400,000             10         40,000        45,000
 500,001-1,000,000                  500,000             20        100,000       145,000
 1,000,001-4,000,000               3,000,000            30        900,000      1,045,000
 4,000,001 ขึ้นไป                                       37

         ตัวอยางเชน ถามีเงินไดสุทธิ 1,450,000 บาท การคานวณภาษในอตรากาวหนา เปนดงน้ี
                                                             ํ                ี ั         ั
         100,000 บาทแรก             คดรอยละ 5
                                      ิ                    ภาษีที่คานวณได 5,000 บาท
                                                                          ํ
เหลือเงินไดสุทธิอีก 1,450,000-100,000 = 1,350,000 บาท นําไปคํานวณในอัตราตอไปเต็มขัน       ้
         400,000 บาท                คิดรอยละ 10            ภาษีที่คานวณได 40,000 บาท
                                                                    ํ
เหลือเงินไดสุทธิอีก        1,350,000 - 400,000 = 950,000 บาท นําไปคํานวณในอัตราตอไปเต็มขัน       ้
         500,000 บาท                คิดรอยละ 20            ภาษีที่คานวณได 100,000 บาท
                                                                      ํ
เหลือเงินไดสุทธิอีก        950,000 - 500,000 = 450,000 บาท นําไปคํานวณในอตราตอไปไมเ ตมขน
                                                                                   ั           ็ ้ั
         450,000 บาท                คดรอยละ 30
                                       ิ                   ภาษีที่คานวณได 135,000 บาท
                                                                        ํ
         รวมภาษีเงินไดที่ตองชําระทงสน 5,000+40,000+100,000+135,000 = 280,000 บาท
                                        ้ั ้ิ
         จากวิธีการคํานวณภาษเี งนไดบคคลธรรมดาในอตรากาวหนาน้ี ใหเ ขยนโปรแกรมเพอคานวณ
                                   ิ  ุ                 ั                     ี            ่ื ํ
หาภาษี เมือกรอกจํานวนเงินไดสุทธิทางแปนพิมพ โดยแสดงขันตอนในการคํานวณอตราแบบกาวหนา
          ่                                                    ้                       ั            
แตละขนใหเ หนโดยละเอยดทางจอภาพ
   ้ั         ็          ี




ศิริชัย นามบุรี                                                                                การเลือกทํา
!                                        43

2. รานโชคดีการคา ตองการใหเ ขยนโปรแกรมเพอคานวณสวนลดของราคาสินคาและราคาสินคาสุทธิ
                                 ี          ่ื ํ
   ใหแกลูกคาจากยอดซื้อที่รวม VAT 10% แลว ตามเงอนไขดงน้ี
                                                     ่ื   ั
        ยอดซื้อ        1- 1000         บาท ใหสวนลด 3%
        ยอดซื้อ        1001- 2000 บาท ใหสวนลด 5%
        ยอดซื้อ        2001- 5000 บาท ใหสวนลด 7%
        ยอดซื้อ        5001- 10000 บาท ใหสวนลด 9%
        ยอดซื้อ        10001 บาทขึ้นไป         ใหสวนลด 10%
   ใหนกศกษาเขยนโปรแกรมเพอรบคาจํานวนยอดซือสินคากอนรวม VAT , คํานวณราคาซอรวม
       ั ึ      ี              ่ื ั             ้                           ้ื
   VAT, คํานวณสวนลดที่ลูกคาจะไดรับ, คํานวณราคาสินคาสุทธิที่ลูกคาตองจาย




ศิริชัย นามบุรี                                                               การเลือกทํา

More Related Content

What's hot

การเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา Cการเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา CWarawut
 
บทที่3 การควบคุมโปรแกรม
บทที่3 การควบคุมโปรแกรมบทที่3 การควบคุมโปรแกรม
บทที่3 การควบคุมโปรแกรมpennapa34
 
พื้นฐานโปรแกรมภาษาจาวา
พื้นฐานโปรแกรมภาษาจาวาพื้นฐานโปรแกรมภาษาจาวา
พื้นฐานโปรแกรมภาษาจาวาThanachart Numnonda
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานการเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานNookky Anapat
 
เครื่องหมายและการดำเนินการในภาษาซี
เครื่องหมายและการดำเนินการในภาษาซีเครื่องหมายและการดำเนินการในภาษาซี
เครื่องหมายและการดำเนินการในภาษาซีเทวัญ ภูพานทอง
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมThanachart Numnonda
 
โครงสร้างภาษาซี
โครงสร้างภาษาซีโครงสร้างภาษาซี
โครงสร้างภาษาซีPatipat04
 
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)Visaitus Palasak
 
บทที่ 3 พื้นฐานภาษา Java
บทที่ 3 พื้นฐานภาษา Javaบทที่ 3 พื้นฐานภาษา Java
บทที่ 3 พื้นฐานภาษา JavaItslvle Parin
 
Java Programming: หลักการเชิงอ็อบเจกต์
Java Programming: หลักการเชิงอ็อบเจกต์Java Programming: หลักการเชิงอ็อบเจกต์
Java Programming: หลักการเชิงอ็อบเจกต์Thanachart Numnonda
 

What's hot (20)

การเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา Cการเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา C
 
บทที่3 การควบคุมโปรแกรม
บทที่3 การควบคุมโปรแกรมบทที่3 การควบคุมโปรแกรม
บทที่3 การควบคุมโปรแกรม
 
พื้นฐานโปรแกรมภาษาจาวา
พื้นฐานโปรแกรมภาษาจาวาพื้นฐานโปรแกรมภาษาจาวา
พื้นฐานโปรแกรมภาษาจาวา
 
02 controlflow php
02 controlflow php02 controlflow php
02 controlflow php
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานการเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
 
เครื่องหมายและการดำเนินการในภาษาซี
เครื่องหมายและการดำเนินการในภาษาซีเครื่องหมายและการดำเนินการในภาษาซี
เครื่องหมายและการดำเนินการในภาษาซี
 
3.3. ชนิดของข้อมูล
3.3. ชนิดของข้อมูล3.3. ชนิดของข้อมูล
3.3. ชนิดของข้อมูล
 
Tec4
Tec4Tec4
Tec4
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุม
 
compromint
compromintcompromint
compromint
 
Know3 2
Know3 2Know3 2
Know3 2
 
02 basic
02 basic02 basic
02 basic
 
12
1212
12
 
Know009
Know009Know009
Know009
 
โครงสร้างภาษาซี
โครงสร้างภาษาซีโครงสร้างภาษาซี
โครงสร้างภาษาซี
 
งานทำ Blog บทที่ 9
งานทำ Blog บทที่ 9งานทำ Blog บทที่ 9
งานทำ Blog บทที่ 9
 
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)
 
Unit8
Unit8Unit8
Unit8
 
บทที่ 3 พื้นฐานภาษา Java
บทที่ 3 พื้นฐานภาษา Javaบทที่ 3 พื้นฐานภาษา Java
บทที่ 3 พื้นฐานภาษา Java
 
Java Programming: หลักการเชิงอ็อบเจกต์
Java Programming: หลักการเชิงอ็อบเจกต์Java Programming: หลักการเชิงอ็อบเจกต์
Java Programming: หลักการเชิงอ็อบเจกต์
 

Viewers also liked

หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูงหลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูงMaxky Thonchan
 
บทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e bookบทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e bookโทโม๊ะจัง นานะ
 
บทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e bookบทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e bookโทโม๊ะจัง นานะ
 
Gunbower forest environmental watering resource booklet.
Gunbower forest environmental watering resource booklet.Gunbower forest environmental watering resource booklet.
Gunbower forest environmental watering resource booklet.Peter Phillips M.Ed.
 
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูงหลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูงMaxky Thonchan
 
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูงหลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูงMaxky Thonchan
 
VCE Environmental Science Unit 3: Biodiversity and conservation management.
VCE Environmental Science Unit 3: Biodiversity and conservation management.VCE Environmental Science Unit 3: Biodiversity and conservation management.
VCE Environmental Science Unit 3: Biodiversity and conservation management.Peter Phillips M.Ed.
 
รายวิชา ปวส
รายวิชา ปวสรายวิชา ปวส
รายวิชา ปวสMaxky Thonchan
 
Gunbower forest environmental watering fieldwork booklet.
Gunbower forest environmental watering fieldwork booklet.Gunbower forest environmental watering fieldwork booklet.
Gunbower forest environmental watering fieldwork booklet.Peter Phillips M.Ed.
 
Characteristics of salt as a pollutant
Characteristics of salt as a pollutantCharacteristics of salt as a pollutant
Characteristics of salt as a pollutantPeter Phillips M.Ed.
 

Viewers also liked (19)

บท1
บท1บท1
บท1
 
Murray River Virtual Field Trip
Murray River Virtual Field TripMurray River Virtual Field Trip
Murray River Virtual Field Trip
 
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูงหลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
 
บทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e bookบทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e book
 
Lesson2
Lesson2Lesson2
Lesson2
 
บทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e bookบทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e book
 
Gunbower forest environmental watering resource booklet.
Gunbower forest environmental watering resource booklet.Gunbower forest environmental watering resource booklet.
Gunbower forest environmental watering resource booklet.
 
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูงหลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
 
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูงหลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
หลักสูตรประกาศนียบัตรวิชาชีพชั้นสูง
 
Lesson6
Lesson6Lesson6
Lesson6
 
บท5
บท5บท5
บท5
 
บท6
บท6บท6
บท6
 
บท4
บท4บท4
บท4
 
Lesson5
Lesson5Lesson5
Lesson5
 
VCE Environmental Science Unit 3: Biodiversity and conservation management.
VCE Environmental Science Unit 3: Biodiversity and conservation management.VCE Environmental Science Unit 3: Biodiversity and conservation management.
VCE Environmental Science Unit 3: Biodiversity and conservation management.
 
รายวิชา ปวส
รายวิชา ปวสรายวิชา ปวส
รายวิชา ปวส
 
Gunbower forest environmental watering fieldwork booklet.
Gunbower forest environmental watering fieldwork booklet.Gunbower forest environmental watering fieldwork booklet.
Gunbower forest environmental watering fieldwork booklet.
 
Characteristics of salt as a pollutant
Characteristics of salt as a pollutantCharacteristics of salt as a pollutant
Characteristics of salt as a pollutant
 
เฉลยแบบฝึกหัดบทที่ 1
เฉลยแบบฝึกหัดบทที่ 1เฉลยแบบฝึกหัดบทที่ 1
เฉลยแบบฝึกหัดบทที่ 1
 

Similar to Lesson3

การเขียนคำสั่งควบคุมแบบมีทางเลือก กลุ่ม 3
การเขียนคำสั่งควบคุมแบบมีทางเลือก กลุ่ม 3การเขียนคำสั่งควบคุมแบบมีทางเลือก กลุ่ม 3
การเขียนคำสั่งควบคุมแบบมีทางเลือก กลุ่ม 3KEk YourJust'one
 
การเขียนคำสั่งควบคุมแบบมีทางเลือก
การเขียนคำสั่งควบคุมแบบมีทางเลือกการเขียนคำสั่งควบคุมแบบมีทางเลือก
การเขียนคำสั่งควบคุมแบบมีทางเลือกThanon Paktanadechanon
 
บทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคบทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคTheeravaj Tum
 
Java Programming [3/12]: Control Structures
Java Programming [3/12]:  Control StructuresJava Programming [3/12]:  Control Structures
Java Programming [3/12]: Control StructuresIMC Institute
 
04 conditional
04 conditional04 conditional
04 conditionala-num Sara
 
คำสั่งและเงื่อนไข [Web-Programming]
คำสั่งและเงื่อนไข [Web-Programming]คำสั่งและเงื่อนไข [Web-Programming]
คำสั่งและเงื่อนไข [Web-Programming]Khon Kaen University
 
ม.3 รหัสจำลอง Pseudocode
ม.3 รหัสจำลอง Pseudocodeม.3 รหัสจำลอง Pseudocode
ม.3 รหัสจำลอง PseudocodeBansit Deelom
 
การควบคุมทิศทางในภาษา C
การควบคุมทิศทางในภาษา Cการควบคุมทิศทางในภาษา C
การควบคุมทิศทางในภาษา CWarawut
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1Pornpimon Aom
 

Similar to Lesson3 (20)

chapter 3 คำสั่งควบคุม
chapter 3 คำสั่งควบคุมchapter 3 คำสั่งควบคุม
chapter 3 คำสั่งควบคุม
 
ฟังก์ชั่น If หลายทางเลือก
ฟังก์ชั่น If หลายทางเลือกฟังก์ชั่น If หลายทางเลือก
ฟังก์ชั่น If หลายทางเลือก
 
การเขียนคำสั่งควบคุมแบบมีทางเลือก กลุ่ม 3
การเขียนคำสั่งควบคุมแบบมีทางเลือก กลุ่ม 3การเขียนคำสั่งควบคุมแบบมีทางเลือก กลุ่ม 3
การเขียนคำสั่งควบคุมแบบมีทางเลือก กลุ่ม 3
 
การเขียนคำสั่งควบคุมแบบมีทางเลือก
การเขียนคำสั่งควบคุมแบบมีทางเลือกการเขียนคำสั่งควบคุมแบบมีทางเลือก
การเขียนคำสั่งควบคุมแบบมีทางเลือก
 
บทที่ 3 คำสั่งควบคุม ส่วนที่ 1
บทที่ 3 คำสั่งควบคุม ส่วนที่ 1บทที่ 3 คำสั่งควบคุม ส่วนที่ 1
บทที่ 3 คำสั่งควบคุม ส่วนที่ 1
 
05 loops
05 loops05 loops
05 loops
 
บทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคบทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบค
 
Java Programming [3/12]: Control Structures
Java Programming [3/12]:  Control StructuresJava Programming [3/12]:  Control Structures
Java Programming [3/12]: Control Structures
 
04 conditional
04 conditional04 conditional
04 conditional
 
ฟังก์ชั่น Switch
ฟังก์ชั่น Switchฟังก์ชั่น Switch
ฟังก์ชั่น Switch
 
Know4 3
Know4 3Know4 3
Know4 3
 
คำสั่งและเงื่อนไข [Web-Programming]
คำสั่งและเงื่อนไข [Web-Programming]คำสั่งและเงื่อนไข [Web-Programming]
คำสั่งและเงื่อนไข [Web-Programming]
 
Control structure
Control structureControl structure
Control structure
 
06 for loops
06 for loops06 for loops
06 for loops
 
C slide
C slideC slide
C slide
 
ม.3 รหัสจำลอง Pseudocode
ม.3 รหัสจำลอง Pseudocodeม.3 รหัสจำลอง Pseudocode
ม.3 รหัสจำลอง Pseudocode
 
05 Loops
05  Loops05  Loops
05 Loops
 
การควบคุมทิศทางในภาษา C
การควบคุมทิศทางในภาษา Cการควบคุมทิศทางในภาษา C
การควบคุมทิศทางในภาษา C
 
ฟังก์ชั่น If สองทางเลือก
ฟังก์ชั่น If สองทางเลือกฟังก์ชั่น If สองทางเลือก
ฟังก์ชั่น If สองทางเลือก
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1
 

More from โทโม๊ะจัง นานะ

บทที่ 2 สร้าง e book ง่ายและเร็ว
บทที่ 2 สร้าง e book ง่ายและเร็วบทที่ 2 สร้าง e book ง่ายและเร็ว
บทที่ 2 สร้าง e book ง่ายและเร็วโทโม๊ะจัง นานะ
 
บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์
บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์
บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์โทโม๊ะจัง นานะ
 

More from โทโม๊ะจัง นานะ (19)

บทที่ 3 การควบคุม e book
บทที่ 3 การควบคุม e bookบทที่ 3 การควบคุม e book
บทที่ 3 การควบคุม e book
 
บทที่ 2 สร้าง e book ง่ายและเร็ว
บทที่ 2 สร้าง e book ง่ายและเร็วบทที่ 2 สร้าง e book ง่ายและเร็ว
บทที่ 2 สร้าง e book ง่ายและเร็ว
 
บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์
บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์
บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์
 
Chapter1
Chapter1Chapter1
Chapter1
 
Lesson1
Lesson1Lesson1
Lesson1
 
Lesson1
Lesson1Lesson1
Lesson1
 
บทที่1
บทที่1บทที่1
บทที่1
 
บท2
บท2บท2
บท2
 
บท3
บท3บท3
บท3
 
บท4
บท4บท4
บท4
 
บท5
บท5บท5
บท5
 
บท6
บท6บท6
บท6
 
บท6
บท6บท6
บท6
 
บท1
บท1บท1
บท1
 
บท2
บท2บท2
บท2
 
บท5
บท5บท5
บท5
 
B2
B2B2
B2
 
บท3
บท3บท3
บท3
 
บท6
บท6บท6
บท6
 

Lesson3

  • 1. ! บทที่ 3 การเลือกทํา ♦! วามหมายของการเลอกทํา ค ื การเลือกทํา หมายถึง การกาหนดใหโปรแกรมทางานอยางหนึงเมือเงือนไข (Condition) เปน ํ  ํ ่ ่ ่  จริงและทา (หรือไมตองทํางานใด ๆ ) เมือเงือนไข เปนเท็จ งานโปรแกรมคอมพิวเตอรมการกําหนดให ํ  ่ ่ ี เลือกทาอยูมากมายการถามผูใช เชน Save (Y/N) _ Continue(Y/N) _ เปนตน ํ  ♦! ารเลอกทําแบบ if ก ื การเลือกทําแบบ if เริ่มดวยการทดสอบเงื่อนไขที่ไดกาหนดไวกอน อาจมีมากกวา 1 เงอนไขก็ ํ  ่ื ได ผลการตรวจสอบเงือนไข ถาผล ่ - เปนจริง statement ตอจาก if จะถูกทํางาน - เปนเท็จ statement ตอจาก if จะไมถูกทํางานหรือไมตองทางานใด ๆ ํ รปแบบ 1 แบบ statement เดียว มีรูปแบบ ดังนี้ ู if (เงือนไขการเปรียบเทียบ) ่ statement; รปแบบ 2 แบบหลาย statement มีรูปแบบ ดังนี้ ู if (เงือนไขการเปรียบเทียบ) ่ { statement; statement; statement; … ! } ศิริชัย นามบุรี การเลือกทํา
  • 2. ! 34 •! ตัวอยางโปรแกรม test_if1.cpp แสดงการรับขอมูลจํานวนเต็ม 2 จานวน และเปรียบเทียบ ํ กันโดยใช if แบบประโยค(statement) เดยว ี /*Program : test_if1.cpp Process : copare 2 integer */ #include <iostream.h> #include <conio.h> void main() { int x,y; int result = 100; //start statement clrscr(); cout<< "Please enter integer number : n"; cout<< "Finst number : "; cin>>x; cout<< "nSecond number : "; cin>>y; //condition of if if (x>y) //condition cout<<"nFirst number is greater than second numberan"; if (x+y>result) //condition cout<<"nFirst number + Seconde number is greater than an"<<result; getch(); } •! โปรแกรม test_if2.cpp แสดงการเปรยบเทยบจํานวนตัวเลข 2 จานวน และใช if แบบ ี ี ํ หลาย ประโยค(Compound statement) /*Program : test_if2.cpp Process : copare 2 integer*/ #include <iostream.h> #include <conio.h> void main() { int x,y; int result = 100; //start statement clrscr(); cout<< "Please enter integer number : n"; cout<< "Finst number : "; cin>>x; cout<< "nSecond number : "; cin>>y; //condition of if ศิริชัย นามบุรี การเลือกทํา
  • 3. ! 35 if (x>y) { clrscr(); cout<<"nFirst number is : "<<x; cout<<"nSecond number is : "<<y; cout<<"nFirst number is greater than second numberan"; } if (x+y<=result) { clrscr(); cout<<"nFirst number is : "<<x; cout<<"nSecond number is : "<<y; cout<<"nFirst number + Seconde number <= an"<<result; } getch(); } ♦! ารเลอกทําแบบ if…else ก ื การเลือกทําแบบ if …else มีหลักการทางาน คอเรมดวยการทดสอบเงอนไขทกําหนดไว ถาผล ํ ื ่ิ  ่ื ่ี การตรวจสอบเงอนไข เปนดงน้ี ่ื  ั - เปนจริง statement ที่อยูตอจาก if จะถูกทํางาน - เปนเท็จ statement ที่อยูตอจาก else จะถูกทํางาน รปแบบท่ี 1 if…else แบบ statement เดียว มีรูปแบบ ดังนี้ ู if(เงือนไขการเปรียบเทียบ) ่ statement; else statement; รปแบบท่ี 2 if…else แบบหลาย statement มีรูปแบบ ดังนี้ ู if(เงือนไขการเปรียบเทียบ) ่ { statement; statement; statement; } else { statement; statement; ! } ศิริชัย นามบุรี การเลือกทํา
  • 4. ! 36 •! ตัวอยางโปรแกรม test_if3.cpp แสดงการใช if…else แบบ statement เดยว ี /*Program : test_if3.cpp Process : copare 2 integer and use if...else one statement */ #include <iostream.h> #include <conio.h> void main() { int x,y; //start statement clrscr(); cout<< "Please enter integer number : n"; cout<< "Finst number : "; cin>>x; cout<< "nSecond number : "; cin>>y; if (x>y) //condition of if cout<<"nFirst number is greater than second numberan"; else cout<<"nSecond number is greater than or equal first numberan"; getch(); } •! ตัวอยางโปรแกรม test_if4.cpp แสดงการใช if…else แบบหลาย statement /*Program : test_if3.cpp Process : copare 2 integer and use if...else */ #include <iostream.h> #include <conio.h> void main() { int x,y; //start statement clrscr(); cout<< "Please enter integer number : n"; cout<< "Finst number : "; cin>>x; cout<< "nSecond number : "; cin>>y; //condition of if if (x>y) ศิริชัย นามบุรี การเลือกทํา
  • 5. ! 37 { clrscr(); cout<<"You enter 2 number :"; cout<<"nFirst number is : "<<x; cout<<"nSecond number is : "<<y; cout<<"nFirst number is greater than second numberan"; getch(); } else { clrscr(); cout<<"You enter 2 number :"; cout<<"nFirst number is : "<<x; cout<<"nSecond number is : "<<y; cout<<"nSecond number is greater than or equal first numberan"; } getch(); } ♦! ารเลอกทําแบบ if ซอนกน (nested if) ก ื  ั เราสามารถใช if…else if ซอนกัน เพอตรวจสอบเงอนไขในโปรแกรมใหมทางเลอกหลาย ๆ ่ื ่ื  ี ื ทางได มรปแบบดงน้ี ีู ั if (เงือนไขที่ 1) ่ ! { statement; statement; } else if (เงือนไขที่ 2) ่ { statement; statement; ! } ! else if (เงือนไขที่ 3) ่ ! { statement; ! statement; ! } ! else ! { statement; •! ตัวอยางโปรแกรม grade1.cpp แสดงการคํานวณเกรด โดยการกรอกคะแนนรวมทาง ! statement; คียบอรด โดยมีเงื่อนไขการตัดเกรดจากคะแนนรวม ดังนี้ ! } ศิริชัย นามบุรี การเลือกทํา
  • 6. ! 38 คะแนนรวม 0-49 ไดเกรด F คะแนนรวม 50-59 ไดเกรด D คะแนนรวม 60-69 ไดเกรด C คะแนนรวม 70-79 ไดเกรด B คะแนนรวม 80-100 ไดเกรด A คะแนนอน ๆ (ไมถูกตอง) ่ื ไดเกรด * โดยแสดงการใช if…else…if แบบ statement เดยว ดังนี้ ี /*Program: grade1.cpp Process: calculate grade from total score*/ #include <iostream.h> #include <conio.h> void main() { int score; char grade; //begin statement clrscr(); cout<< "Program calculate grade"; cout<< "nnPlease enter your score : "; cin>>score; //input score if (score<0) //calculate grade use if...else if.. grade='*'; else if (score<=49) grade='F'; else if(score<=59) grade='D'; else if(score<=69) grade='C'; else if(score<=79) grade='B'; else if(score<=100) grade='A'; else grade='*'; cout<< "You get grade : a"<<grade<<'n'; //show get grade if (grade=='*') cout<< "Your score = "<<score<< " is error range !!!n"; getch(); } ศิริชัย นามบุรี การเลือกทํา
  • 7. ! 39 •! ตัวอยางโปรแกรม grade2.cpp แสดงการคํานวณการตัดเกรดโดยใช Logical Operator ได แก || (OR) && (AND) มาใชในการกําหนดเงอนไข ซงมผลลพธการทํางานเหมือน ่ื ่ึ ี ั  โปรแกรม grade1.cpp /*Program: grade2.cpp Process: calculate grade from total score */ #include <iostream.h> #include <conio.h> void main() { int score; char grade; //begin statement clrscr(); cout<< "Program calculate grade"; cout<< "nnPlease enter your score : "; cin>>score; //input score //calculate grade use if...else if.. if (score<0 || score>100) //check enter error score grade='*'; else if (score>=0 && score<=49) grade='F'; else if(score>=50 && score<=59) grade='D'; else if(score>=60 && score<=69) grade='C'; else if(score>=70 && score<=79) grade='B'; else grade='A'; // end of if command cout<< "You get grade : a"<<grade<<'n'; //show get grade if (grade=='*') cout<< "Your score = "<<score<< " is error range !!!n"; getch(); } ศิริชัย นามบุรี การเลือกทํา
  • 8. ! 40 ♦! ารใช Conditional Operator ?: ก ใน C++ มีการกําหนดเงือนไขทีทํางานไดเหมือน if…else เรยกวา conditional operator ใช ่ ่ ี  สัญลักษณ ?: แทน มีรูปแบบ คือ ตัวแปรเก็บผลลัพธ = (เงื่อนไขเปรียบเทียบ) ? คาที่ 1 : คาที่ 2; หลักการทางานเมอเงอนไขใหคาเปน จริง ตัวแปรผลลัพธจะมีคาเปน คาที่ 1 แตถา ํ ่ื ่ื   เงอนไขใหคาเปน เทจ ตัวแปรผลลัพธจะมีคาเปน คาที่ 2 ่ื   ็ พิจารณาประโยค if…else ตอไปน้ี  if (a>b) c=a; else c=b; หรือ if (a>b) c= a; else c=b; จากประโยค if…else ดังกลาว สามารถใช conditional operator แทนไดดงน้ี ั c=(a>b)? a : b; ♦! ารเลอกทําแบบ switch … case ก ื ในกรณีที่การเลือกทํามีหลายเงื่อนไข แตละเงื่อนไขขึ้นอยูกับ ตัวแปร (variable) ตัวเดียวกัน ที่ เปนประเภท int หรือ char สามารถใชการเลือกทาแบบ switch…case แทนการเลอก ทําแบบ if  ํ ื ซอนกัน (nested if ) ได โดยมีรปแบบดังนี้ ู switch(ตัวแปรชนิด int หรอ char) ื { case คาคงที่ชนิด int หรอ char: ื statement; statement; break; case คาคงที่ชนิด int หรอ char: ื statement; statement; break; default: statement; break; ! } ศิริชัย นามบุรี การเลือกทํา
  • 9. ! 41 หลักการทางานของ switch ....case คือ การตรวจสอบเงือนไขของ switch จะนําคาของตวแปร ํ ่  ั ที่กําหนดไวในวงเล็บหลัง switch (เปนตัวแปรชนิด int หรือ char เทานน) ไปตรวจสอบกับคาคงที่ที่  ้ั กําหนดไวในแตละกรณหรอ case ตาง ๆ (ซึ่งตองเปนคาคงที่ชนิด int หรือ char เหมอนกบตวแปรหลง   ี ื  ื ั ั ั switch) ถาตรงกบกรณใดเปนอนดบแรก จะทํา statement หลัง case นัน แลวก็ออกจากการทางานดวยคีย  ั ี  ั ั ้ ํ เวิรด break ไปทางาน statement อน ๆ ที่อยูหลัง } ตอไป แตถาคาตัวแปรที่กาหนดไวไมตรงกบกรณี  ํ ่ื ํ   ั ใด ๆ เลย จะทํา statement หลัง default (ถากาหนด default: ไว) ํ  •! ตัวอยางโปรแกรม switch.cpp แสดงการใช switch โดยกําหนดตัวแปรหลง switch เปน ั  ประเภท char ดงนนคาคงททกาหนดไวหลัง case จะตองเปน char โดยกําหนดไวในเครื่อง ั ้ั  ่ี ่ี ํ ' ' ดวย เพื่อใหสามารถนําคาคงที่ประเภทเดียวกันเปรียบเทียบกันได /*Program : switch.cpp Process : test statement switch...case */ #include <iostream.h> #include <conio.h> void main() { int first,second; char choice; //begin statement clrscr(); cout<<"Program Calcurate Arean"; cout<<"1. Circlen"; cout<<"2. Squaren"; cout<<"3. Trianglen"; cout<<"Please select your choice <1-3>: "; cin>>choice; //begin switch statement switch(choice) { case '1': cout<<"nYou select choice "<<choice<< " calculate Circle Arean"; cout<<"Press any key to end programn"; break; case '2': cout<<"nYou select choice "<<choice<< " calculate Square Arean"; cout<<"Press any key to end programn"; break; case '3': cout<<"nYou select choice "<<choice<< " calculate Triangle Arean"; cout<<"Press any key to end programn"; break; default: ศิริชัย นามบุรี การเลือกทํา
  • 10. ! 42 cout<<"nYou select Another choice aan"; cout<<"Press any key to end programn"; } getch(); } ♦! บบฝกหัดทายบท แ 1. ในการคํานวณภาษเงินไดบุคคลธรรมดา (ภงด.91) มีการคํานวณภาษเี งนไดในอตรากาวหนาจากเงน ิ  ั   ิ ได สุทธิ ดังอัตราตามตารางตอไปนี้ เงินไดสุทธิตั้งแต จํานวนเงนไดสงสดของ อัตราภาษี ภาษีเงินไดใน ภาษีสะสมสูงสุด ิ  ู ุ ขั้น รอยละ  แตละขั้น ของขน ้ั 1 ถึง 100,000 100,000 5 5,000 5,000 100,001-500,000 400,000 10 40,000 45,000 500,001-1,000,000 500,000 20 100,000 145,000 1,000,001-4,000,000 3,000,000 30 900,000 1,045,000 4,000,001 ขึ้นไป 37 ตัวอยางเชน ถามีเงินไดสุทธิ 1,450,000 บาท การคานวณภาษในอตรากาวหนา เปนดงน้ี ํ ี ั    ั 100,000 บาทแรก คดรอยละ 5 ิ  ภาษีที่คานวณได 5,000 บาท ํ เหลือเงินไดสุทธิอีก 1,450,000-100,000 = 1,350,000 บาท นําไปคํานวณในอัตราตอไปเต็มขัน ้ 400,000 บาท คิดรอยละ 10 ภาษีที่คานวณได 40,000 บาท ํ เหลือเงินไดสุทธิอีก 1,350,000 - 400,000 = 950,000 บาท นําไปคํานวณในอัตราตอไปเต็มขัน ้ 500,000 บาท คิดรอยละ 20 ภาษีที่คานวณได 100,000 บาท ํ เหลือเงินไดสุทธิอีก 950,000 - 500,000 = 450,000 บาท นําไปคํานวณในอตราตอไปไมเ ตมขน ั  ็ ้ั 450,000 บาท คดรอยละ 30 ิ  ภาษีที่คานวณได 135,000 บาท ํ รวมภาษีเงินไดที่ตองชําระทงสน 5,000+40,000+100,000+135,000 = 280,000 บาท ้ั ้ิ จากวิธีการคํานวณภาษเี งนไดบคคลธรรมดาในอตรากาวหนาน้ี ใหเ ขยนโปรแกรมเพอคานวณ ิ  ุ ั   ี ่ื ํ หาภาษี เมือกรอกจํานวนเงินไดสุทธิทางแปนพิมพ โดยแสดงขันตอนในการคํานวณอตราแบบกาวหนา ่ ้ ั   แตละขนใหเ หนโดยละเอยดทางจอภาพ  ้ั ็ ี ศิริชัย นามบุรี การเลือกทํา
  • 11. ! 43 2. รานโชคดีการคา ตองการใหเ ขยนโปรแกรมเพอคานวณสวนลดของราคาสินคาและราคาสินคาสุทธิ  ี ่ื ํ ใหแกลูกคาจากยอดซื้อที่รวม VAT 10% แลว ตามเงอนไขดงน้ี ่ื ั ยอดซื้อ 1- 1000 บาท ใหสวนลด 3% ยอดซื้อ 1001- 2000 บาท ใหสวนลด 5% ยอดซื้อ 2001- 5000 บาท ใหสวนลด 7% ยอดซื้อ 5001- 10000 บาท ใหสวนลด 9% ยอดซื้อ 10001 บาทขึ้นไป ใหสวนลด 10% ใหนกศกษาเขยนโปรแกรมเพอรบคาจํานวนยอดซือสินคากอนรวม VAT , คํานวณราคาซอรวม  ั ึ ี ่ื ั  ้ ้ื VAT, คํานวณสวนลดที่ลูกคาจะไดรับ, คํานวณราคาสินคาสุทธิที่ลูกคาตองจาย ศิริชัย นามบุรี การเลือกทํา