งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Flow Control.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Flow Control."— ใบสำเนางานนำเสนอ:

1 Flow Control

2 ทบทวน โปรแกรม อย่างน้อยมีส่วนประกอบดังนี้ #include <stdio.h>
preprocessor void main() { int a, b, c; a = 10; b = 20; c = a + b; printf(“c = %d\n”, c); } int a, b, c; declaration a = 10; input b = 20; c = a + b; process printf(“c = %d\n”, c); output

3 ทบทวน โปรแกรม อย่างน้อยมีส่วนประกอบดังนี้ #include <stdio.h>
preprocessor void main() { } int a, b, c; declaration scanf(“%d”, &a); input scanf(“%d”, &b); c = a + b; process printf(“c = %d\n”, c); output

4 ต้องการคำนวณว่าพื้นที่ x ไร่ ปลูกมะม่วงได้กี่ต้น เมื่อมะม่วง 1 ต้น ใช้พื้นที่ในการปลูก 4 ตารางวา

5 ต้องการคำนวณว่าพื้นที่ x ไร่ ปลูกมะม่วงได้กี่ต้น เมื่อมะม่วง 1 ต้น ใช้พื้นที่ในการปลูก 4 ตารางวา
#include <stdio.h> void main() { int x, mango; x = 5; mango = x * 400 / 4; printf(“number of mango trees = %d\n”, mango); } preprocessor declaration input process output

6 ต้องการคำนวณว่าพื้นที่ x ไร่ y งาน มีกี่ตารางวา
#include <stdio.h> void main() { int x, mango; scanf(“%d”, &x); mango = x * 400 / 4; printf(“number of mango trees = %d\n”, mango); } preprocessor declaration input process output

7 Flow Control ลำดับการทำงานของคำสั่งในโปรแกรม จะทำงานจากบนลงล่าง เหมือนสายน้ำ ไหลจากที่สูงสู่ที่ต่ำ คำสั่งที่อยู่บน ทำงานก่อนคำสั่งที่อยู่ล่าง เมื่อทำงานคำสั่งสุดท้ายแล้ว จะจบโปรแกรม ถ้าต้องการให้ลำดับการทำงาน ไม่เรียงลงมาตามลำดับ ให้ใช้คำสั่ง if, switch, for, while, do – while

8 ตัวดำเนินการสัมพันธ์ (Relational Operator)
ให้ผลลัพธ์เป็น จริง หรือ เท็จ > greater than >= greater than or equal < less than <= less than or equal == equal != not equal

9 ตัวอย่าง ถ้าผลลัพธ์เป็นเท็จ ค่าที่ได้เท่ากับ 0
printf(“value of true = %d\n”, 5==5); พิมพ์ 1 printf(“value of false = %d\n”, 5 !=5); พิมพ์ 0 ถ้าผลลัพธ์เป็นเท็จ ค่าที่ได้เท่ากับ 0 ถ้าผลลัพธ์เป็นจริง ค่าที่ได้ ไม่เท่ากับ 0

10 ตัวอย่าง ถ้า i = 1, j = 2, k = 3 นิพจน์ การแปลความหมาย ผลลัพธ์
(j + k) > (i + 5) k != 3 j == 2 จริง เท็จ 1

11 Logical Operator (ตัวดำเนินการตรรก)
มีสามตัวคือ ! not && and || or

12 ตารางความจริง Op1 Op2 Op1 && Op2 Op1 || Op2 จริง เท็จ

13 ทดสอบ (5>3) && 3<2 6 != 6 || 7 != 7 2 <= 3 && 3 >= 2
!(2 > 3) || (3 > 4) (4>3) && (3 < 2) || (2 == 2) 6*2 < 2*9 && 3 == 4 5+10 == 15 && 2 == 1+1 1 1 1 1

14 if มีการใช้งาน 3 รูปแบบ if if – else nested if

15 if #include <stdio.h> void main() { int score; score = 82;
if (score > 50) printf(“You pass\n”); }

16 start score = 82 score > 50 ? n y You pass “You pass” stop

17 if #include <stdio.h> void main() { int score; score = 22;
if (score > 50) printf(“You pass\n”); }

18 start score = 22 score > 50 ? n y no output “You pass” stop

19 if #include <stdio.h> void main() { int score;
printf(“enter score ”); scanf(“%d”, &score); if (score > 50) printf(“You pass\n”); }

20 start input score score 75 n score >50 ? y You pass “You pass” stop

21 input score no output start score > 50 ? “You pass” stop score 36 n

22 if – else #include <stdio.h> void main() { int score;
printf(“enter score ”); scanf(“%d”, &score); if (score > 50) printf(“Pass\n”); else printf(“Fail\n”); }

23 start input score score 75 n score >50 ? y Pass “Fail” “Pass” stop

24 input score Fail start score > 50 ? “Fail” “You pass” stop score 36

