ใบความรูที่ 3.2
                                     โครงสราง if และ if...else

    โครงสราง if เปนโครงสรางที่ใชควบคุมการทํางานของคําสั่งอื่น ๆ ภายใตเงื่อนไข (condition) ที่
กําหนด การใชงานนั้นมีสองรูปแบบคราว ๆ ไดแก
   • รูปแบบที่ 1: โครงสราง if
       จากการใชงานดานลาง คําสั่ง statement จะถูกเรียกทํางานก็ตอเมือนิพจนทางตรรกศาสตรที่
                                                                      ่
       กําหนดเปน condition มีคาเปนจริง
       if (condition)
          statement; // executed if the condition is true

       เนื่องจากโครงสรางขางตนอนุญาตใหเรากําหนดเงื่อนไขใหกับคําสั่งเพียงคําสั่งเดียวเทานั้น อยางไร
       ก็ตาม หากมีคาสั่งมากกวาหนึ่งภายใตเงื่อนไขเดียวกัน คําสั่งเหลานี้สามารถถูกจัดกลุมใหเปน
                     ํ
       เสมือนคําสั่งเดียวไดโดยการครอบคําสั่งทั้งหมดดวยวงเล็บปกกา ({...})
       if (condition)         {
          statement1;         // executed if the condition is true
          statement2;         // executed if the condition is true
          statement3;         // executed if the condition is true
          :
       }


   • รูปแบบที่ 2: โครงสราง if...else
       คําสั่ง statement1 จะถูกเรียกทํางานเมื่อนิพจนในตําแหนง condition มีคาเปนจริง หาก
       นิพจนดังกลาวมีคาเปนเท็จ คําสั่ง statement2 จะถูกเรียกทํางานแทน
       if (condition)
         statement1; //executed if the condition is true
       else
         statement2; //executed if the condition is false

       และเชนเคย เราสามารถใชงานโครงสราง if...else รวมกับวงเล็บปกกาหากมีคําสั่งที่ตองการ
       ใหทํางานภายใตเงื่อนไขมากกวาหนึ่ง
       if (condition)         {
         statementT1;         //executed if the condition is true
         statementT2;         //executed if the condition is true
       }
       else {
         statementF1;         //executed if the condition is false
         statementF2;         //executed if the condition is false
       }



                                     สถาบันสงเสริมการสอนวิทยาศาสตรและเทคโนโลยี                      53
ตัวอยางที่ 3.1 รหัสจําลอง (pseudo-code) ดานลางอธิบายขั้นตอนวิธีสําหรับใหโปรแกรมพิมพคําวา
Passed หากคะแนนของนักเรียนมีคามากกวาหรือเทากับ 60 ไมเชนนั้นใหพิมพคาวา Failed
                                                                               ํ
  if student's score is greater than or equal to 60
     Print "Passed"
  otherwise
     Print "Failed"

เราสามารถนํารหัสจําลองขางตนมาเขียนเปนโปรแกรมภาษา C# ไดดงนี้ (แสดงเพียงสวนสําคัญเทานั้น)
                                                           ั
  if (score >= 60)
    Console.WriteLine("Passed");
  else
    Console.WriteLine("Failed");

เนื่องจากคียเวิรด else เปนตัวกําหนดใหการพิมพคําวา Failed ทํางานเมื่อเงื่อนไข score >=       60
เปนเท็จ ดังนันหากเราแทนที่ else ดวยคําสั่ง if และใชเงื่อนไขที่ตรงขามกันคือ score < 60
              ้
โปรแกรมก็จะมีการทํางานเหมือนกับโปรแกรมขางบนทุกประการ
  if (score >= 60)
    Console.WriteLine("Passed");
  if (score < 60)
    Console.WriteLine("Failed");

ตัวอยางที่ 3.2 โปรแกรมตอไปนี้จะรอรับตัวเลขจากผูใชและใหคําตอบวาตัวเลขนั้น ๆ เปนเลขคู (even) หรือ
เลขคี่ (odd)
     • ใชรูปแบบ if
        using System;
        class OddOrEven {
          static void Main() {
            int N;
            Console.Write("Please input N: ");
            N = int.Parse(Console.ReadLine());
            if (N%2 == 0)
              Console.WriteLine("{0} is even", N); //true
            if (N%2 != 0)
              Console.WriteLine("{0} is odd", N); //true
          }
        }




