บทที่ 4

คำสั่งทดสอบเงื่อนไขเพื่อการตัดสินใจ (decision  statements)
คำสั่งทดสอบเงื่อนไขเพื่อการตัดสินใจ  เป็นคำสั่งที่มีการทดสอบเงื่อนไขก่อนที่จะทำงานตามคำสั่งที่กำหนดไว้  ซึ่งได้แก่คำสั่ง  if, if else, โครงสร้าง  else  if  (หรือ  nested  if)  และคำสั่ง  switch
คำสั่ง  if
if  เป็นคำสั่งที่สั่งให้มีการทดสอบเงื่อนไขก่อนที่จะไปทำงานตามคำสั่งที่กำหนดไว้
รูปแบบการใช้คำสั่ง  if
if  (expression)  statement;
หรือ
if  (expression)
{
statement(s);
}
โดยที่   
expression  คือ  นิพจน์เงื่อนไข  ซึ่งจะมีค่าจริงหรือเท็จอย่างใดอย่างหนึ่งเท่านั้น  ถ้าเงื่อนไขเป็นจริง  จะทำงานตามคำสั่งที่อยู่ใน  if  จากนั้นก็ออกจากคำสั่ง  if  ไปทำคำสั่งถัดไป  ถ้าเงื่อนไขเป็นเท็จจะออกจากคำสั่ง  if  ทันที
ลักษณะการทำงานของคำสั่ง  if  สามารถเขียนเป็นแผนผังได้ดังนี้

ผังงานแสดงลักษณะการทำงานของคำสั่ง  if
โปรแกรมตัวอย่าง แสดงการใช้คำสั่ง  if  เพื่อตรวจสอบค่าที่รับจากคีย์บอร์ด

 /*     if1.C.    */
#include <stdio.h>                                                                         
#include <conio.h>                                                                        
#include <ctype.h>                                                               
/*Capitalize Keys read from The Keyboard */                     
main()                                                                                  
{                                                                                           
char any_char;                                                                        
clrscr();                                                                                 
printf("Please type a lowercase letter : ");                              
   scanf("%c", &any_char);                                                     
  if(any_char >= 'a')                                                                 
  printf("In uppercase: %c \n", toupper(any_char));                
  getch();                                                                                 
  return(0);                                                                                 
}                                                                                                 


ผลลัพธ์ที่ได้จากโปรแกรม

    โปรแกรมตัวอย่าง แสดงการใช้คำสั่ง  if  เพื่อช่วยในการนับตัวอักขระและนับคำในประโยคที่ผู้ใช้พิมพ์


/*             if2.c        */
#include<stdio.h>                                                                
#include<conio.h>                                                               
void main(void)                                                                     
{                                                                                           
      int charcnt = 0, wordcnt = 0;                                           
      char ch;                                                                            
      clrscr();                                                                          
      printf("Type your  sentense or  a  phrase : "); 
      while( (ch=getche( ) )  != '\r'  )                         
{                                                                            
      charcnt++;                                                      
      if( ch==' ' ) wordcnt++;                                  
}  /*   end while  */                                              
printf("\n\nCharacter count is %d", charcnt);       
printf("\nWord count is %d", wordcnt+1);           
printf("\n\nPress any key back to program...");    
getch();                                                                
}                                                                             

ผลลัพธ์ที่ได้จากโปรแกรม
คำสั่ง  if  else
            if else เป็นคำสั่งที่สั่งให้มีการทดสอบเงื่อนไข  โดยมีการตัดสินใจแบบ 2 ทางเลือก
รูปแบบการใช้คำสั่ง  if  else
if  (expression)  {
statementA(s);
}
else  {    
statementB(s);
}
จากรูปแบบการใช้คำสั่ง  if  else  หมายความว่า  ถ้านิพจน์เงื่อนไข (expression)  มีค่าเป็นจริงจะทำตามคำสั่งชุด  A  (statementA(s);)  ถ้ามีค่าเป็นเท็จจะทำตามคำสั่งชุด  B  (statementB(s);)  เมื่อทำเสร็จก็ออกจากคำสั่งนี้
ลักษณะการทำงานของคำสั่ง  if  else  สามารถเขียนเป็นแผนผังได้ดังนี้

ผังงานแสดงลักษณะการทำงานของคำสั่ง  if  else
โปรแกรมตัวอย่าง แสดงการใช้คำสั่ง  if  else  เพื่อตรวจสอบค่าที่รับจากคีย์บอร์ด

 /*             if_else1.c               */
#include <stdio.h>                                                                         
#include <conio.h>                                                                        
#include <ctype.h>                                                                        
/*Capitalize Keys read from The Keyboard */                              
main()                                                                                           
{                                                                                                   
      char any_char;                                                                         
      clrscr();                                                                                 
      printf("Please type a lowercase letter : ");                             
      scanf("%c",&any_char);                                                       
      if(any_char < 'a')                                                                   
      printf("Sorry, I can not capitalize that.\n");                     
     else                                                                                  
            printf("Thank you. In uppercase : %c.",toupper (any_char));
  printf("\n\nPress any key back to program...");                            
  getch();                                                                                     
return(0);                                                                                      
}                                                                                                   


