ค่าในข้อใดไม่สามารถใช้เป็นค่าหลัง case ในคำสั่ง switch..case ได้

Interesting facts about switch statement in C

Switch เป็นคำสั่งควบคุมที่อนุญาตให้ค่าเปลี่ยนการควบคุมการดำเนินการ ต่อไปนี้เป็นข้อเท็จจริงที่น่าสนใจเกี่ยวกับคำสั่ง switch (เปลี่ยน)

  1. นิพจน์ที่ใช้ใน switch จะต้องเป็นประเภทหนึ่ง (int, char และ enum) ไม่อนุญาตให้ใช้ประเภทอื่นในการแสดงออก
// float is not allowed in switch#include <stdio.h>
int main(){
float x = 1.1;
switch (x){
case 1.1: printf("Choice is 1");
break;
default: printf("Choice other than 1, 2 and 3");
break;
}
return 0;
}
Output: Compiler Error: switch quantity not an integer

ใน Java อนุญาตให้ใช้สตริงได้ในสวิตช์ (ดูที่นี่)

2. ข้อความทั้งหมดที่ตามหลังตัวพิมพ์ที่ตรงกันดำเนินการจนกว่าจะถึงคำสั่ง break

// There is no break in all cases (ไม่มีการหยุดพักในทุกกรณี)#include <stdio.h>
int main(){
int x = 2;
switch (x){
case 1: printf("Choice is 1\n");
case 2: printf("Choice is 2\n");
case 3: printf("Choice is 3\n");
default: printf("Choice other than 1, 2 and 3\n");
}
return 0;
}
Output:
Choice is 2
Choice is 3
Choice other than 1, 2 and 3

// There is no break in some cases (ไม่มีการหยุดพักในบางกรณี)#include <stdio.h>
int main(){
int x = 2;
switch (x){
case 1: printf("Choice is 1\n");
case 2: printf("Choice is 2\n");
case 3: printf("Choice is 3\n");
case 4: printf("Choice is 4\n");
break;
default: printf("Choice other than 1, 2, 3 and 4\n");
break;
}
printf("After Switch");
return 0;
}
Output:
Choice is 2
Choice is 3
Choice is 4
After Switch

3. บล็อกเริ่มต้นสามารถวางได้ทุกที่ ตำแหน่งเริ่มต้นไม่สำคัญว่าจะยังคงดำเนินการหากไม่พบที่ตรงกัน

// The default block is placed above other cases.
(บล็อกเริ่มต้นถูกวางไว้เหนือกรณีอื่นๆ)
#include <stdio.h>
int main(){
int x = 4;
switch (x){
default: printf("Choice other than 1 and 2");
break;
case 1: printf("Choice is 1");
break;
case 2: printf("Choice is 2");
break;
}
return 0;
}
Output: Choice other than 1 and 2

4. นิพจน์ที่สำคัญที่ใช้ใน ป้ายกำกับ จะต้องเป็นนิพจน์คงที่

// A program with variable expressions in labels
โปรแกรมที่มีนิพจน์ตัวแปรในป้ายกำกับ
#include <stdio.h>
int main(){
int x = 2;
int arr[] = {1, 2, 3};
switch (x){
case arr[0]: printf("Choice 1\n");
case arr[1]: printf("Choice 2\n");
case arr[2]: printf("Choice 3\n");
}
return 0;
}
Output:Compiler Error: case label does not reduce to an integer constant

5. ข้อความที่เขียนไว้ด้านบนจะไม่ถูกดำเนินการหลังจากคำสั่งสวิตช์การควบคุมการถ่ายโอนไปยังกรณีที่ตรงกันคำสั่งที่เขียนก่อนกรณีจะไม่ถูกดำเนินการ

// Statements before all cases are never executed
(คำชี้แจงก่อนทุกกรณีจะไม่ดำเนินการ)
#include <stdio.h>
int main(){
int x = 1;
switch (x){
x = x + 1; // This statement is not executed (คำสั่งนี้ไม่ได้ถูกดำเนินการ)
case 1: printf("Choice is 1");
break;
case 2: printf("Choice is 2");
break;
default: printf("Choice other than 1 and 2");
break;
}
return 0;
}
Output: Choice is 1

6. สองกรณีป้ายกำกับไม่สามารถมีค่าเดียวกัน

// Program where two case labels have same value
(โปรแกรมที่สองกรณีป้ายกำกับมีค่าเท่ากัน)
#include <stdio.h>
int main(){
int x = 1;
switch (x){
case 2: printf("Choice is 1");
break;
case 1+1: printf("Choice is 2");
break;
}
return 0;
}
Output: Choice is 1

Toplist

โพสต์ล่าสุด

แท็ก

ไทยแปลอังกฤษ แปลภาษาไทย โปรแกรม-แปล-ภาษา-อังกฤษ พร้อม-คำ-อ่าน lmyour แปลภาษา แปลภาษาอังกฤษเป็นไทย pantip ไทยแปลอังกฤษ ประโยค แอพแปลภาษาอาหรับเป็นไทย ห่อหมกฮวกไปฝากป้าmv ระเบียบกระทรวงการคลังว่าด้วยการจัดซื้อจัดจ้างและการบริหารพัสดุภาครัฐ พ.ศ. 2560 แปลภาษาอาหรับ-ไทย Terjemahan พจนานุกรมศัพท์ทหาร หยน แปลภาษา มาเลเซีย ไทย Bahasa Thailand ข้อสอบภาษาอังกฤษ พร้อมเฉลย pdf บบบย tor คือ จัดซื้อจัดจ้าง การ์ดแคปเตอร์ซากุระ ภาค 4 ชขภใ ยศทหารบก เรียงลําดับ ห่อหมกฮวกไปฝากป้า หนังเต็มเรื่อง เขียน อาหรับ แปลไทย แปลภาษาอิสลามเป็นไทย Google map กรมพัฒนาฝีมือแรงงาน อบรมออนไลน์ กระบวนการบริหารทรัพยากรมนุษย์ 8 ขั้นตอน ข้อสอบคณิตศาสตร์ พร้อมเฉลย ค้นหา ประวัติ นามสกุล อาจารย์ ตจต แจ้ง ประกาศ น้ำประปาไม่ไหล แปลบาลีเป็นไทย แปลภาษา ถ่ายรูป แปลภาษาจีน แปลภาษามลายู ยาวี โรงพยาบาลภมูพลอดุยเดช ที่อยู่ Google Drive Info TOR คือ กรมพัฒนาฝีมือแรงงาน ช่างไฟฟ้า กรมพัฒนาฝีมือแรงงาน อบรมฟรี 2566 กลยุทธ์ทางการตลาด มีอะไรบ้าง การบริหารทรัพยากรมนุษย์ มีอะไรบ้าง การประปาส่วนภูมิภาค การ์ดแคปเตอร์ซากุระ ภาค 3 ขขขขบบบยข ่ส ข่าว น้ำประปา วันนี้ ข้อสอบโอเน็ต ม.6 มีกี่ตอน ตารางธาตุ ประปาไม่ไหล วันนี้