Control Structure
มี 3 แบบคือ
1.Sequential(แบบลำดับ)มี 3 แบบคือ
2.Selection(แบบเลือก)
3.Loop(แบบทำซ้ำ)
อาจจะประกอบด้วยคำสั่งเดียวหรือทำงานร่วมกับ If-then หรือ Loop ต่างๆexample.
#include<stdio.h>
int main(){
int num=5;
printf("Hello World\n");
printf("%d \n",num);
return 0;
}
การทำงาน จะตรวจสอบเงื่อนไขถ้าจริงทำคำสั่งต่างๆด้าน true
รูปแบบไวยากรณ์
if(logical expression){
statemets;
}
example.
If(total>=80){
printf("Excellent \n");
}
2.2. If-then-else
การทำงาน จะตรวจสอบเงื่อนไขถ้าเป็นจริงทำคำสั่งต่างๆด้าน true
ถ้าเป็นเท็จทำคำสั่งต่างๆด้าน false
รูปแบบไวยากรณ์
If(logical expression){
true statements;
}
else{
false statements;
}
example.
If(temp<32){
printf("Cover Tomatoes \n");
}
else{
printf("Uncover Tomatoes \n");
}
2.3. If-If-If
การทำงาน จะตรวจสอบเงื่อนไข ถ้าเป็นจริงทำตามคำสั่งฝั่งด้าน
true ถ้าเป็นเท็จทำคำสั่งด้าน false ปละตรวจสอบเงื่อนไขของ If
ตัวใหม่

รูปแบบไวยากรณ์
If(logical expression){
statement 1
}else if(logical expression){
statement 2
}else if(logical expression){
statement 3
}else{
statement 4
}
example.
int total=79;
if(total>=80){
printf("Excellent \n");
}
else if(total>=70){
printf("Good \n");
}
else if(total>=60){
printf("Fair \n");
}
else{
printf("Poor \n");
}
2.4. Case
การทำงาน จะตรวจสอบเงื่อนไขทีละกรณี ถ้าจริงงทำคำสั่งด้าน
true ถ้าเป็นเท็จ ตรวจสอบเงื่อนไขต่อไป
รูปแบบไวยากรณ์
switch(expression){
case 1:statements 1;break;
case 2:statements 2;break;
case N:statements N;break;
default:statement N
รูปแบบไวยากรณ์
-> Initial statementss;








ไม่มีความคิดเห็น:
แสดงความคิดเห็น