SlideShare a Scribd company logo
โครงสร้างควบคุมการทางานแบบควบคุมการทางาน
( if , if-else, if-else-if, switch..case)
1. คาสั่ง if
• คำสั่ง if ใช้ในกรณีมีทำงเลือกให้ทำงำนอยู่ ทำงเลือกเดียว โดย ถ้ำ
ตรวจสอบเงื่อนไข เป็นจริง จะทำงำนคำสั่งหลังเงื่อนไข
รูปแบบ
if (condition)
{
statement1;
statement2;
.
.
statement n;
}
ตัวอย่างโปรแกรม
int a,b,c;
a =2;
b = 5 ;
c=a*b;
If (c>=10)
printf(“hello”);
2. คาสั่ง if-else
• คำสั่ง if-else ใช้ในกรณีที่ต้องกำรให้โปรแกรมมีทำงเลือกกำรทำงำน
2 ทำงเลือก โดยกำรทำงำคำสั่ง if-else จะเริ่มตรวจสอบเงื่อนไข
หลัง if ก่อน ถ้ำเงื่อนไขหลัง if เป็นจริง จะทำงำนงำนภำยใต้คำสั่งหลัง
เงื่อนไข if แต่ถ้ำเป็นเท็จจะทำงำนภำยใต้คำสั่งหลัง else
รูปแบบคาสั่ง if –else
If (condition)
statement 1;
else
statement 2;
If (condition)
{
statement 1;
Statement2;
}
else
{
statement 3;
Statement4;
}
ตัวอย่าง
int a,b,c;
scanf(“%d”,a);
scanf(“%d”,b);
c= a+b;
If (c>=10)
printf(“hello”);
else
print(“Bye”)
3 if...else if
• ใช้ในกรณีที่ ต้องกำรให้โปรแกรมมีกำรตรวจสอบเงื่อนไขมำกกว่ำ 2 เงื่อนไข
( หลำยๆ เงื่อนไข)
รูปแบบ
if (condition)
statement 1;
else if(condition)
statement2;
else if(condition)
statement-3;
else statement 4;
ตัวอย่าง
int s;
printf(“Enter your score”);
scanf(“%d”,&s);
If(s>90)
printf(“A”);
else if(s>=80)
printf(“B”);
else if(s>=70)
printf(“C”);
else
printf(“D”);
คาสั่ง switch…case
• เป็นคำสั่งใช้ในโจทย์ที่มีลักษณะหลำยทำงเลือกโดยใช้กำรตรวจสอบ
เงื่อนไขร่วมกันเพียงครั้งเดียว ผลกำรตรวจสอบเงื่อนไขจะถูกนำไป
พิจำรณำเพื่อเลือกว่ำจะทำงำน ตำมทำงเลือกใด
รูปแบบ
Switch(variable)
{
case constant1 : statement 1;
break;
case constant 2: statement 2;
break;
case constant 3: statement3;
break;
default : statement4;
}
• variable คือ ตัวแปรชนิด int หรือ char
• constant คือค่ำคงที่ ชนิด int หรือ char โดยต้องกำหนดชนิด
ของ constant ให้ตรงกับชนิดข้อมูลของ variable
ถ้ำ variable = constant ของ case ใด โปรแกรมจะทำงำน
ตำมคำสั่งของ case นั้น
• break ; คำสั่งสำหรับออกจำก กำรทำงำนขอแต่ละ case
• default : คำสั่งที่โปรแกรมจะต้องทำก่อนออกจำกswitch
…caseอื่นๆถ้ำคำสั่งตัวแปรของ variable ไม่ตรงกับค่ำ
constant ในcaseใดๆเลยก็จะข้ำมมำทำคำสั่งหลัง default
ตัวอย่าง switch…case
Int a;
Scanf(“%d”,&a);
Switch(a)
{
case 0: printf(“zero”);
break;
case1 :printf(“red”);
break;
case2 : printf(“blue”);
break;
case3 : printf(“green”);
break;
case4 : printf(“yellow”);
break;
default : printf(“ Other case”);
break;
}

More Related Content

What's hot

Error handle-OOP(รูปแบบและลักษณะการ Error ในโปรแกรม)
Error handle-OOP(รูปแบบและลักษณะการ Error ในโปรแกรม)Error handle-OOP(รูปแบบและลักษณะการ Error ในโปรแกรม)
Error handle-OOP(รูปแบบและลักษณะการ Error ในโปรแกรม)
Anekwong Yoddumnern
 
บทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคบทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคTheeravaj Tum
 
การเขียนคำสั่งควบคุมแบบมีทางเลือก
การเขียนคำสั่งควบคุมแบบมีทางเลือกการเขียนคำสั่งควบคุมแบบมีทางเลือก
การเขียนคำสั่งควบคุมแบบมีทางเลือก
Thanon Paktanadechanon
 
หน่วยที่ 6 การเลือกทำด้วยคำสั่ง
หน่วยที่ 6 การเลือกทำด้วยคำสั่ง หน่วยที่ 6 การเลือกทำด้วยคำสั่ง
หน่วยที่ 6 การเลือกทำด้วยคำสั่ง
น.นิ นิยะดา สาระไกร
 
