34
C Programming Lecture no. 2 More on Control Statements

C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

C Programming

Lecture no. 2

More on Control Statements

Page 2: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 2

ค ำสงในกำรควบคมโปรแกรม

Page 3: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 3

ค ำสง loop หรอ ค ำสงวนซ ำ

Page 4: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 4

รปแบบ for ( นพจนท 1 ; นพจนท 2 ; นพจนท 3 ) { ค ำสงวนรอบ; ……. } เปนค ำสงทใชในกำรควบคมใหมกำรวนรอบค ำสงหลำย ๆ รอบ โดย นพจนท 1 คอ กำรก ำหนดคำเรมตนใหกบตวแปรทใชในกำรวนรอบ นพจนท 2 เปนกำรเปรยบเทยบ กอนทจะวนรอบถำเงอนไขของนพจนเปนจรงจะม กำรท ำงำนตำมค ำสงวนรอบ นพจนท 3 เปนค ำสงในกำรก ำหนดคำทจะเปลยนแปลงไปในแตละรอบ

1. ค ำสงลป for

Page 5: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 5

รปแบบ for ( initial; condition; increment (decrement) ) statement; initial = สวนก ำหนดคำเรมตน condition = เงอนไขกำรตรวจสอบกำรท ำซ ำ increment (decrement) = นพจนทก ำหนดกำรเพมคำหรอลดคำของตวแปร statement = กลมค ำสงทตองกำรท ำซ ำ

ค ำสง for

ค ำสงท ำซ ำ for

Page 6: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 6

ตวอยำง for (i=1; i<10; i++) printf (“%d\n”,i); หรอ for (i=10; i>=1; i--) printf (“%d\n”,i);

ค ำสงท ำซ ำ for

i = 1; for ( ; i <= 5 ; ) { printf("Loop i = %d\n", i++); } printf("\nLoop terminates at i = %d\n", i);

Page 7: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 7

Condition

Statement

Yes No

Initial

Increment (Decrement)

Flow Chart ของค ำสงท ำซ ำ for

Page 8: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 8

Page 9: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 9

Activity

#include <stdio.h> int main () { int i; /* counter variable */ int sum = 0; const int N = 10; /* limit value */ for (i = 1; i <= N; i++) { sum += i; } printf ("1 + 2 + ... + %d = %d\n", N, sum); return 0; }

Page 10: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 10

Activity

#include <stdio.h> int main () { int i; /* counter variable */ const int N = 10; /* limit value */ for (i = 1; i <= N; i++) { printf ("i = %d\n", i); i = N+1; } printf ("---------------\n", i); return 0; }

Page 11: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 11

Activity

#include <stdio.h> int main () { int i; /* counter variable */ int N = 10; /* limit value */ for (i = 1; i <= N; i++) { printf ("i = %d\n", i); N++; } printf ("---------------\n", i); return 0; }

Page 12: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 12

รปแบบ while (นพจนเงอนไข) {

ค ำสงทวนลป;

………… compound statements …………. }

2. ค ำสงลป while

Page 13: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 13

รปแบบ while (condition) statement; condition = เงอนไขกำรตรวจสอบกำรท ำซ ำ statement = กลมค ำสงทตองกำรท ำซ ำ

ค ำสง while

ค ำสงท ำซ ำ while

Page 14: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 14

ตวอยำง i=1; while ( i<10 ) { printf (“%d”,i); i++; }

ค ำสงท ำซ ำ while

Page 15: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 15

Condition

Statement

Yes No

Initial

Increment (Decrement)

Flow Chart ของค ำสงท ำซ ำ while

Page 16: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 16

Page 17: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 17

Activity

i = 0;

while (i)

{

printf ("While Loop\n");

}

Page 18: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 18

Activity

#include <stdio.h>

int main()

{

const int N = 10;

int i = 1, sum = 0;

printf("\t i \t sum\n");

while (i <= N) {

sum += i;

printf("\t %3d \t %4d\n", i, sum);

i++;

}

printf ("----------------------\n");

printf ("1 + 2 + ... + %d = %d\n", N, sum);

return 0; }

