ควบคุมการทำงานด้วยเงื่อนไข Control Statement

Slides:



Advertisements
งานนำเสนอที่คล้ายกัน
ครั้งที่ 9 Function(ต่อ).
Advertisements

สาขาวิชาคอมพิวเตอร์ คณะวิทยาศาสตร์และเทคโนโลยี
โดยอาจารย์ศิริพร ศักดิ์บุญญารัตน์ ครูชำนาญการ โรงเรียนมหิดลวิทยานุสรณ์
ตัวแปรชุด การเขียนโปรแกรมภาษาคอมพิวเตอร์ 1
การเขียนโปรแกรมด้วยคำสั่งเบื้องต้น
Computer Programming 1 1.หากต้องการพิมพ์ให้ได้ผลลัพธ์ดังนี้ต้องเขียน code อย่างไร (ใช้for)
ทบทวน & ลุยโจทย์ (Midterm)
ครั้งที่ 8 Function.
การแสดงผล และการรับข้อมูล การแสดงผล และการรับข้อมูล.
Control Statement if..else switch..case for while do-while.
คำสั่งในการควบคุมโปรแกรม
การรับค่าและแสดงผล.
บทที่ 3 ตอนที่ 1 คำสั่งเงื่อนไขและการตัดสินใจ(p
Lecture no. 5 Control Statements
C Programming Lecture no. 4 กราบเรียนท่านอาจารย์ และสวัสดีเพื่อนๆ
การควบคุมทิศทางการทำงาน
LAB # 3 Computer Programming 1
คำสั่ง while ควบคุมการทำงานวนซ้ำ กรณีระบบงานมีลักษณะตรวจสอบเงื่อนไขก่อน แล้วเข้าสู่ส่วนลำดับงานคำสั่งที่กำหนดไว้ การเขียนคำสั่ง while ต้องกำหนดนิพจน์แบบตรรกะเป็นเงื่อนไขมีค่าในหน่วยความจำเป็น.
ผังงานโปรแกรม (Program Flowchart)
Week 6 ประกาศค่าตัวแปร.
Week 15 C Programming.
คำสั่งเพื่อการกำหนดเงื่อนไข
การแสดงผล และการรับข้อมูล
รับและแสดงผลข้อมูล.
คำสั่งควบคุมการทำงาน
ตัวแปรชุด.
การประกาศตัวแปร “ตัวแปร” คือสิ่งที่เราสร้างขึ้นมาเพื่อใช้เก็บค่าต่างๆและอ้างอิงใช้งานภายในโปรแกรม ตามที่เรากำหนดขึ้น การสร้างตัวแปรขึ้นมาเราเรียกว่า.
Arrays.
C Programming Lecture no. 6: Function.
การเขียนโปรแกรมเชิงวัตถุ ด้วยภาษาจาวา
Relational Operators by Accords (IT SMART CLUB 2006) by Accords 1.
คำสั่งควบคุม (Control Statement)
บทที่ 11 การเขียนโปรแกรมภาษาซี
คำสั่งควบคุมเงื่อนไข และการทำงานเป็นรอบ
หน่วยที่ 1 พื้นฐานภาษา C
หน่วยที่ 4: คำสั่งควบคุมโปรแกรม (Control Flow Command)
คำสั่งควบคุมการทำงาน
รูปแบบ if-else if if (เงื่อนไข1) {
Flow Control.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2550 Structure Programming มหาวิทยาลัยเนชั่น.
การเขียนโปรแกรมภาษาซี
Chapter 4 คำสั่งควบคุมทิศทาง
ครูรัตติยา บุญเกิด.
ง30212 การเขียนโปรแกรมภาษาคอมพิวเตอร์ โรงเรียนปลวกแดงพิทยาคม
การรับและพิมพ์ข้อมูลเบื้องต้น
คำสั่งควบคุมการทำงานของ ActionScripts
Week 3 Flow Control in PHP
Week 2 Variables.
การประมวลผลสายอักขระ
Computer Programming for Engineers
บทที่ 9 เซต (Set) เซต หมายถึงกลุ่ม ฝูง พวก ชุด ของสิ่งใดสิ่งหนึ่ง เช่น เพศ ประกอบด้วย หญิง และ ชาย รายการที่อยู่ในเซต เรียกว่าสมาชิก เซตย่อย (Subset) คือ.
คำสั่งควบคุมขั้นตอน Flow control statements
2 /* ข้อความนี้เป็นเพียงคำอธิบาย ไม่มีผลต่อขั้นตอนการ ทำงานของโปรแกรม */ /* A simple program to display a line of text */ #include void main ( ) { printf.
การออกแบบโครงสร้าง (Structured Design)
คำสั่งเงื่อนไข (Condition Statement)
CHAPTER 4 Control Statements
การเขียนโปรแกรมแบบวนซ้ำ: คำสั่ง while คำสั่ง do….while
โครงสร้างภาษาซี #include <stdio.h> void main() {
คำสั่งเกี่ยวกับการรับ และแสดงผล
บทที่ 4 คำสั่งควบคุมแบบมีทางเลือก
PHP การตรวจสอบเงื่อนไข.
บทที่ 7 เงื่อนไขในภาษาซี
การเขียน แบบวนซ้ำ , วนลูป
CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka.
บทที่ 6 คำสั่งเงื่อนไข.
การเขียนโปรแกรม (ภาษาซี)
การรับและแสดงผลข้อมูล (Input/Output)
Computer Programming การเขียนโปรแกรม คอมพิวเตอร์ สัปดาห์ที่ 6 คำสั่งควบคุมการทำงานแบบ เงื่อนไขและคำสั่งควบคุมการ ทำงานแบบวนซ้ำ.
ผังงานโปรแกรม (Program Flowchart)
Computer Programming การเขียนโปรแกรมคอมพิวเตอร์ สัปดาห์ที่ 5 การเขียนโปรแกรมแบบทางเลือก การเขียนโปรแกรมแบบวนซ้ำ.
ใบสำเนางานนำเสนอ:

ควบคุมการทำงานด้วยเงื่อนไข Control Statement BC322 ครั้งที่ 3 ควบคุมการทำงานด้วยเงื่อนไข Control Statement

Sequence Statement เป็นการดำเนินการ คำสั่ง (statement) จากบนลงล่าง ทีละคำสั่ง add grade to total add grade to total total = total + grade; count = count + 1; add 1 to count add 1 to count ผังงาน (flowchart) คำสั่งใน C

Control Statement if..else switch..case for while do-while

if Statement if if … else … if … else if… if … else if… else …

 รูปแบบของคำสั่ง if หรือ if (เงื่อนไข) สิ่งที่ต้องการให้ทำ { สิ่งที่ต้องการให้ทำ 1 สิ่งที่ต้องการให้ทำ 2 สิ่งที่ต้องการให้ทำ 3 ………………. }

โครงสร้างการเลือกแบบ if grade >= 50 N Y write “Passed” if ( grade >= 50 ) printf( “Passed\n” ); ผังงาน (flowchart) คำสั่งใน C

Ex1 #include <stdio.h> void main() { int a,b; printf(“Input a :”);scanf(“%d”,&a); printf(“Input b :”);scanf(“%d”,&b); if (a>b) printf(“%5d\n”,a); if (b>a) printf(“%5\nd”,b); }

 รูปแบบของคำสั่ง if..else สิ่งที่ต้องการให้ทำถ้าเงื่อนไขเป็นจริง; else สิ่งที่ต้องการให้ทำถ้าเงื่อนไขเป็นเท็จ; หรือ if (เงื่อนไข) { สิ่งที่ต้องการให้ทำ 1 ถ้าเงื่อนไขเป็นจริง; สิ่งที่ต้องการให้ทำ 2 ถ้าเงื่อนไขเป็นจริง; } else สิ่งที่ต้องการให้ทำ 1 ถ้าเงื่อนไขเป็นเท็จ; สิ่งที่ต้องการให้ทำ 2 ถ้าเงื่อนไขเป็นเท็จ;

โครงสร้างการเลือกแบบ if else Y grade >= 50 ผังงาน (flowchart) write “Passed” write “Failed” if ( grade >= 50 ) printf( “Passed\n”); else printf (“Failed\n); คำสั่งใน C

Ex2 #include <stdio.h> void main() { int a,b; printf(“Input a :”);scanf(“%d”,&a); printf(“Input b :”);scanf(“%d”,&b); if (a==b) printf(“%d is equal %d”,a,b); else printf(“%d is not equal %d”,a,b); }

Ex3 จงเขียนคำสั่ง if ในแต่ละข้อต่อไปนี้ 1.ถ้า 10+20 หาร 3 ลงตัว ให้แสดงข้อความว่า“Oh Yes” แต่ถ้าไม่ลงตัว ให้แสดงข้อความว่า“Oh No” 2.ถ้าตัวเลขที่รับเข้ามา(num) เป็นจำนวนคู่ ให้แสดงข้อความว่า “Even Number” แต่ถ้าเป็นจำนวนคี่ ให้แสดงข้อความว่า “Odd Number”

ข้อควรระวัง อย่าให้การเว้นระยะ ย่อหน้า (indentation) int x = 5, y = 5; ข้อควรระวัง คำสั่ง (Statements) ค่า x y อย่าให้การเว้นระยะ ย่อหน้า (indentation) ทำให้เกิดความสับสนได้ if (x == 10) x = x + 1; y = y + 1; if ( x == 10 ) x = x + 1; y = y + 1; ภาษา C จะไม่สนใจ ช่องว่าง (space), แท็บ (tab), การขึ้นบรรทัดใหม่ (newline) if ( x == 10 ) { x = x + 1; y = y + 1; } การใส่เครื่องหมาย ; หลัง เงื่อนไข (condition) ใน if มีผลทำให้เกิด ความผิดพลาดทางตรรกะ (logic error) ได้ if ( x == 10 ); x = x + 1; y = y + 1;

Ex4 จงเขียนโปรแกรมรับค่าเลขจำนวนเต็ม 2 จำนวนจากแป้นพิมพ์ จากนั้นให้ตรวจสอบเลขจำนวนเต็มตัวแรกมากกว่าเลขจำนวนเต็มตัวที่สองหรือไม่ ถ้าใช่ให้หาผลบวกของตัวเลข 2 จำนวนและแสดงผลออกทางจอภาพ ถ้าไม่ใช่ให้นำตัวเลขตัวแรกคูณตัวที่สอง Input Enter Number1 : 10 Enter Number2 : 7 Output Result = 17 Enter Number1 : 7 Enter Number2 : 10 Result = 70

Solution Ex4 #include <stdio.h> void main() { int num1,num2,result;

 รูปแบบของคำสั่ง Nested if Statement คำสั่งชุดที่ 1 ; //กรณีเงื่อนไขที่ 1 เป็นจริง else if (เงื่อนไขที่ 2) คำสั่งชุดที่ 2; //กรณีเงื่อนไขที่ 2 เป็นจริง ... else คำสั่งชุดที่ n;//กรณีที่นอกเหนือจากเงื่อนไขข้างต้น คำสั่งชุดต่อไป;

Ex5 Enter score: 76 Enter score: 92 #include <stdio.h> #include <conio.h> void main() { int score; char grade; clrscr(); printf( "Enter score: " ); scanf( "%d", &score ); if ( score >= 90 ) grade = 'A'; else if ( score >= 80 ) grade = 'B'; else if ( score >= 70 ) grade = 'C'; else if ( score >= 60 ) grade = 'D'; else grade = 'F'; printf( "Grade is %c\n", grade ); } Ex5 Enter score: 76 Enter score: 92

Remark !!! การจับคู่ if/else ใช้หลักที่ว่า else จะจับคู่กับ if ตัวก่อนหน้านี้ที่ใกล้ที่สุด เราสามารถใช้เครื่องหมายวงเล็บ { } กับการใช้ if แบบซ้อน (nested if)ได้ เพื่อความชัดเจน

if ( condition1 ) { if ( condition2 ) statement1; else statement2; } statement3; if ( condition1 ) if ( condition2 ) statement1; else statement2; statement3; if ( conditionA ) statementA; else { if ( conditionB ) statementB; statementC; } if ( conditionA ) statementA; else if ( conditionB ) statementB; statementC;

Ex6 ร้านขายหนังสือการ์ตูนแห่งหนึ่งกำหนดให้หนังสือทุกเล่มราคา 50 บาท สำหรับสมาชิกหากซื้อหนังสือตั้งแต่ 200 บาทขึ้นไปมีส่วนลด 10% Input :……………………………………. Output :………………………………… Input Member (Y/N) : Y Amount : 4 Output Price = 200 Discount = 20 Net Price = 180 Member (Y/N) : N Discount = 0 Net Price = 200

Solution Ex6 #include <stdio.h> void main() { char mem; int amt; float price,disc,net;

การเชื่อมหลายเงื่อนไขโดยไม่จำเป็นต้องใส่วงเล็บ && (and) เป็นจริงเมื่อทุกเงื่อนไขต้องเป็นจริงทั้งหมด || (or) เป็นจริงเมื่อมีบางเงื่อนไขเป็นจริง ตัวอย่าง if (x > 10 && x <= 15) printf(“OK”); if (x > 10 || y > 15) x มากกว่า 10 และ x น้อยกว่าหรือเท่ากับ 15 จะทำให้เงื่อนไขเป็นจริง x > 10 หรือ y >15 เป็นจริงเพียงเงื่อนไขเดียว จะทำให้เงื่อนไขเป็นจริง

Solution Ex6 (อีกแบบ) #include <stdio.h> Void main() { char mem; int amt; float price,disc,net; printf(“Member (Y/N) :”);scanf(“%c”,&mem); printf(“Amount :”);scanf(“%d”,&amt); price = 50*amt; if (mem == ‘Y’ && price >= 200) disc = 0.1*price; else disc = 0; net=price-disc; printf(“Price = %.2f\n”,price); printf(“Discount = %.2f\n”,disc); printf(“Net = %.2f”,net); }

ใช้แทนคำสั่ง if…else ได้ เลือกทำเงื่อนไขใดเงื่อนไขหนึ่ง  คำสั่ง switch… case ใช้แทนคำสั่ง if…else ได้ เลือกทำเงื่อนไขใดเงื่อนไขหนึ่ง เหมาะกับกรณีที่มีทางเลือกมากกว่า 2

รูปแบบคำสั่ง switch ..case { case ค่า1 : คำสั่งชุดที่1; break; case ค่า 2 : คำสั่งชุดที่2; break; case ค่า 3 : คำสั่งชุดที่; break; . . . default : คำสั่ง; }

Ex7 #include<stdio.h> int main() { int year; printf(“Enter year : ”); scanf(“%d”, &year); switch (year) { case 1 : printf(“Freshman\n”);break; case 2 : printf(“Sophomore\n”);break; case 3 : printf(“Junior\n”);break; case 4 : printf(“Senior\n”);break; default : printf(“Invalid Year\n”); } return 0; ค่าที่อยู่หลัง switch ต้องเป็นค่าที่นับได้เป็นตัว ๆ เท่านั้น เช่น 0,1,2 หรือ a,b,c

Ex8 Enter a number [1-4]: 2 Enter a number [1-4]: 8 #include <stdio.h> #include <conio.h> void main() { int num; clrscr(); printf( "Enter a number [1-4]: "); scanf( "%d", &num ); switch (num) case 1: printf( "one\n" ); break; case 2: printf( "two\n" ); case 3: printf( "three\n" ); case 4: printf( "four\n" ); default: printf( "out of range\n" ); } Ex8 Enter a number [1-4]: 2 Enter a number [1-4]: 8

Ex9 ถ้า ไม่ใส่ คำสั่ง break; หลัง case Enter a number [1-4]: 2 #include <stdio.h> #include <conio.h> void main() { int num; clrscr(); printf( "Enter a number [1-4]: "); scanf( "%d", &num ); switch ( num ) case 1: printf( "one\n" ); case 2: printf( "two\n" ); case 3: printf( "three\n" ); case 4: printf( "four\n" ); default:printf( "out of range\n" ); } ถ้า ไม่ใส่ คำสั่ง break; หลัง case Enter a number [1-4]: 2 Enter a number [1-4]: 8

ชุดคำสั่งที่มีในแต่ละ case ไม่ต้องมีเครื่องหมายปีกกา { } กำกับ #include <stdio.h> #include <conio.h> void main() { char color; clrscr(); printf( "Enter a color of the Thai flag [R,W,B]: " ); scanf( "%c", &color ); switch (color) case 'R': case 'r': printf( "Red\n" ); printf( "represents the nation\n" ); break; case 'W': case 'w': printf( "White\n" ); printf( "evokes the religion\n" ); case 'B': case 'b': printf( "Blue\n" ); printf( "symbolizes the monarchy\n" ); default: printf( "Invalid color\n" ); } Enter a color of the Thai flag [R,W,B]: r Red represents the nation Enter a color of the Thai flag [R,W,B]: B Blue symbolizes the monarchy ชุดคำสั่งที่มีในแต่ละ case ไม่ต้องมีเครื่องหมายปีกกา { } กำกับ Ex10

Ex11 จงเขียนโปรแกรมเพื่อตรวจสอบตัวอักษรที่รับจากแป้นพิมพ์ ถ้าเป็นสระให้พิมพ์ข้อความบอกว่าเป็นสระ (vowel) แต่ถ้าไม่ใช่ให้บอกว่าเป็นตัวอักษรปกติ (consonant) ขั้นตอน รับตัวอักษร 1 ตัว -> ประกาศตัวแปรที่เป็นตัวอักษร ตรวจสอบตัวอักษรโดยใช้ switch

Solution Ex11 #include<stdio.h> int main() { char ch; printf(“Enter character : ”);scanf(“%c”,&ch); switch (ch) { case ‘a’ : printf(“%c is a vowel\n”,ch);break; case ‘e’ : printf(“%c is a vowel\n”,ch);break; case ‘i’ : printf(“%c is a vowel\n”,ch);break; case ‘o’ : printf(“%c is a vowel\n”,ch);break; case ‘u’ : printf(“%c is a vowel\n”,ch);break; default : printf(“%c is a consonant\n”,ch); } return 0;

Solution Ex11 (อีกแบบ) ตัวอย่าง ใน switch อาจมีการใช้ case #include <stdio.h> void main() { clrscr(); printf( "Enter character : " ); scanf( "%c", &ch ); switch(ch) ใน switch อาจมีการใช้ case หลาย case ร่วมกัน เพื่อทำให้สะดวกและสั้นขึ้น

Ex12 จงเขียนโปรแกรม โดยใช้ if และ switch case เพื่อรับค่าเลขจำนวนเต็ม และหาว่าเลขที่รับมานั้นใช้แทนชื่อวันอะไร ให้แสดงชื่อวันนั้นออกทางจอภาพโดยที่ เลข 1ใช้แทนวันจันทร์ เลข 2ใช้แทนวันอังคาร .................... เลข 7 ใช้แทนวันอาทิตย์ Input Enter number (1-7) : 1 Output Monday !!! Enter number (1-7) : 4 Thursday !!! Enter number (1-7) : 8 Invalid number !!!

Solution Ex12(แบบ if…else) #include<stdio.h> void main() { int num; printf(“Enter number (1-7) : ”);scanf(“%d”,&num); if (num==1) printf(“Monday !!!\n”); else if (num==2) printf(“Tuesday !!!\n”); else if (num==3) printf(“Wednesday !!!\n”); else if (num==4) printf(“Thursday !!!\n”); else if (num==5) printf(“Friday !!!\n”); else if (num==6) printf(“Saturday !!!\n”) else if (num==7) printf(“Sunday !!!\n”); else printf(“Invalid number\n”); }

Solution Ex12 (แบบ switch…case) #include<stdio.h> void main() { int num; printf(“Enter number (1-7) : ”);scanf(“%d”,&num);

Ex13 จงเปลี่ยนจาก if…else ให้เป็น switch…case if (number == 1) printf(“Have a good breakfast”); else if (number == 2) printf(“Have a nice lunch”); else if ((number == 3)||(number == 4)||(number == 5)) printf(“Have a copious supper”);

Solution Ex13