ผลลัพธ์ที่ได้จากโปรแกรม
            กรณีที่เติมตัวอักษรพิมพ์เล็ก

  
             กรณีที่เติมตัวอักษรพิมพ์ใหญ่

 
โปรแกรมตัวอย่าง แสดงการใช้คำสั่ง  if else  เพื่อเปรียบเทียบค่าตัวเลขที่ผู้ใช้เติม

 /* if_else2.c             */
#include<stdio.h>                                                           
#include<conio.h>                                                         
void main(void)                                                               
{                                                                                     
      int  x, y;    char ch;                                                    
      clrscr( );                                                                    
      printf("Enter  an integer  of  X : ");                        
      scanf("%d",&x);                                                       
      printf("Enter  an integer  of  Y : ");                        
      scanf("%d",&y);    ch=getchar( );                            
      if ( x==y )                                                                 
            printf("\nX  equal to Y = %d\n",x);                   
      else                                                                         
            printf("\nX = %d  not equal to Y= %d\n",x,y);
      printf("\n\nPress any key back to program...");       
      getch();                                                                   
}                                                                                     

ผลลัพธ์ที่ได้จากโปรแกรม
            กรณีที่เติมตัวเลข  2  ตัวเท่ากัน
  

             กรณีที่เติมตัวเลข  2  ตัวไม่เท่ากัน

                   

