8.02.2554

เนื้อหาจากการประชุมครั้งที่ 9






นอกจากคำสั่งควบคุมทิศทางการทำงานของโปรแกรมในแบบต่างๆ แล้ว ภาษาC ยังมีคำสั่งอีกกลุ่มหนึ่ง ซึ่งอาจจะไม่ได้ใช้สำหรับควบคุมทิศทางการทำงานของโปรแกรมโดยตรง แต่คำสั่งเหล่านี้มักจะถูกนำรวมไปใช้กับคำสั่งอื่นๆ เช่น นำไปเพื่อหยุดการเลือกทำ หรือออกจากการทำงานของระบบ เพราะฉะนั้น ในการประชุมครั้งนี้เราจะประชุมคำสั่งที่ใช้ควบคู่พร้อมกับ คำสั่ง loopในบางโปรแกรมในการประชุมนี้จะขอเสนอคำสั่ง2อย่างที่ใช้เป็นประจำคือ

1.คำสั่งbreak

2.คำสั่งcontinue

คำสั่ง break

-คำสั่ง break ถูกนำไปใช้รวมกับคำสั่งเลือกทำหรือคำสั่งวนรอบ เพื่อให้โปรแกรมหยุดการทำงานของคำสั่งแบบเลือกทำหรือวนรอบที่กำลังทำงานอยู่ ตัวอย่างการใช้งานคำสั่ง breakเช่น
โปรแกรมที่มีการใช้คำสั่งbreakซ้อนอยู่ในคำสั่งif
#include
int main ()
{
int count=0,num;
while (count <10)
{
printf("Enterr number :");
scanf("%d",&num);
if(num == 0)
{
printf("Wrong number\n");
break;
}
else
printf("Right number\n");
}
}

เมื่อสั่งรันโปรแกรม ให้ป้อนค่าหลายๆๆค่าจากนั้นให้ป้อนเลข 0 ซึ่งจะทำให้เงื่อนไขของคำสั่ง if เป็นจริง โปรแกรมจะแสดงข้อความ Wrong number แล้วออกจากโปรแกรมของำสั่ง if และลูป while ทันทีเนื่องจากคำสั่งbreak ดังนั้นจึงจะไม่มีข้อความ Enter new number

คำสั่ง continue
-คำสั่ง continue จะใช้ร่วมอยู่กับคำสั่ง loop เพื่อส่งให้โปรแกรมหยุดการทำงานในรอบปัจจุบันและกลับไปทำงานเริ่มรอบใหม่ ตัวอย่างcontinue เช่น

#include
int main ()
{
int count;
for(count=0;count<=100;count++)
{
if(count%10==0)
printf("%d\n",count);
else
{
continue;
printf("Guy Inw Za 555+");
}
}
}

เมื่อรันโปรแกรมนี้ ถ้าเงื่อนไขของคำสั่ง if เป็นเท็จ โปรแกรมจะทำตามคำสั่ง continue นั่นคือหยุดการทำงานในรอบปัจจุบันกลับไปทงานรอบใหม่ จึงทำให้ข้อความ Guy Inw za 55+ จะไม่แสดงขึ้นมาให้เห็นบนหน้าจอ

