SlideShare a Scribd company logo
1 of 4
Download to read offline
ใบความรูที่ 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                      สถาบันสงเสริมการสอนวิทยาศาสตรและเทคโนโลยี

More Related Content

What's hot

แบบทดสอบหน่วยที่ 3
แบบทดสอบหน่วยที่ 3แบบทดสอบหน่วยที่ 3
แบบทดสอบหน่วยที่ 3kruvisart
 
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม (2)
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม (2)บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม (2)
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม (2)View Nudchanad
 
บทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคบทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคTheeravaj Tum
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1Pornpimon Aom
 
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุมบทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุมView Nudchanad
 
การเขียนคำสั่งควบคุมแบบวนซ้ำ
การเขียนคำสั่งควบคุมแบบวนซ้ำการเขียนคำสั่งควบคุมแบบวนซ้ำ
การเขียนคำสั่งควบคุมแบบวนซ้ำToey Sunisa
 
การเขียนคำสั่งควบคุมแบบมีทางเลือก.
การเขียนคำสั่งควบคุมแบบมีทางเลือก.การเขียนคำสั่งควบคุมแบบมีทางเลือก.
การเขียนคำสั่งควบคุมแบบมีทางเลือก.Mink Kamolwan
 
ข้อสอบก่อนเรียน หลังเรียน
ข้อสอบก่อนเรียน หลังเรียนข้อสอบก่อนเรียน หลังเรียน
ข้อสอบก่อนเรียน หลังเรียนOrapan Chamnan
 

What's hot (20)

Lesson3
Lesson3Lesson3
Lesson3
 
แบบทดสอบหน่วยที่ 3
แบบทดสอบหน่วยที่ 3แบบทดสอบหน่วยที่ 3
แบบทดสอบหน่วยที่ 3
 
06 for loops
06 for loops06 for loops
06 for loops
 
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม (2)
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม (2)บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม (2)
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม (2)
 
บทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคบทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบค
 
บทที่ 3 คำสั่งควบคุม ส่วนที่ 1
บทที่ 3 คำสั่งควบคุม ส่วนที่ 1บทที่ 3 คำสั่งควบคุม ส่วนที่ 1
บทที่ 3 คำสั่งควบคุม ส่วนที่ 1
 
Control structure
Control structureControl structure
Control structure
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1
 
Unit13
Unit13Unit13
Unit13
 
ฟังก์ชั่น Switch
ฟังก์ชั่น Switchฟังก์ชั่น Switch
ฟังก์ชั่น Switch
 
ฟังก์ชั่น break
ฟังก์ชั่น breakฟังก์ชั่น break
ฟังก์ชั่น break
 
ฟังก์ชั่น If ทางเลือกเดียว
ฟังก์ชั่น If ทางเลือกเดียวฟังก์ชั่น If ทางเลือกเดียว
ฟังก์ชั่น If ทางเลือกเดียว
 
ฟังก์ชั่น If หลายทางเลือก
ฟังก์ชั่น If หลายทางเลือกฟังก์ชั่น If หลายทางเลือก
ฟังก์ชั่น If หลายทางเลือก
 
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุมบทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม
 
การเขียนคำสั่งควบคุมแบบวนซ้ำ
การเขียนคำสั่งควบคุมแบบวนซ้ำการเขียนคำสั่งควบคุมแบบวนซ้ำ
การเขียนคำสั่งควบคุมแบบวนซ้ำ
 
ฟังก์ชั่น If สองทางเลือก
ฟังก์ชั่น If สองทางเลือกฟังก์ชั่น If สองทางเลือก
ฟังก์ชั่น If สองทางเลือก
 
การเขียนคำสั่งควบคุมแบบมีทางเลือก.
การเขียนคำสั่งควบคุมแบบมีทางเลือก.การเขียนคำสั่งควบคุมแบบมีทางเลือก.
การเขียนคำสั่งควบคุมแบบมีทางเลือก.
 
ข้อสอบก่อนเรียน หลังเรียน
ข้อสอบก่อนเรียน หลังเรียนข้อสอบก่อนเรียน หลังเรียน
ข้อสอบก่อนเรียน หลังเรียน
 
Know4 2
Know4 2Know4 2
Know4 2
 
C lang
C langC lang
C lang
 

Viewers also liked

La buena alimentacion
La buena alimentacionLa buena alimentacion
La buena alimentacionfisiostety
 
Rerovani ujredebi t mkurnaloba
Rerovani ujredebi t mkurnalobaRerovani ujredebi t mkurnaloba
Rerovani ujredebi t mkurnalobaMikheil Janjgava
 
Mundu Mira Kooperatiba Elkartea Urola Kostako Mankomunitatearekin elkarlanean
Mundu Mira Kooperatiba Elkartea Urola Kostako Mankomunitatearekin elkarlaneanMundu Mira Kooperatiba Elkartea Urola Kostako Mankomunitatearekin elkarlanean
Mundu Mira Kooperatiba Elkartea Urola Kostako Mankomunitatearekin elkarlaneanmundu-mira-kooperatiba
 
O olho de londres v
O olho de londres vO olho de londres v
O olho de londres vPaulo Bachur
 