คำสั่งโครงสร้าง  else  if  (คำสั่ง  nested  if)
else  if  เป็นโครงสร้างที่ทำให้เราสามารถใช้คำสั่ง  if  else  ซ้อนกันได้เรื่อย ๆ ส่วนมากจะใช้ในการตัดสินใจที่มากกว่า  2  ทางเลือกขึ้นไป  บางครั้งอาจเรียกโครงสร้างนี้ว่า  nested  if   
รูปแบบการใช้โครงสร้าง  nested  if  หรือ  else  if
else  if  (expression)  {
statementA(s);
}
else  if  (expression){         
statementB(s);
}
else  if  (expression){         
……..
ข้อควรระวังในการใช้  nested  if
การเขียนคำสั่ง  nested  if  ค่อนข้างยุ่งยาก  อาจเกิดความสับสนได้  ควรเขียนคำสั่ง nested  if  ให้เยื้องกันเพื่อความสะดวกในการแก้ไขคำสั่งในภายหลัง
โปรแกรมตัวอย่าง แสดงการใช้คำสั่ง  nested if เพื่อตรวจสอบคะแนนที่ผู้ใช้เติม  แล้วให้เกรดตามเงื่อนไขที่กำหนดในโปรแกรม

 /*  nestif1.c            */
#include<stdio.h>                                                                   
#include<conio.h>                                                                 
#include<stdlib.h>                                                                   
void main(void)                                                                       
{                                                                                             
      int score, n, i;                                                                     
      char grade;                                                                         
      char numstr[20];                                                                
      clrscr( );                                                                   
      printf("Enter Number of Students : ");                 
      n = atoi(gets(numstr));                                           
      for ( i=1; i<=n; i++ ){                                            
            printf("\nEnter score of student #%d : ", i);    
            score = atoi(gets(numstr));                              
            if ( score >= 80 )                                             
                  grade = 'A';                                               
                  else if ( score >= 70 )                               
                        grade = 'B';                                        
                  else if ( score >= 60 )                              
                        grade = 'C';                                        
                  else if ( score >= 50 )                              
                        grade = 'D';                                       
                  else  grade = 'F';                                     
            printf("Score = %d, Grade of std #%d is %c\n",score,i,grade);
} /* end for */                                                           
printf("\n\nPress any key back to program...");         
getch();                                                                     
}                                                                                 

ผลลัพธ์ที่ได้จากโปรแกรม
  
โปรแกรมตัวอย่าง แสดงการใช้คำสั่ง   nested  if  เพื่อเขียนเส้นทะแยงมุมในกรอบสี่เหลี่ยมพื้นสีเทา

 /*             nestif2.c */
/* Prints two diagonal lines on screen  */                                      
#include <stdio.h>                                                                         
#include <conio.h>                                                                        
#include <dos.h>                                                                           
void main(void)                                                                               
{                                                                                                    
      int x, y;                                                                                
      clrscr();                                                                                  
      printf("\n");                                                                            
      for (y=1; y<24; y++)                                                             
{                                                                                                    
            for (x=1; x<24; x++)                                                         
                  if ( x == y )                                                                   
                        printf("\xDB");                                                     
                  else                                                                             
                  if ( x == 24 - y )                                                
                        printf("\xDB");                                          
                  else                                                                  
printf("\xB0");                                                                 
printf("\n");                                                                     
delay(200);                                                                       
}                                                                                     
printf("\n\nPress any key back to program...");              
getche();                                                                        
}                                                                                              



ผลลัพธ์ที่ได้จากโปรแกรม

  
คำสั่ง  switch
switch  เป็นคำสั่งที่ใช้ทดสอบเงื่อนไขในกรณีที่มีทางเลือกสำหรับตัดสินใจมากกว่า  2  ทางขึ้นไปเช่นเดียวกันกับ  nested  if  โดยมากนิยมใช้คำสั่ง  switch  แทนคำสั่ง  nested  if  เพราะมีรูปแบบการใช้คำสั่งที่ง่ายและสะดวกในการแก้ไขคำสั่งเมื่อมีข้อผิดพลาดเกิดขึ้น
รูปแบบการใช้คำสั่ง  switch
switch  (expression) {
case  expression1:
statement(s);  break;
case  expression2:
statement(s);  break;
…..
case  expressionN:
statement(s);  break;
default:
statement(s);
}
โดยที่ 
expression  คือ  นิพจน์  หรือตัวแปรที่จะใช้เปรียบเทียบกับนิพจน์  expression1, expression2, …, expressionN  ว่ามีค่าตรงกับนิพจน์ใด
expression1, expression2, …, expressionN  คือ  นิพจน์  หรือค่าคงที่ในเงื่อนไขที่  1, 2, 3, …, N  ตามลำดับ
break  คือ  คำสั่งที่จะต้องใส่ไว้ในแต่ละ  case  เพื่อเป็นการบอกให้ออกจากคำสั่ง  switch  หลังจากทำคำสั่งที่อยู่ใน  case  นั้น ๆ แล้ว  ถ้าหากไม่มีคำสั่ง  break  ใน  case  ใด  เมื่อทำงานจบ  case  นั้นแล้ว  จะทำงานใน  case  ถัดไปจนกว่าจะเจอคำสั่ง  break  ซึ่งทำให้เกิดการทำงานผิดพลาดได้
default  คือ  กรณีที่  expression  ไม่ตรงกับเงื่อนไขใด ๆ เลย  ให้ทำงานตามคำสั่งที่เขียนไว้ใน  default  โดย  default  นี้ไม่จำเป็นต้องใส่คำสั่ง  break  เอาไว้  เพราะ  default  เป็นกรณีสุดท้ายของคำสั่ง  switch 
ข้อควรระวังในการใช้คำสั่ง  switch 

1) ถ้าใช้คำสั่ง  switch  ในแต่ละกรณี  (case)  จะต้องใส่คำสั่ง  break  เอาไว้ด้วยมิฉะนั้นจะเกิดการทำงานซ้ำใน  case  ต่อมาจนกว่าจะเจอคำสั่ง  break  ยกเว้นกรณี  default  ไม่ต้องใส่คำสั่ง  break
2) expression  ที่อยู่หลังคำสั่ง  switch  ควรใช้เป็นตัวแปร เพื่อจะได้สะดวกในการนำไปเปรียบเทียบกับกรณีต่าง ๆ ส่วน  expression1, expression2, …, expressionN  ที่อยู่หลัง case  ต่าง ๆ ควรใช้เป็นค่าคงที่ 
โปรแกรมตัวอย่าง แสดงการใช้คำสั่ง  switch  เพื่อตรวจสอบเกรดที่ผู้ใช้เติม  ว่าตรงกับกรณีใด  แล้วแสดงเกรดที่เป็นตัวเลขออกจอภาพ

 /*             switch.c                 */
#include<stdio.h>                                                                    
#include<conio.h>                                                                   
#include<stdlib.h>                                                                   
#include<ctype.h>                                                                   
void main(void)                                                                         
{                                                                                               
      int  n, i;                                                                                
      float  gradepoint;                                                                 
      char grade;                                                                          
      char numstr[20];                                                                 
      clrscr( );                                                                              
      printf("Enter Number of Students : ");                            
      n = atoi(gets(numstr)) ;                                                     
      for ( i=1; i<=n; i++ ) {                                                    
            printf("\nEnter grade of student #%d: ", i);                
            grade = getche( );                                                        
            switch(toupper(grade)) {                                          
                  case 'A':                                                                
                        gradepoint = 4.0; break;                                 
                  case 'B':                                                                
                        gradepoint = 3.0; break;                                
                  case 'C':                                                               
                        gradepoint = 2.0; break;                               
                  case 'D':                                                              
                        gradepoint = 1.0; break;                               
                  default:  gradepoint = 0.0;                                 
            } /* end switch */                                                  
            printf("\nGrade Point of Student#%d is %.2f\n",i,gradepoint); 
      } /* end for */                                                                     
printf("\n\nPress any key back to program...");                         
getch();                                                                                     
}  /* end main */                                                                        

ผลลัพธ์ที่ได้จากโปรแกรม