10 ความคิดเห็น:

  1. ไม่ระบุชื่อ2 สิงหาคม 2554 เวลา 00:33

    สุดยอดมากครับ

    ตอบลบ
  2. วีรศักดิ์ ดังชนกนันท์2 สิงหาคม 2554 เวลา 09:30

    #include
    int main(void)
    {
    int sum,i;
    i=1,sum=0;
    while(i<=10)
    {
    int score;
    printf("score %d : ",i);
    scanf("%d",&score);
    if(score<0)
    break;
    sum=sum+score;
    i++;
    }
    printf("Sum=%d",sum);
    }


    แสดงผล

    score 1 : 2
    score 2 : 5
    score 3 : 4
    score 4 : 1
    score 5 : 2
    score 6 : 5
    score 7 : -3
    Sum=19

    ตอบลบ
  3. ไม่ระบุชื่อ3 สิงหาคม 2554 เวลา 00:08

    โปรแกรมรับค่าตัวเลขจำนวนเต็มลบ ถ้าหากเจอเลขจำนวนเต็มบวกจะออกจากลูปและ ทำการแสดงผลรวมของเลขจำนวนเต้มลบที่ป้อนเข้ามา


    #include"stdio.h"
    int main()
    {
    int a,i,sum;
    i=1;
    sum=0;
    while(i>0)
    {
    printf("enter negative number : ");
    scanf("%d",&a);
    if(a<0)
    {
    i++;
    sum = sum + a;
    }
    else
    break;
    }
    printf("sum of negative number is %d ",sum);
    return 0;
    }



    แสดงผล
    enter negative number : -5
    enter negative number : -5
    enter negative number : 9
    sum of negative number is -10




    (หัสรา วัจนะรัตน์ 5401011620542)

    ตอบลบ
  4. #include

    void main ()
    {
    char sex;

    printf("are you m (male) of f (female)?");
    scanf ("%c",&sex);

    switch (sex)
    {case 'm' :printf("You are male\n");break;
    case 'f' :printf("You are female\n");break;
    default :printf("Try agai !!!\n");
    }
    return 0;
    }
    การทำงาน
    are you m (male) of f (female)? m
    You are male

    นายธน สุทธิธรรม 5401011630149

    ตอบลบ
  5. #include

    int main (void)
    {
    int a;
    while(1)
    {
    printf("*****MENU*****\n");
    printf("1)Group A : Uruguay\n");
    printf("2)Group D : Germany\n");
    printf("3)Group E : Netherlands\n");
    printf("4)Group H : Spain\n");
    printf("5)Exit\n");
    printf("Enter Number: ");
    scanf("%d",&a);
    if(a==1)
    printf("Uruguay VS Netherlands\n");
    else if(a==2)
    printf("Germany VS Spain\n");
    else if(a==3)
    printf("Netherlands VS Uruguay\n");
    else if(a==4)
    printf("Spain VS Germany");
    else if(a==5)
    {
    printf("Good bye\n");
    break;
    }
    else
    printf("Error\n");
    }
    }
    ผลทีไ่ด้คือ
    *****MENU*****
    1) Group A : Uruguay
    2) Group D : Germany
    3) Group E : Netherlands
    4) Group H : Spain
    5) Exit
    Enter Number : 4
    Spain VS Germany
    *****MENU*****
    1) Group A : Uruguay
    2) Group D : Germany
    3) Group E : Netherlands
    4) Group H : Spain
    5) Exit

    ตอบลบ
  6. #include

    int main (void)
    {
    int a;
    while(1)
    {
    printf("*****MENU*****\n");
    printf("1)Group A : Uruguay\n");
    printf("2)Group D : Germany\n");
    printf("3)Group E : Netherlands\n");
    printf("4)Group H : Spain\n");
    printf("5)Exit\n");
    printf("Enter Number: ");
    scanf("%d",&a);
    if(a==1)
    printf("Uruguay VS Netherlands\n");
    else if(a==2)
    printf("Germany VS Spain\n");
    else if(a==3)
    printf("Netherlands VS Uruguay\n");
    else if(a==4)
    printf("Spain VS Germany");
    else if(a==5)
    {
    printf("Good bye\n");
    break;
    }
    else
    printf("Error\n");
    }
    }
    ผลทีไ่ด้คือ
    *****MENU*****
    1) Group A : Uruguay
    2) Group D : Germany
    3) Group E : Netherlands
    4) Group H : Spain
    5) Exit
    Enter Number : 4
    Spain VS Germany
    *****MENU*****
    1) Group A : Uruguay
    2) Group D : Germany
    3) Group E : Netherlands
    4) Group H : Spain
    5) Exit
    (เบญจพล ศรีสันติธรรม 54-010116-2059-3)

    ตอบลบ
  7. #include

    int main (void)
    {
    int a;
    while(1)
    {
    printf("*****MENU*****\n");
    printf("1)Group A : Uruguay\n");
    printf("2)Group D : Germany\n");
    printf("3)Group E : Netherlands\n");
    printf("4)Group H : Spain\n");
    printf("5)Exit\n");
    printf("Enter Number: ");
    scanf("%d",&a);
    if(a==1)
    printf("Uruguay VS Netherlands\n");
    else if(a==2)
    printf("Germany VS Spain\n");
    else if(a==3)
    printf("Netherlands VS Uruguay\n");
    else if(a==4)
    printf("Spain VS Germany");
    else if(a==5)
    {
    printf("Good bye\n");
    break;
    }
    else
    printf("Error\n");
    }
    }
    ผลทีไ่ด้คือ
    *****MENU*****
    1) Group A : Uruguay
    2) Group D : Germany
    3) Group E : Netherlands
    4) Group H : Spain
    5) Exit
    Enter Number : 4
    Spain VS Germany
    *****MENU*****
    1) Group A : Uruguay
    2) Group D : Germany
    3) Group E : Netherlands
    4) Group H : Spain
    5) Exit
    (นาย เบญจพล ศรีสันติธรรม 54-010116-2059-3)

    ตอบลบ
  8. #include

    int main (void)
    {
    int a;
    while(1)
    {
    printf("*****MENU*****\n");
    printf("1)Group A : Uruguay\n");
    printf("2)Group D : Germany\n");
    printf("3)Group E : Netherlands\n");
    printf("4)Group H : Spain\n");
    printf("5)Exit\n");
    printf("Enter Number: ");
    scanf("%d",&a);
    if(a==1)
    printf("Uruguay VS Netherlands\n");
    else if(a==2)
    printf("Germany VS Spain\n");
    else if(a==3)
    printf("Netherlands VS Uruguay\n");
    else if(a==4)
    printf("Spain VS Germany");
    else if(a==5)
    {
    printf("Good bye\n");
    break;
    }
    else
    printf("Error\n");
    }
    }
    ผลทีไ่ด้คือ
    *****MENU*****
    1) Group A : Uruguay
    2) Group D : Germany
    3) Group E : Netherlands
    4) Group H : Spain
    5) Exit
    Enter Number : 4
    Spain VS Germany
    *****MENU*****
    1) Group A : Uruguay
    2) Group D : Germany
    3) Group E : Netherlands
    4) Group H : Spain
    5) Exit
    (นาย เบญจพล ศรีสันติธรรม 54-010116-2059-3)

    ตอบลบ
  9. #include
    int main (void)
    {
    int a,b;
    printf("Please enter number for search the true number between 1-10\n");
    b=0;
    while(b<100)
    {
    printf("Enter number =\t ");
    scanf("%d",&a);
    if(a==5)
    {
    printf("True\n");
    break;
    }
    else
    {
    printf("False");

    }
    printf("\n");
    }

    }
    ใส่ค่าระหว่าง 1-10 จนใส่ค่า5 ถึงจะหยุด
    ศราวุฒิ คำเมือง 54-010116-2049-6

    ตอบลบ
  10. #include"stdio.h"
    int main()
    {
    int a,i,sum;
    i=1;
    sum=0;
    while(i>0)
    {
    printf("enter negative number : ");
    scanf("%d",&a);
    if(a<0)
    {
    i++;
    sum = sum + a;
    }
    else
    break;
    }
    printf("sum of negative number is %d ",sum);
    return 0;
    }



    แสดงผล
    enter negative number : -5
    enter negative number : -5
    enter negative number : 9
    sum of negative number is -10
    (นายอาภากร กัณหา 54-010116-2062-3)

    ตอบลบ