Page 19: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 19

Activity

#include <stdio.h>

int main()

{

const int N = 10;

int i = N, sum = 0;

printf ("\t i \t sum\n");

while (i > 0) {

sum += i;

printf("\t %3d \t %4d\n", i, sum);

i--;

}

printf ("----------------------\n");

printf ("%d + %d + ... + 1 = %d\n", N, N-1, sum);

return 0; }

Page 20: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 20

Activity

#include <stdio.h>

int main()

{

int ch;

int counter = 0;

while ((ch = getchar()) != 'Y') {

if (ch != 10) counter++;

}

printf("%d characters\n", counter);

return 0; }

Page 21: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 21

รปแบบ

do

statement; while (นพจนเงอนไข); เชน num = 2; do {

num++;

printf(“Now no is %d\n”,num); } while (num == 10)

3. ค ำสงวนรอบแบบทตรวจสอบเงอนไขทหลง : do... while

Page 22: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 22

ตวอยำง i=1; do { printf(“%d”,i); i++; } while ( i<10 );

ค ำสงท ำซ ำ do...while

Page 23: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 23

Flow Chart ของค ำสงท ำซ ำ do...while

Condition

Statement

Yes No

Initial

Increment (Decrement)

Page 24: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 24

Infinite Loop

for ( ; ; )

statement

while (1)

statement

do

statement

while (1);

Page 25: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 25

Nested Loop

#include <stdio.h> int main() { int i, j; int N = 9, function_calls = 0; for (i = 1; i <= N; i++) { for (j = 1; j <= i; j++) { printf ("%d", i); function_calls++; } printf ("\n", i); } printf ("Number of function calls : %d\n", function_calls); return 0; }

Page 26: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 26

ค ำสงควบคมอน ๆ break, continue, goto และ labels

Page 27: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 27

•ค ำสง break ใชเมอตองกำรใหกำรท ำงำนสำมำรถหลดออกจำกลปและกระโดดไปยงค ำสงทอยนอกลปทนท โดยไมตองตรวจสอบเงอนไขใด ๆ •ค ำสง continue ใชเมอตองกำรใหกำรท ำงำนนน ยอนกลบไปวนรอบใหมอกครง ซงมลกษณะทตรงขำมกบค ำสง break

ค ำสง break ค ำสง continue

Page 28: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 28

รปแบบ break; ตวอยำง for (i=1; i<10; i++) { if (i == 5 ) break; printf (“%d”,i); }

ค ำสง break

Page 29: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 29

รปแบบ continue; ตวอยำง for (i=1;i<10;i++) { if (i = = 5 ) continue; printf (“%d”,i); }

ค ำสง continue

Page 30: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 30

ค ำสง goto ประกอบดวย 2 สวน คอ - ตวค ำสง goto เปนค ำสงใหกระโดดไปยงต ำแหนงทก ำหนด โดยจะก ำหนดเปนชอ เรยกวำ label name - ชอ (label name) เปนตวก ำหนดต ำแหนงทค ำสงจะกระโดด ไปท ำงำน

ขอควรระวง ! ค ำสงนถอเปนค ำสงทควรหลกเลยงในกำรเขยนโปรแกรม แตถำจ ำเปนหรอหลกเลยงไมไดเทำนน จงจะใชค ำสงน

ค ำสง goto และ labels

Page 31: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 31

รปแบบ goto label; statement1; label : statement2 ; label = ต ำแหนงทตองกำรไป statement = กลมของค ำสงทตองกำร

ค ำสง goto

Page 32: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 32

ตวอยำง if (num == 5 ) goto end_work; printf (“num not equa 5”); end_work: printf (“num equa 5”);

ค ำสง goto

Page 33: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

Faculty of Informatics, BUU 885288 C Programming 33

#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”); }

ตวอยำงโปรแกรมทใชค ำสง goto

Page 34: C Programming - Burapha Universitykrisana/885288/... · 2016-09-11 · Faculty of Informatics, BUU 4 885288 C Programming รูปแบบ for ( นิพจน์ที่ 1 ;

END