A 029 dades català observatori desembre 2012 (19 03-13)
A 029 dades català observatori desembre 2012 (19 03-13)A 029 dades català observatori desembre 2012 (19 03-13)
A 029 dades català observatori desembre 2012 (19 03-13)Premsa Sant Cugat
 
Instal·lacions esportives girona
Instal·lacions esportives gironaInstal·lacions esportives girona
Instal·lacions esportives gironaig25
 
Sk1 - Aligned Views
Sk1 - Aligned ViewsSk1 - Aligned Views
Sk1 - Aligned ViewsMazer Visual
 
Descargar videos o audio de youtube
Descargar videos o audio de youtubeDescargar videos o audio de youtube
Descargar videos o audio de youtubeFelipe Campos Udt
 
Calculo vectorial
Calculo vectorialCalculo vectorial
Calculo vectorialFreddy Leon
 
MVT: LÒ ĐỐT RÁC THẢI KHÔNG TIÊU HAO DẦU
MVT: LÒ ĐỐT RÁC THẢI KHÔNG TIÊU HAO DẦUMVT: LÒ ĐỐT RÁC THẢI KHÔNG TIÊU HAO DẦU
MVT: LÒ ĐỐT RÁC THẢI KHÔNG TIÊU HAO DẦUanhtuyetnghean
 
El modernisme.palau macaya.equip08
El modernisme.palau macaya.equip08El modernisme.palau macaya.equip08
El modernisme.palau macaya.equip08novembre2010
 
Fazendo a coisa acontecer
Fazendo a coisa acontecerFazendo a coisa acontecer
Fazendo a coisa acontecerFellyph Cintra
 

Viewers also liked (20)

Pdf beta
Pdf betaPdf beta
Pdf beta
 
La buena alimentacion
La buena alimentacionLa buena alimentacion
La buena alimentacion
 
Rerovani ujredebi t mkurnaloba
Rerovani ujredebi t mkurnalobaRerovani ujredebi t mkurnaloba
Rerovani ujredebi t mkurnaloba
 
Tips tema 3
Tips tema 3Tips tema 3
Tips tema 3
 
Introduccion A La Innovacion Marzo 18
Introduccion A La Innovacion Marzo 18Introduccion A La Innovacion Marzo 18
Introduccion A La Innovacion Marzo 18
 
Somece seminario virtual_2010_2011
Somece seminario virtual_2010_2011 Somece seminario virtual_2010_2011
Somece seminario virtual_2010_2011
 
Mundu Mira Kooperatiba Elkartea Urola Kostako Mankomunitatearekin elkarlanean
Mundu Mira Kooperatiba Elkartea Urola Kostako Mankomunitatearekin elkarlaneanMundu Mira Kooperatiba Elkartea Urola Kostako Mankomunitatearekin elkarlanean
Mundu Mira Kooperatiba Elkartea Urola Kostako Mankomunitatearekin elkarlanean
 
O olho de londres v
O olho de londres vO olho de londres v
O olho de londres v
 
Roma antigua
Roma antiguaRoma antigua
Roma antigua
 
A 029 dades català observatori desembre 2012 (19 03-13)
A 029 dades català observatori desembre 2012 (19 03-13)A 029 dades català observatori desembre 2012 (19 03-13)
A 029 dades català observatori desembre 2012 (19 03-13)
 
Instal·lacions esportives girona
Instal·lacions esportives gironaInstal·lacions esportives girona
Instal·lacions esportives girona
 
Sk1 - Aligned Views
Sk1 - Aligned ViewsSk1 - Aligned Views
Sk1 - Aligned Views
 
Descargar videos o audio de youtube
Descargar videos o audio de youtubeDescargar videos o audio de youtube
Descargar videos o audio de youtube
 
Calculo vectorial
Calculo vectorialCalculo vectorial
Calculo vectorial
 
MVT: LÒ ĐỐT RÁC THẢI KHÔNG TIÊU HAO DẦU
MVT: LÒ ĐỐT RÁC THẢI KHÔNG TIÊU HAO DẦUMVT: LÒ ĐỐT RÁC THẢI KHÔNG TIÊU HAO DẦU
MVT: LÒ ĐỐT RÁC THẢI KHÔNG TIÊU HAO DẦU
 
Atividades
AtividadesAtividades
Atividades
 
El modernisme.palau macaya.equip08
El modernisme.palau macaya.equip08El modernisme.palau macaya.equip08
El modernisme.palau macaya.equip08
 
Fazendo a coisa acontecer
Fazendo a coisa acontecerFazendo a coisa acontecer
Fazendo a coisa acontecer
 
Michelangelo
MichelangeloMichelangelo
Michelangelo
 
Untitled 1
Untitled 1Untitled 1
Untitled 1
 

Similar to Know3 2

งาน อ.ทรงศักดิ์
งาน อ.ทรงศักดิ์งาน อ.ทรงศักดิ์
งาน อ.ทรงศักดิ์Oraphan4
 
คำสั่งควบคุมของโปรแกรม
คำสั่งควบคุมของโปรแกรมคำสั่งควบคุมของโปรแกรม
คำสั่งควบคุมของโปรแกรมParn Nichakorn
 
