ใช้เพื่อเลือกหนึ่งในการตัดสินใจหลายครั้ง 'สวิตช์' จะทดสอบค่าอย่างต่อเนื่องกับรายการของจำนวนเต็ม (หรือ) ค่าคงที่อักขระ เมื่อพบการจับคู่ คำสั่ง (หรือ) คำสั่งที่เกี่ยวข้องกับค่านั้นจะถูกดำเนินการ
ไวยากรณ์
ไวยากรณ์ได้รับด้านล่าง −
switch (expression){
case value1 : stmt1;
break;
case value2 : stmt2;
break;
- - - - - -
default : stmt – x;
} อัลกอริทึม
อ้างถึงอัลกอริทึมที่ระบุด้านล่าง -
Step 1: Declare variables. Step 2: Read expression variable. Step 3: Switch(expression) If value 1 is select : stmt 1 executes break (exists from switch) If value 2 is select : stmt 2 executes ;break If value 3 is select : stmt 3 executes; break …………………………………………… Default : stmt-x executes;

ตัวอย่าง
โปรแกรม C ต่อไปนี้สาธิตการใช้คำสั่ง switch -
#include<stdio.h>
main ( ){
int n;
printf ("enter a number");
scanf ("%d", &n);
switch (n){
case 0 : printf ("zero");
break;
case 1 : printf ("one");
break;
default : printf ("wrong choice");
}
} ผลลัพธ์
คุณจะเห็นผลลัพธ์ต่อไปนี้ -
enter a number 1 One
พิจารณาโปรแกรมอื่นในกรณีสวิตช์ตามที่กล่าวไว้ด้านล่าง -
ตัวอย่าง
#include<stdio.h>
int main(){
char grade;
printf("Enter the grade of a student:\n");
scanf("%c",&grade);
switch(grade){
case 'A': printf("Distiction\n");
break;
case 'B': printf("First class\n");
break;
case 'C': printf("second class \n");
break;
case 'D': printf("third class\n");
break;
default : printf("Fail");
}
printf("Student grade=%c",grade);
return 0;
} ผลลัพธ์
คุณจะเห็นผลลัพธ์ต่อไปนี้ -
Run 1:Enter the grade of a student:A Distiction Student grade=A Run 2: Enter the grade of a student:C Second class Student grade=C