25 คำสั่ง 1 – 3 ทำงานเมื่อเงื่อนไขเป็นจริง
บล็อกคำสั่ง บล็อกคำสั่ง ใช้เครื่องหมาย { } บล็อกคำสั่ง มีหน้าที่บอกขอบเขตของชุดคำสั่งที่อยู่ภายใต้การควบคุมของคำสั่งอื่น ตัวอย่าง if (เงื่อนไข) { คำสั่งที่ 1 คำสั่งที่ 2 คำสั่งที่ 3 } คำสั่ง 1 – 3 ทำงานเมื่อเงื่อนไขเป็นจริง

26 คำสั่งที่ 1 ทำงานเมื่อเงื่อนไขเป็นจริง
บล็อกคำสั่ง ถ้าไม่มีบล็อกคำสั่ง คำสั่งที่อยู่ภายใต้การควบคุมของคำสั่งอื่นจะมีเพียงคำสั่งเดียว ตัวอย่าง if (เงื่อนไข) คำสั่งที่ 1 คำสั่งที่ 2 คำสั่งที่ 3 คำสั่งที่ 1 ทำงานเมื่อเงื่อนไขเป็นจริง

27 บล็อกคำสั่ง if - else if (เงื่อนไข) { คำสั่งที่ 1 คำสั่งที่ 2 } else {
คำสั่งที่ 3 คำสั่งที่ 4 คำสั่งที่ 5 } ทำงานเมื่อเงื่อนไขเป็นจริง ทำงานเมื่อเงื่อนไขเป็นเท็จ

28 ตัวอย่าง #include <stdio.h> void main() { int x = 7, y = 5;
if (x%2 == 0) x++; y--; printf(“x = %d y = %d\n”, x, y); } x = 7 y = 4

29 ตัวอย่าง #include <stdio.h> void main() { int x = 8, y = 5;
if (x%2 == 0) x++; y--; printf(“x = %d y = %d\n”, x, y); } x = 9 y = 4

30 การย่อหน้าที่ถูกต้องจะช่วยให้อ่านโปรแกรมง่ายขึ้น
#include <stdio.h> void main() { int x = 7, y = 5; if (x%2 == 0) x++; printf(“x = %d y = %d\n”, x, y); } y--

31 #include <stdio.h>
void main() { int x = 7, y = 5; if (x%2 == 0) { x++; y--; }else { x--; y++; } printf(“x = %d y = %d\n”, x, y); x = 6 y = 6

32 #include <stdio.h>
void main() { int x = 10, y = 20; if (x%2 == 0) { x++; y--; } else { x--; y++; } printf(“x = %d y = %d\n”, x, y); x = 11 y = 19

33 nested if คำสั่ง if ที่นำมาซ้อนกัน เรียกว่า nested if รูปแบบ if () {
คำสั่ง… } else if () { คำสั่ง … }

34 #include <stdio.h>
void main() { int number; printf(“enter number : ”); scanf(“%d”, &number); if (number > 0) printf(“positive number\n”); else if (number < 0) printf(“negative number\n”); else printf(“zero\n”) } 10 positive number

35 #include <stdio.h>
void main() { int number; printf(“enter number : ”); scanf(“%d”, &number); if (number > 0) printf(“positive number\n”); else if (number < 0) printf(“negative number\n”); else printf(“zero\n”) } -2 negative number

36 #include <stdio.h>
void main() { int number; printf(“enter number : ”); scanf(“%d”, &number); if (number > 0) printf(“positive number\n”); else if (number < 0) printf(“negative number\n”); else printf(“zero\n”) } zero

37 2 Two #include <stdio.h> void main() { int number;
printf(“enter number : ”); scanf(“%d”, &number); if (number == 1) printf(“One\n”); else if (number == 2) printf(“Two\n”); else if (number == 3) printf(“Three\n”); else if (number == 4) printf(“Four\n”); else if (number == 5) printf(“Five\n”); else printf(“I don’t know\n”); } 2 Two

38 4 Four #include <stdio.h> void main() { int number;
printf(“enter number : ”); scanf(“%d”, &number); if (number == 1) printf(“One\n”); else if (number == 2) printf(“Two\n”); else if (number == 3) printf(“Three\n”); else if (number == 4) printf(“Four\n”); else if (number == 5) printf(“Five\n”); else printf(“I don’t know\n”); } 4 Four

39 I don’t know #include <stdio.h> void main() { int number;
printf(“enter number : ”); scanf(“%d”, &number); if (number == 1) printf(“One\n”); else if (number == 2) printf(“Two\n”); else if (number == 3) printf(“Three\n”); else if (number == 4) printf(“Four\n”); else if (number == 5) printf(“Five\n”); else printf(“I don’t know\n”); } I don’t know

40 6 I don’t know #include <stdio.h> void main() { int number;
printf(“enter number : ”); scanf(“%d”, &number); if (number == 1) printf(“One\n”); else if (number == 2) printf(“Two\n”); else if (number == 3) printf(“Three\n”); else if (number == 4) printf(“Four\n”); else if (number == 5) printf(“Five\n”); else printf(“I don’t know\n”); } 6 I don’t know

41 ไปห้อง lab กันเถอะ


ดาวน์โหลด ppt Flow Control.

งานนำเสนอที่คล้ายกัน


Ads by Google