คำสั่งควบคุม
คำสั่งควบคุมคำสั่งควบคุม
คำสั่งควบคุมumaraporn
 
อัลกอริทึมและการวิเคราะห์ปัญหา
อัลกอริทึมและการวิเคราะห์ปัญหาอัลกอริทึมและการวิเคราะห์ปัญหา
อัลกอริทึมและการวิเคราะห์ปัญหาsupatra178
 
ม.3 รหัสจำลอง Pseudocode
ม.3 รหัสจำลอง Pseudocodeม.3 รหัสจำลอง Pseudocode
ม.3 รหัสจำลอง PseudocodeBansit Deelom
 
Java-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsJava-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsWongyos Keardsri
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
อัลกอริทึมและการวิเคราะห์ปัญหา
อัลกอริทึมและการวิเคราะห์ปัญหาอัลกอริทึมและการวิเคราะห์ปัญหา
อัลกอริทึมและการวิเคราะห์ปัญหาskiats
 
If statemet1
If statemet1If statemet1
If statemet1sup11
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมThanachart Numnonda
 

Similar to Know3 2 (19)

chapter 3 คำสั่งควบคุม
chapter 3 คำสั่งควบคุมchapter 3 คำสั่งควบคุม
chapter 3 คำสั่งควบคุม
 
Know009
Know009Know009
Know009
 
งาน อ.ทรงศักดิ์
งาน อ.ทรงศักดิ์งาน อ.ทรงศักดิ์
งาน อ.ทรงศักดิ์
 
Know3 4
Know3 4Know3 4
Know3 4
 
05 loops
05 loops05 loops
05 loops
 
คำสั่งควบคุมของโปรแกรม
คำสั่งควบคุมของโปรแกรมคำสั่งควบคุมของโปรแกรม
คำสั่งควบคุมของโปรแกรม
 
งานทำ Blog บทที่ 6
งานทำ Blog บทที่ 6งานทำ Blog บทที่ 6
งานทำ Blog บทที่ 6
 
คำสั่งควบคุม
คำสั่งควบคุมคำสั่งควบคุม
คำสั่งควบคุม
 
หลักการเขียนโปรแกรม
หลักการเขียนโปรแกรมหลักการเขียนโปรแกรม
หลักการเขียนโปรแกรม
 
อัลกอริทึมและการวิเคราะห์ปัญหา
อัลกอริทึมและการวิเคราะห์ปัญหาอัลกอริทึมและการวิเคราะห์ปัญหา
อัลกอริทึมและการวิเคราะห์ปัญหา
 
ม.3 รหัสจำลอง Pseudocode
ม.3 รหัสจำลอง Pseudocodeม.3 รหัสจำลอง Pseudocode
ม.3 รหัสจำลอง Pseudocode
 
Java-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsJava-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and Applications
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
อัลกอริทึมและการวิเคราะห์ปัญหา
อัลกอริทึมและการวิเคราะห์ปัญหาอัลกอริทึมและการวิเคราะห์ปัญหา
อัลกอริทึมและการวิเคราะห์ปัญหา
 
If statemet1
If statemet1If statemet1
If statemet1
 
ตัวอย่างโปรแกรมลงBlog
ตัวอย่างโปรแกรมลงBlogตัวอย่างโปรแกรมลงBlog
ตัวอย่างโปรแกรมลงBlog
 
Computer Programming 3
Computer Programming 3 Computer Programming 3
Computer Programming 3
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุม
 

More from โรงเรียนอุตรดิตถ์ดรุณี

More from โรงเรียนอุตรดิตถ์ดรุณี (20)

Ass4 5
Ass4 5Ass4 5
Ass4 5
 
Ass4 4
Ass4 4Ass4 4
Ass4 4
 
Ass4 3
Ass4 3Ass4 3
Ass4 3
 
Ass4 2
Ass4 2Ass4 2
Ass4 2
 
Ass4 1
Ass4 1Ass4 1
Ass4 1
 
Know4 1
Know4 1Know4 1
Know4 1
 
Ass3 5
Ass3 5Ass3 5
Ass3 5
 
Ass3 4
Ass3 4Ass3 4
Ass3 4
 
Ass3 3
Ass3 3Ass3 3
Ass3 3
 
Ass3 2
Ass3 2Ass3 2
Ass3 2
 
Ass3 1
Ass3 1Ass3 1
Ass3 1
 
Know3 3
Know3 3Know3 3
Know3 3
 
Know3 1
Know3 1Know3 1
Know3 1
 
Ass2 3
Ass2 3Ass2 3
Ass2 3
 
Ass2 2
Ass2 2Ass2 2
Ass2 2
 
Ass2 1
Ass2 1Ass2 1
Ass2 1
 
Know2 2
Know2 2Know2 2
Know2 2
 
Know2 1
Know2 1Know2 1
Know2 1
 
Ass1 2
Ass1 2Ass1 2
Ass1 2
 
Ass1 1
Ass1 1Ass1 1
Ass1 1
 

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 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 สถาบันสงเสริมการสอนวิทยาศาสตรและเทคโนโลยี