54                                   สถาบันสงเสริมการสอนวิทยาศาสตรและเทคโนโลยี
• ใชรูปแบบ if...else
          using System;
          class OddOrEven {
            static void Main() {
              int N;
              Console.Write("Please input N: ");
              N = int.Parse(Console.ReadLine());
              if (N%2 == 0)
                Console.WriteLine("{0} is even", N); //true
              else
                Console.WriteLine("{0} is odd", N); //false
            }
          }

ตัวอยางที่ 3.3 บริษัทโทรศัพทมือถือแหงหนึ่งเสนอโปรโมชั่นใหกับลูกคาโดยมีการคํานวณคาธรรมเนียม
การใชงานดังนี้
     • สองนาทีแรก คิดนาทีละหาบาท
     • นาทีถัดมาคิดนาทีละสองบาท
      โปรแกรมดานลางจะรับคาจํานวนนาทีจากผูใช และคํานวณคาธรรมเนียมการใชงาน นอกจากนี้
ภายในโปรแกรมยังมีคอมเมนตกํากับเอาไวหลายจุดเพื่ออธิบายการทํางานของโปรแกรมในสวนตาง ๆ
 using System;
 class Cellphone {
    static void Main() {
       // Step 1: Take the number of minutes input
       Console.Write("Enter the number of minutes: ");
       int minutes = int.Parse(Console.ReadLine());

           // Step 2: Split the number of minutes into two parts,
           // the first two and the remaining
           int first, remaining;
           if (minutes > 2) {
              // Step 2.1: If the call takes more than two minutes,
              // then the first two minutes is used entirely and the
              // remaining is minutes subtracted by two
              first = 2;
              remaining = minutes - 2;
           }
           else {
              // Step 2.2: If the call takes less than 2 minutes,
              // these minutes are considered part of the first two
              first = minutes;
              remaining = 0;
           }

           // Step 3: Compute the fee based on the number of minutes
           // during the first two minutes, and the number of minutes
           // after the first two minutes
           int fee = (first*5) + (remaining*2);
           Console.WriteLine("The air time fee is {0} baht.", fee);
      }
 }



                                    สถาบันสงเสริมการสอนวิทยาศาสตรและเทคโนโลยี                    55
ตัวอยางผลการทํางาน
 Enter the number of minutes: 1
 The air time fee is 5 baht.


 Enter the number of minutes: 5
 The air time fee is 16 baht.




56                      สถาบันสงเสริมการสอนวิทยาศาสตรและเทคโนโลยี