Know010
Know010Know010
Know010
the pooh
 
Know009
Know009Know009
Know009
the pooh
 
การเขียนคำสั่งแบบวนซ้ำ
การเขียนคำสั่งแบบวนซ้ำการเขียนคำสั่งแบบวนซ้ำ
การเขียนคำสั่งแบบวนซ้ำ
Gatesiree G'ate
 
แบบทดสอบหน่วยที่ 3
แบบทดสอบหน่วยที่ 3แบบทดสอบหน่วยที่ 3
แบบทดสอบหน่วยที่ 3
kruvisart
 
สถานะของโปรเซส
สถานะของโปรเซสสถานะของโปรเซส
สถานะของโปรเซส
Thanaporn Singsuk
 

What's hot (10)

Error handle-OOP(รูปแบบและลักษณะการ Error ในโปรแกรม)
Error handle-OOP(รูปแบบและลักษณะการ Error ในโปรแกรม)Error handle-OOP(รูปแบบและลักษณะการ Error ในโปรแกรม)
Error handle-OOP(รูปแบบและลักษณะการ Error ในโปรแกรม)
 
บทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคบทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบค
 
การเขียนคำสั่งควบคุมแบบมีทางเลือก
การเขียนคำสั่งควบคุมแบบมีทางเลือกการเขียนคำสั่งควบคุมแบบมีทางเลือก
การเขียนคำสั่งควบคุมแบบมีทางเลือก
 
หน่วยที่ 6 การเลือกทำด้วยคำสั่ง
หน่วยที่ 6 การเลือกทำด้วยคำสั่ง หน่วยที่ 6 การเลือกทำด้วยคำสั่ง
หน่วยที่ 6 การเลือกทำด้วยคำสั่ง
 
Know010
Know010Know010
Know010
 
Know009
Know009Know009
Know009
 
การเขียนคำสั่งแบบวนซ้ำ
การเขียนคำสั่งแบบวนซ้ำการเขียนคำสั่งแบบวนซ้ำ
การเขียนคำสั่งแบบวนซ้ำ
 
แบบทดสอบหน่วยที่ 3
แบบทดสอบหน่วยที่ 3แบบทดสอบหน่วยที่ 3
แบบทดสอบหน่วยที่ 3
 
บทที่ 3 คำสั่งควบคุม ส่วนที่ 1
บทที่ 3 คำสั่งควบคุม ส่วนที่ 1บทที่ 3 คำสั่งควบคุม ส่วนที่ 1
บทที่ 3 คำสั่งควบคุม ส่วนที่ 1
 
สถานะของโปรเซส
สถานะของโปรเซสสถานะของโปรเซส
สถานะของโปรเซส
 

Viewers also liked

บทที่ 9 การทำงานแบบมีทางเลือก
บทที่ 9 การทำงานแบบมีทางเลือกบทที่ 9 การทำงานแบบมีทางเลือก
บทที่ 9 การทำงานแบบมีทางเลือก
จูน นะค่ะ
 
ตัวแปร
ตัวแปรตัวแปร
ตัวแปรguest2b872f
 
บทที่ 9 การทำงานแบบมีทางเลือก
บทที่ 9 การทำงานแบบมีทางเลือกบทที่ 9 การทำงานแบบมีทางเลือก
บทที่ 9 การทำงานแบบมีทางเลือก
จูน นะค่ะ
 
10 Insightful Quotes On Designing A Better Customer Experience
10 Insightful Quotes On Designing A Better Customer Experience10 Insightful Quotes On Designing A Better Customer Experience
10 Insightful Quotes On Designing A Better Customer Experience
Yuan Wang
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming Convention
In a Rocket
 
How to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanHow to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media Plan
Post Planner
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting Personal
Kirsty Hulse
 
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika AldabaLightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
ux singapore
 
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job? Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Stanford GSB Corporate Governance Research Initiative
 

Viewers also liked (9)

บทที่ 9 การทำงานแบบมีทางเลือก
บทที่ 9 การทำงานแบบมีทางเลือกบทที่ 9 การทำงานแบบมีทางเลือก
บทที่ 9 การทำงานแบบมีทางเลือก
 
ตัวแปร
ตัวแปรตัวแปร
ตัวแปร
 
บทที่ 9 การทำงานแบบมีทางเลือก
บทที่ 9 การทำงานแบบมีทางเลือกบทที่ 9 การทำงานแบบมีทางเลือก
บทที่ 9 การทำงานแบบมีทางเลือก
 
10 Insightful Quotes On Designing A Better Customer Experience
10 Insightful Quotes On Designing A Better Customer Experience10 Insightful Quotes On Designing A Better Customer Experience
10 Insightful Quotes On Designing A Better Customer Experience
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming Convention
 
How to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanHow to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media Plan
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting Personal
 
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika AldabaLightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
 
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job? Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
 

โครงสร้างควบคุมการทำงานแบบควบคุมการทำงาน