160 likes | 244 Views
Chapter 4 คำสั่งควบคุมทิศทาง. PROGRAMMIMG I. คำสั่งควบคุมทิศทาง. ใช้ในกรณีที่โจทย์ปัญหาที่ต้องมีการเลือก หรือมีเงื่อนไขในการเลือกทำงาน เช่น ถ้าสถานการณ์เป็น A ให้ทำงานอย่างหนึ่ง ส่วนถ้าสถานการณ์เป็น B ก็ให้ทำงานอีกอย่างหนึ่งแทน
E N D
Chapter 4คำสั่งควบคุมทิศทาง PROGRAMMIMG I
คำสั่งควบคุมทิศทาง • ใช้ในกรณีที่โจทย์ปัญหาที่ต้องมีการเลือก หรือมีเงื่อนไขในการเลือกทำงาน เช่น ถ้าสถานการณ์เป็น A ให้ทำงานอย่างหนึ่ง ส่วนถ้าสถานการณ์เป็น B ก็ให้ทำงานอีกอย่างหนึ่งแทน • ในภาษาซี คำสั่งที่ใช้ควบคุมทิศทางการทำงานของโปรแกรม มี 2 ประเภทหลักๆ • If-else statement • Switch-case statement
if - else statement • if (condition) statement; • if (condition) { statement 1; statement 2; . . statement n; } • if (condition) statement 1; else statement 2;
if - else statement condition condition เท็จ เท็จ จริง จริง statement statement statement
ตัวอย่าง Start Get C n C != D y ‘a’ <= C <= ‘z’ n y C ==‘\n’ n Print C y Print double new line Stop
ตัวอย่าง • จงเขียนโปรแกรมภาษาซี เพื่อหาค่ามากที่สุดของเลขจำนวนเต็ม 3 จำนวน • วิเคราะห์ • ผลลัพธ์ ค่าที่มากที่สุด (max) • ข้อมูลนำเข้า เลขจำนวนเต็ม 3 จำนวน (a, b, c) • วิธีประมวล • รับข้อมูล • เปรียบเทียบหาค่าที่มากที่สุด • แสดงผล
start จริง เท็จ a > b Get a, b, c max = a max = b max < c เท็จ จริง max = c Print max stop หมายเหตุ a, b, c เป็นเลขจำนวนเต็ม
#include <stdio.h> void main( ) { int a, b, c, max; printf(“Enter integer \na = ”); scanf(“%d”, &a); printf(“b = ”); scanf(“%d”, &b); printf(“c = ”); scanf(“%d”, &c); if (a > b) max = a; else max = b; if (max < c) max = c; printf(“The maximum is %d”, max); }
ตัวอย่าง จงเขียนโปรแกรมเพื่อเปรียบเทียบตัวเลข 2 จำนวน โดยโปรแกรมจะให้ผู้ใช้ป้อนจำนวนเต็มเข้ามา 2 จำนวนเพื่อเปรียบเทียบ แล้วจึงแสดงผลการเปรียบเทียบออกทางหน้าจอเพื่อแจ้งว่า จำนวนแรกมากกว่า, น้อยกว่า, หรือเท่ากับจำนวนหลัง
#include<stdio.h> Main() { int first, second; printf(“Enter first number: ”); scanf(“%d”,&first); printf(“Enter second number: ”); scanf(“%d”,&second); if(first > second) printf(“%d is bigger than %d\n”,first,second); else if(first == second) printf(“%d is equal %d\n”,first,second); else printf(“%d is less than %d\n”,first,second); }
switch–case statement • เป็นคำสั่งเพื่อเลือกการทำงานจะใช้ในกรณีที่มีทางเลือกให้ทำงานหลายทาง โดยใช้การตรวจสอบเงื่อนไขร่วมกันเพียงครั้งเดียว ผลการตรวจสอบเงื่อนไข จะถูกนำไปพิจารณาเพื่อเลือกว่าจะทำงานตามทางเลือกใด • รูปแบบ switch (expression ) { caseinteger-const-1:statement-1; break; caseinteger-const-2:statement-2; break; caseinteger-const-n:statement-n; break; : : default: statement; }
ตัวอย่าง #include<stdio.h> main() { charchoice; scanf(“%c”, &choice); switch (choice) { case ‘1’ : printf(“You get 1\n”); case ‘2’ : printf(“You get 2\n”); break; default : printf(“You get neither 1 nor 2\n”); }
การบ้านบทที่ 3 • จากส่วนของโปรแกรมต่อไปนี้ ข้อใดเป็นผลลัพธ์จากการทำงานของโปรแกรม 1) ByeGood Luck 2) HelloGood Luck 3) Hello4) Bye int a=2,b=5,c=a++*b++; if(c++ >= 10) printf(“Hello”); Else Printf(“Bye”); Printf(“Good Luck”);
int a; scanf(“%d”,&a); switch(a) { case 0: case 1: printf(“red”); case 2: printf(“blue”); case 3: printf(“green”); case 4: printf(“yell”); } 2. จากส่วนของโปรแกรมต่อไปนี้ จงหาว่าผลลัพธ์จะออกมาเป็นอย่างไร ถ้าป้อนค่าให้ตัวแปร a เป็น 2 1) red 2) blue 3) green 4) bluegreenyellow
3. จงเขียนโปรแกรมเพื่อคิดเกรดให้นักศึกษา โดยมีการรับค่าคะแนนเข้ามา (คะแนนต้องไม่เกิน 100 คะแนน) แล้วแสดงเกรดออกทางจอภาพ
4. จงเขียนโปรแกรมเพื่อตรวจสอบอักขระที่รับเข้ามาว่าเป็นสระ(vowel)(A,a,E,e,I,i,O,o,U,u) หรือพยัญชนะ (letter) พร้อมทั้งแสดงข้อความออกทางหน้าจอ