Know3 2

  • 1.
    ใบความรูที่ 3.2 โครงสราง if และ if...else โครงสราง if เปนโครงสรางที่ใชควบคุมการทํางานของคําสั่งอื่น ๆ ภายใตเงื่อนไข (condition) ที่ กําหนด การใชงานนั้นมีสองรูปแบบคราว ๆ ไดแก • รูปแบบที่ 1: โครงสราง if จากการใชงานดานลาง คําสั่ง statement จะถูกเรียกทํางานก็ตอเมือนิพจนทางตรรกศาสตรที่ ่ กําหนดเปน condition มีคาเปนจริง if (condition) statement; // executed if the condition is true เนื่องจากโครงสรางขางตนอนุญาตใหเรากําหนดเงื่อนไขใหกับคําสั่งเพียงคําสั่งเดียวเทานั้น อยางไร ก็ตาม หากมีคาสั่งมากกวาหนึ่งภายใตเงื่อนไขเดียวกัน คําสั่งเหลานี้สามารถถูกจัดกลุมใหเปน ํ เสมือนคําสั่งเดียวไดโดยการครอบคําสั่งทั้งหมดดวยวงเล็บปกกา ({...}) if (condition) { statement1; // executed if the condition is true statement2; // executed if the condition is true statement3; // executed if the condition is true : } • รูปแบบที่ 2: โครงสราง if...else คําสั่ง statement1 จะถูกเรียกทํางานเมื่อนิพจนในตําแหนง condition มีคาเปนจริง หาก นิพจนดังกลาวมีคาเปนเท็จ คําสั่ง statement2 จะถูกเรียกทํางานแทน if (condition) statement1; //executed if the condition is true else statement2; //executed if the condition is false และเชนเคย เราสามารถใชงานโครงสราง if...else รวมกับวงเล็บปกกาหากมีคําสั่งที่ตองการ ใหทํางานภายใตเงื่อนไขมากกวาหนึ่ง if (condition) { statementT1; //executed if the condition is true statementT2; //executed if the condition is true } else { statementF1; //executed if the condition is false statementF2; //executed if the condition is false } สถาบันสงเสริมการสอนวิทยาศาสตรและเทคโนโลยี 53
  • 2.
    ตัวอยางที่ 3.1 รหัสจําลอง(pseudo-code) ดานลางอธิบายขั้นตอนวิธีสําหรับใหโปรแกรมพิมพคําวา Passed หากคะแนนของนักเรียนมีคามากกวาหรือเทากับ 60 ไมเชนนั้นใหพิมพคาวา Failed ํ if student's score is greater than or equal to 60 Print "Passed" otherwise Print "Failed" เราสามารถนํารหัสจําลองขางตนมาเขียนเปนโปรแกรมภาษา C# ไดดงนี้ (แสดงเพียงสวนสําคัญเทานั้น) ั if (score >= 60) Console.WriteLine("Passed"); else Console.WriteLine("Failed"); เนื่องจากคียเวิรด else เปนตัวกําหนดใหการพิมพคําวา Failed ทํางานเมื่อเงื่อนไข score >= 60 เปนเท็จ ดังนันหากเราแทนที่ else ดวยคําสั่ง if และใชเงื่อนไขที่ตรงขามกันคือ score < 60 ้ โปรแกรมก็จะมีการทํางานเหมือนกับโปรแกรมขางบนทุกประการ if (score >= 60) Console.WriteLine("Passed"); if (score < 60) Console.WriteLine("Failed"); ตัวอยางที่ 3.2 โปรแกรมตอไปนี้จะรอรับตัวเลขจากผูใชและใหคําตอบวาตัวเลขนั้น ๆ เปนเลขคู (even) หรือ เลขคี่ (odd) • ใชรูปแบบ if using System; class OddOrEven { static void Main() { int N; Console.Write("Please input N: "); N = int.Parse(Console.ReadLine()); if (N%2 == 0) Console.WriteLine("{0} is even", N); //true if (N%2 != 0) Console.WriteLine("{0} is odd", N); //true } } 54 สถาบันสงเสริมการสอนวิทยาศาสตรและเทคโนโลยี
  • 3.
    • ใชรูปแบบ if...else using System; class OddOrEven { static void Main() { int N; Console.Write("Please input N: "); N = int.Parse(Console.ReadLine()); if (N%2 == 0) Console.WriteLine("{0} is even", N); //true else Console.WriteLine("{0} is odd", N); //false } } ตัวอยางที่ 3.3 บริษัทโทรศัพทมือถือแหงหนึ่งเสนอโปรโมชั่นใหกับลูกคาโดยมีการคํานวณคาธรรมเนียม การใชงานดังนี้ • สองนาทีแรก คิดนาทีละหาบาท • นาทีถัดมาคิดนาทีละสองบาท โปรแกรมดานลางจะรับคาจํานวนนาทีจากผูใช และคํานวณคาธรรมเนียมการใชงาน นอกจากนี้ ภายในโปรแกรมยังมีคอมเมนตกํากับเอาไวหลายจุดเพื่ออธิบายการทํางานของโปรแกรมในสวนตาง ๆ using System; class Cellphone { static void Main() { // Step 1: Take the number of minutes input Console.Write("Enter the number of minutes: "); int minutes = int.Parse(Console.ReadLine()); // Step 2: Split the number of minutes into two parts, // the first two and the remaining int first, remaining; if (minutes > 2) { // Step 2.1: If the call takes more than two minutes, // then the first two minutes is used entirely and the // remaining is minutes subtracted by two first = 2; remaining = minutes - 2; } else { // Step 2.2: If the call takes less than 2 minutes, // these minutes are considered part of the first two first = minutes; remaining = 0; } // Step 3: Compute the fee based on the number of minutes // during the first two minutes, and the number of minutes // after the first two minutes int fee = (first*5) + (remaining*2); Console.WriteLine("The air time fee is {0} baht.", fee); } } สถาบันสงเสริมการสอนวิทยาศาสตรและเทคโนโลยี 55
  • 4.
    ตัวอยางผลการทํางาน Enter thenumber of minutes: 1 The air time fee is 5 baht. Enter the number of minutes: 5 The air time fee is 16 baht. 56 สถาบันสงเสริมการสอนวิทยาศาสตรและเทคโนโลยี