ตัวอย่างคำสั่งวนซ้ำ for

Download Report

Transcript ตัวอย่างคำสั่งวนซ้ำ for

C Programming
Lecture no. 5
Control
Statements
คำสั่งใน
กำรควบคุมกำรทำงำนของโปรแกรม
คำสั่ง
ในกำรควบคุมโปรแกรม
แบบเงื่อนไข
คำสั่ งเงือ่ นไข if...else
คำสั่ ง if / else
รูปแบบ
if <condition> statement1;
else statement2;
condition = เงื่อนไขในการตัดสิ นใจ
statement = คาสัง่ หรื อกลุ่มคาสัง่
ตัวอย่ ำง
Department of Computer Science
if (score>80) printf (“Excellent”);
else printf (“Good”);
3/32
310322 C Programming
Flow Chart ของคำสั่ งเงื่อนไข
Yes
Condition
Statement 2
Statement 1
Department of Computer Science
No
4/32
310322 C Programming
คำสั่ งเงื่อนไข if...else if
คำสั่ ง if / else หลำยทำงเลือก
รูปแบบ
if <condition1> statement1;
else if <condition2> statement2;
else statement3;
ตัวอย่ ำง
if (score>80) printf (“Very Good”);
else if (score>70) printf (“Good”);
else printf (“Standard”);
Department of Computer Science
5/32
310322 C Programming
คำสั่ งเงื่อนไข Switch
คำสั่ ง switch
รูปแบบ
switch(variable) {
case constant1: statement1;
break;
case constant2: statement2;
break; }
variable = ตัวแปร หรื อ นิพจน์
constant = ค่าคงที่มีชนิดข้อมูลเป็ น int หรื อ char ที่เป็ นตัวเลือกทางาน
Department of Computer Science
6/32
310322 C Programming
Flow Chart ของคำสั่ งแบบหลำยเงื่อนไข
Condition 1
No
Yes
Statement 1
Condition 2
No
Yes
Condition 3
Statement 2
Yes
Statement 3
Department of Computer Science
No
7/32
Statement 4
310322 C Programming
กำรใช้ เครื่องหมำยเปรียบเทียบในคำสั่ งเงื่อนไข
กำรใช้ เครื่องหมำยเปรียบเทียบกับเงื่อนไข
รูปแบบ
if <value1 operand value2>
statement1;
else if <value3 operand value4 logical operand
value5 operand value6>
statement2;
else statement3;
value = ค่ ำต้ องกำรตรวจสอบเงื่อนไข
operand = เครื่องหมำยตรวจสอบทำงคณิตศำสตร์
logical operand = เครื่องหมำยตรวจสอบทำงตรรกะ
Department of Computer Science
8/32
310322 C Programming
เครื่องหมำยเปรียบเทียบทีใ่ ช้ ในคำสั่ งเงื่อนไข
เครื่องหมำยเปรียบเทียบทำงตรรกะ (Logical Operand)
!
>,<
>= , <=
= = , !=
&&
||
Department of Computer Science
Not
มากกว่า , น้อยกว่า
มากกว่าหรื อเท่ากับ , น้อยกว่าหรื อเท่ากับ
เท่ากับ , ไม่เท่ากับ
And
Or
9/32
310322 C Programming
คำสั่ งเงื่อนไข
กำรใช้ เครื่องหมำยเปรียบเทียบกับเงื่อนไข
ตัวอย่ ำง
Department of Computer Science
if (score > 70)
printf (“Grade B”);
else ((score<=70) && (score >=60))
printf (“Grade C”);
else
printf (“Grade D”);
10/32
310322 C Programming
คำสั่ง
ในกำรควบคุมโปรแกรม
แบบ loop หรือ คำสั่งวนซำ้
1. คำสั่งลูป for
รู ปแบบทัว่ ไป
for ( นิพจน์ ที่ 1 ; นิพจน์ ที่ 2 ; นิพจน์ ที่ 3 )
{
คำสั่ งวนรอบ;
…….
}
เป็ นคำสั่ งทีใ่ ช้ ในกำรควบคุมให้ มกี ำรวนรอบคำสั่ งหลำย ๆ รอบ โดย
นิพจน์ ที 1 คือ กำรกำหนดค่ ำเริ่มต้ นให้ กบั ตัวแปรทีใ่ ช้ ในกำรวนรอบ
นิพจน์ ที่ 2 เป็ นกำรเปรียบเทียบ ก่อนที่จะวนรอบถ้ ำเงือ่ นไขของนิพจน์ เป็ นจริงจะมี
กำรทำงำนตำมคำสั่ งวนรอบ
นิพจน์ ที่ 3 เป็ นคำสั่ งในกำรกำหนดค่ ำที่จะเปลีย่ นแปลงไปในแต่ ละรอบ
Department of Computer Science
12/32
310322 C Programming
คำสั่ งวนซ้ำ for
รูปแบบเฉพำะ for ( initial; condition; increment (decrement) )
statement;
initial = ส่ วนกาหนดค่าเริ่ มต้น
condition = เงื่อนไขการตรวจสอบการทาซ้ า
increment (decrement) = นิพจน์ที่กาหนดการเพิ่มค่าหรื อลดค่าของตัวแปร
statement = กลุ่มคาสัง่ ที่ตอ้ งการทาซ้ า
Department of Computer Science
13/32
310322 C Programming
ตัวอย่ ำงคำสั่ งวนซ้ำ for
ตัวอย่ ำง
for (i=1; i<10; i++)
printf (“%d”,i);
หรือ
for (i=10; i>=1; i--)
printf (“%d”,i);
Department of Computer Science
14/32
310322 C Programming
Flow Chart ของคำสั่ งวนซ้ำ for
Initial
Condition
Yes
No
Statement
Increment (Decrement)
Department of Computer Science
15/32
310322 C Programming
2. คำสั่งลูป while
รูปแบบทัว่ ไป
while (นิพจน์ เงื่อนไข)
{
คำสั่ งทีว่ นลูป;
…………
………….
}
Department of Computer Science
compound statements
16/32
310322 C Programming
คำสั่ งวนซ้ำ while
รูปแบบเฉพำะ
while (condition)
statement;
condition = เงื่อนไขการตรวจสอบการทาซ้ า
statement = กลุ่มคาสัง่ ที่ตอ้ งการทาซ้ า
Department of Computer Science
17/32
310322 C Programming
ตัวอย่ ำงคำสั่ งวนซ้ำ while
ตัวอย่ ำง
Department of Computer Science
i=1;
while ( i<10 )
{
printf (“%d”,i);
i++;
}
18/32
310322 C Programming
Flow Chart ของคำสั่ งวนซ้ำ while
Initial
Condition
Yes
No
Statement
Increment (Decrement)
Department of Computer Science
19/32
310322 C Programming
3. คำสั่งวนรอบแบบทีต่ รวจสอบเงือ่ นไขทีหลัง: do... while
รูปแบบทั่วไป
do
statement;
while (นิพจน์ เงื่อนไข);
เช่ น
num = 2;
do
{
num++;
printf(“Now no is %d\n”,num);
} while (num == 10)
Department of Computer Science
20/32
310322 C Programming
คำสั่ งทำซ้ำ do...while
รูปแบบเฉพำะ do
{
statement;
}
while (condition);
condition = เงื่อนไขการตรวจสอบการทาซ้ า
statement = กลุ่มคาสัง่ ที่ตอ้ งการทาซ้ า
Department of Computer Science
21/32
310322 C Programming
ตัวอย่ ำงคำสั่ งทำซ้ำ do...while
ตัวอย่ ำง
i=1;
do
{
printf(“%d”,i);
i++;
}
while ( i<10 );
Department of Computer Science
22/32
310322 C Programming
Flow Chart ของคำสั่ งทำซ้ำ do...while
Initial
Statement
Increment (Decrement)
Condition
Yes
Department of Computer Science
23/32
No
310322 C Programming
คำสัง่ ควบคุมอื่น ๆ
break,continue, goto
และ labels
คำสั่ง break คำสั่ง continue
•คำสั่ง break
ใช้ เมื่อต้ องกำรให้ กำรทำงำนสำมำรถหลุดออกจำกลูปและกระโดด
ไปยังคำสั่ งทีอ่ ยู่นอกลูปทันที โดยไม่ ต้องตรวจสอบเงือ่ นไขใด ๆ
•คำสั่ง continue
ใช้ เมื่อต้ องกำรให้ กำรทำงำนนั้น ย้ อนกลับไปวนรอบใหม่ อกี ครั้ง
ซึ่งมีลกั ษณะทีต่ รงข้ ำมกับคำสั่ ง break
Department of Computer Science
25/32
310322 C Programming
คำสั่ ง break
รูปแบบ
ตัวอย่ ำง
Department of Computer Science
break;
for (i=1;i<10;i++)
{
if (i == 5 ) break;
printf (“%d”,i);
}
26/32
310322 C Programming
คำสั่ ง continue
รูปแบบ
ตัวอย่ ำง
Department of Computer Science
continue;
for (i=1;i<10;i++)
{
if (i = = 5 ) continue;
printf (“%d”,i);
}
27/32
310322 C Programming
คำสั่ง goto และ labels
คำสั่ ง goto ประกอบด้ วย 2 ส่ วน คือ
- ตัวคำสั่ ง goto เป็ นคำสั่ งให้ กระโดดไปยังตำแหน่ งทีก่ ำหนด
โดยจะกำหนดเป็ นชื่อ เรียกว่ ำ label name
- ชื่อ (label name) เป็ นตัวกำหนดตำแหน่ งทีค่ ำสั่ งจะกระโดด
ไปทำงำน
ข้ อควรระวัง !
คำสั่ งนีถ้ ือเป็ นคำสั่ งทีค่ วรหลีกเลีย่ งในกำรเขียนโปรแกรม
แต่ ถ้ำจำเป็ นหรือหลีกเลีย่ งไม่ ได้ เท่ ำนั้น จึงจะใช้ คำสั่ งนี้
Department of Computer Science
28/32
310322 C Programming
คำสั่ ง goto
รู ปแบบ
goto label;
statement1;
label : statement2 ;
label = ตาแหน่งที่ตอ้ งการไป
statement = กลุ่มของคาสัง่ ที่ตอ้ งการ
Department of Computer Science
29/32
310322 C Programming
ตัวอย่ ำงคำสั่ ง goto
ตัวอย่ ำง
Department of Computer Science
if (num == 5 ) goto end_work;
printf (“num not equa 5”);
end_work: printf (“num equa 5”);
30/32
310322 C Programming
ตัวอย่ ำงโปรแกรมทีใ่ ช้ คำสั่ ง goto
#include<stdio.h>
main()
{ int sum,n;
}
for(n=1;n<10;n++)
if (n==5)
goto part1;
else printf(“%d\n”,n);
part1 : printf(“Interrupt with no. 5\n”);
Department of Computer Science
31/32
310322 C Programming
END