การโปรแกรมภาษา C

36
1 กกกกกกกกกก กกกกกกกกกก กกกก กกกก C C

description

การโปรแกรมภาษา C. โปรแกรมแปลภาษา. การเขียนโปรแกรมคอมพิวเตอร์ทุกครั้ง จะต้องแปลงภาษาเหล่านั้นให้เป็นรหัสภาษาเครื่องที่คอมพิวเตอร์เข้าใจก่อนทุกครั้ง ก่อนที่คอมพิวเตอร์จะทำงานได้ การเขียนชุดคำสั่ง จะเรียกว่า โปรแกรมต้นฉบับ ( source program ) หรือ รหัสต้นฉบับ ( source code ) - PowerPoint PPT Presentation

Transcript of การโปรแกรมภาษา C

Page 1: การโปรแกรมภาษา  C

1

การโปรแกรมภาษา การโปรแกรมภาษา CC

Page 2: การโปรแกรมภาษา  C

2

โปรแกรมแปลภาษาโปรแกรมแปลภาษา การเขี ยนโปรแกรมคอมพิ�วเตอร�ทุ�กคร��ง จะต�องแปลงภาษาเหล!าน��น

ให�เป#นรห�สภาษาเคร%&องทุ &คอมพิ�วเตอร�เขี�าใจก!อนทุ�กคร��ง ก!อนทุ &คอมพิ�วเตอร�จะทุ'างานได้�

การเขี ยนชุ�ด้ค'าส�&ง จะเร ยกว!า โปรแกรมต้�นฉบั�บั (source program) หร%อ รหั�สต้�นฉบั�บั (source code)

ภาษาเคร%&องทุ &คอมพิ�วเตอร�ทุ'างานได้�เร ยกว!า executable program

โปรแกรมต�นฉบั�บัโปรแกรมต�นฉบั�บั รห�สภาษาเคร%&องรห�สภาษาเคร%&องInterpreterInterpreter

โปรแกรมต�นฉบั�บัโปรแกรมต�นฉบั�บั รห�สภาษาเคร%&องรห�สภาษาเคร%&องCompilerCompiler

แปลทุ ละบัรรทุ�ด้

แปลทุ��งโปรแกรม

Page 3: การโปรแกรมภาษา  C

3

ขั้��นต้อนการพั�ฒนาโปรแกรมขั้��นต้อนการพั�ฒนาโปรแกรม1 . การก'าหนด้และว�เคราะห�ป-ญหา (Problem

definition and problem analysis)2. การเขี ยนผั�งงานและซู1โด้โค�ด้ (pseudocoding)3. การเขี ยนโปรแกรม (programming)4. การทุด้สอบัและแก�ไขีโปรแกรม (program

testing and debuggin)5. การทุ'าเอกสารและบั'าร�งร�กษาโปรแกรม

(documentation and maintenance)ว�เคราะห�ป-ญหาว�เคราะห�ป-ญหา ซู1โด้โค�ด้ และผั�งงานซู1โด้โค�ด้ และผั�งงาน

การทุด้สอบัและแก�ไขีการทุด้สอบัและแก�ไขี

การเขี ยนโปรแกรมการเขี ยนโปรแกรม

การทุ'าเอกสาร และบั'าร�งร�กษาโปรแกรมการทุ'าเอกสาร และ

บั'าร�งร�กษาโปรแกรม

Page 4: การโปรแกรมภาษา  C

4

โครงสร�างโปรแกรมโครงสร�างโปรแกรมPreprocessor

directivesPreprocessor

directives

global declarationsglobal declarations

int main (void){

}

int main (void){

}

local definitions

statements

# include “stdio.h”

# include “conio.h”

int cola = 10 ;

main( )

{

int pepsi = 20 ;

total = cola + pepsi ;

printf (“This result is : %d”,total) ;

}

# include “stdio.h”

# include “conio.h”

int cola = 10 ;

main( )

{

int pepsi = 20 ;

total = cola + pepsi ;

printf (“This result is : %d”,total) ;

}

Page 5: การโปรแกรมภาษา  C

5

โปรแกรมภาษาซี�เบั��องต้�นโปรแกรมภาษาซี�เบั��องต้�น# include “stdio.h”main( ){

}

# include “stdio.h”main( ){

}

# include “stdio.h”main( ){

}

# include “stdio.h”main( ){

}

หัร�อ

# include “stdio.h”main( ){ printf (“COMPUTER”) ;}

# include “stdio.h”main( ){ printf (“COMPUTER”) ;}

# include “stdio.h”main( ){ printf (“COMPUTER”) ;}

# include “stdio.h”main( ){ printf (“COMPUTER”) ;}

หัร�อ

Page 6: การโปรแกรมภาษา  C

6

ประเภทขั้องขั้�อม!ล ประเภทขั้องขั้�อม!ล ((Data typeData type))ประเภท ค"าอธิ$บัาย ขั้นาดหัน'วยความ

จำ"าint เลขีจ'านวนเต2ม 2 byte

char อ�กขีระ 1 ต�ว 1 byte

float เลขีทุศน�ยม หร%อเลขีชุ �ก'าล�ง 4 byte

double

เลขีทุศน�ยมทุ &ม ความเทุ &ยง 2เทุ!า ( ซู4&งค%อ ม จ'านวนเลขีน�ย

ส'าค�ญมากกว!าและเลขีชุ �ก'าล�งม ขีนาด้

ใหญ!กว!า )

8 byte ขี�อม1ลประเภทุอาจจะม ค'าว!า short หร%อ long หร%อ unsigned ซู4&งหมายความว!า short int : จะใชุ�เน%�อทุ &หน!วยความจ'าน�อยกว!าหร%อเทุ!าก�บั int long int : จะใชุ�เน%�อทุ &หน!วยความจ'ามากกว!าหร%อเทุ!าก�บั int unsigned : จะเก2บัขี�อม1ลทุ &ไม!ม ค!าลบั เพิราะจะใชุ�ทุ�กๆบั�ตแทุนค!าต�วเลขี

ด้�งน��น unsigned int จะเก2บัขี�อม1ลเลขีบัวก ได้�มากกว!า int 2 เทุ!า

Page 7: การโปรแกรมภาษา  C

7

การประกาศต้�วแปรและค'าคงท�+การประกาศต้�วแปรและค'าคงท�+# include "stdio.h"const float taxrate = 0.07 ;float itemcost,salestax;main( ) { printf("Please enter cost of item: "); scanf("%f",&itemcost); salestax = taxrate * itemcost; printf("Item cost is = %f \n",itemcost); printf("Sales tax is %f\n",salestax); getch();}

# include "stdio.h"const float taxrate = 0.07 ;float itemcost,salestax;main( ) { printf("Please enter cost of item: "); scanf("%f",&itemcost); salestax = taxrate * itemcost; printf("Item cost is = %f \n",itemcost); printf("Sales tax is %f\n",salestax); getch();}

การประกาศต�วแปรการประกาศต�วแปรการประกาศค!าคงทุ &การประกาศค!าคงทุ &

Page 8: การโปรแกรมภาษา  C

8

ต้�วด"าเน$นการเลขั้คณิ$ต้ต้�วด"าเน$นการเลขั้คณิ$ต้ต้�วด"าเน$น

การ กระบัวนการ ต้�วอย'าง ผลล�พัธิ.+ บัวก 5+2 7- ลบั 5-2 3* ค1ณ 5*2 10/ หาร 5/2 2

%การหารแบับั Modulus 5%2 1

++การเพิ�&มค!าขี4�นหน4&ง

ค!าให� X = 5

++ X X = 6

- -การลด้ค!าลงหน4&ง

ค!าให� X = 5 -

- X X = 4

ล'าด้�บัการทุ'างานขีองต�วด้'าเน�นการ ค%อ ( ) , * , / , + , - เชุ!น 5 % 2 + 14 / 3 – 6 ผัลล�พิธ์�ค%อ –1 75. – 100. / 4.0 * (2 + 3) ผัลล�พิธ์�ค%อ –50.

Page 9: การโปรแกรมภาษา  C

9

ผลล�พัธิ.จำากการด"าเน$นการก�บัขั้�อม!ลผลล�พัธิ.จำากการด"าเน$นการก�บัขั้�อม!ลต้'างประเภทต้'างประเภท

#include "stdio.h"main(){ char aChar = 'A'; int intNum = 200; double fltNum = 245.3; printf("aChar contains: %c\n",aChar); printf("aChar numeric: %d\n",aChar); printf("intNum contains: %d\n",intNum); printf("fltNum contains: %f\n",fltNum); intNum = intNum + aChar; fltNum = fltNum + aChar; printf("\nAfter additions....\n");

#include "stdio.h"main(){ char aChar = 'A'; int intNum = 200; double fltNum = 245.3; printf("aChar contains: %c\n",aChar); printf("aChar numeric: %d\n",aChar); printf("intNum contains: %d\n",intNum); printf("fltNum contains: %f\n",fltNum); intNum = intNum + aChar; fltNum = fltNum + aChar; printf("\nAfter additions....\n");

printf("aChar numaric: %d\n",aChar); printf("intNum contains: %d\n",intNum); printf("fltNum contains: %f\n",fltNum);}

printf("aChar numaric: %d\n",aChar); printf("intNum contains: %d\n",intNum); printf("fltNum contains: %f\n",fltNum);}

aChar contains : AaChar numeric : 65intNum contains : 200

After additions…aChar numeric : 65intNum contains : 265fltNum contains : 310.300000

aChar contains : AaChar numeric : 65intNum contains : 200

After additions…aChar numeric : 65intNum contains : 265fltNum contains : 310.300000

ผัลล�พิธ์� ค%อ

Page 10: การโปรแกรมภาษา  C

10

การพั$มพั.ขั้�อม!ลเบั��องต้�น การพั$มพั.ขั้�อม!ลเบั��องต้�น : : ฟั0งก.ชั�น ฟั0งก.ชั�น printf ( )printf ( )

ฟั-งก�ชุ�น printf( ) เป#นฟั-งก�ชุ�นทุ &ใชุ�พิ�มพิ�ขี�อความต!างๆออกทุางจอภาพิ ม ล�กษณะโครงสร�างด้�งน �

control string : เป#นส!วนทุ &ใชุ�แสด้งขี�อความออกทุางจอภาพิ

variable list : เป#นส!วนขีองค!าคงทุ & หร%อต�วแปรทุ &จะให�แสด้งผัล

printf (“control string”,variable list,…………..) ;printf (“control string”,variable list,…………..) ;

# include “stdio.h”main( ){ int fanta = 22 ; printf (“Value is : %d and %d \n”,fanta,500) ;}

# include “stdio.h”main( ){ int fanta = 22 ; printf (“Value is : %d and %d \n”,fanta,500) ;}

Page 11: การโปรแกรมภาษา  C

11

การพั$มพั.ขั้�อม!ลเบั��องต้�น การพั$มพั.ขั้�อม!ลเบั��องต้�น : : ฟั0งก.ชั�น ฟั0งก.ชั�น printf ( )printf ( )

รหั�ส ผลท�+ได�\n ให�ขี4�นบัรรทุ�ด้ใหม!\t ให�เว�น tab เป#นระยะ 8 ชุ!อง

\xhh ใส!ต�วอ�กษร hh เม%&อเป#นเลขีฐานส�บัหก\a ส!งเส ยงบั�;ป\\ เคร%&องหมาย backslash

\” เคร%&องหมาย quotation mark

printf (“Computer\n”); printf (“\\Computer”); printf (“\nComputer”); printf (“\aComputer\n”); printf (“\nComputer\n”); printf (“\tComputer”);

printf (“Computer\n”); printf (“\\Computer”); printf (“\nComputer”); printf (“\aComputer\n”); printf (“\nComputer\n”); printf (“\tComputer”);

Page 12: การโปรแกรมภาษา  C

12

การพั$มพั.ขั้�อม!ลเบั��องต้�น การพั$มพั.ขั้�อม!ลเบั��องต้�น : : ฟั0งก.ชั�น ฟั0งก.ชั�น printf ( )printf ( )

เคร�+องหัมาย

การใชั�งาน

%d พิ�มพิ�เลขีฐานส�บั%u พิ�มพิ�เลขีไม!ม เคร%&องหมาย%f พิ�มพิ�เลขีทุศน�ยม%e พิ�มพิ�เลขีจ'านวนจร�งในร1ปเลขียกก'าล�ง%c พิ�มพิ�ต�วอ�กษรต�วเด้ ยว (char)

%s พิ�มพิ�ชุ�ด้ต�วอ�กษร(String)

% % พิ�มพิ�เคร%&องหมาย %%o พิ�มพิ�เลขีฐานแปด้%x พิ�มพิ�เลขีฐานส�บัหก

Page 13: การโปรแกรมภาษา  C

13

การพั$มพั.ขั้�อม!ลเบั��องต้�น การพั$มพั.ขั้�อม!ลเบั��องต้�น : : ฟั0งก.ชั�น ฟั0งก.ชั�น printf ( )printf ( )

printf (“%s %d %f %c \n”,”Sam”,14,-8.76,’X’) ;printf (“%s %d %f %c \n”,”Sam”,14,-8.76,’X’) ;Sam 14 –8.760000 XSam 14 –8.760000 X

printf (“%f %.3f %.2f %.1f”,4.5678,4.5678,4.5678,4.5678) ;printf (“%f %.3f %.2f %.1f”,4.5678,4.5678,4.5678,4.5678) ;4.567800 4.568 4.57 4.64.567800 4.568 4.57 4.6

# include “stdio.h”main( ){ int x,y; x = 5; y = 6; printf (“%d\n”,x); printf (“%c\n”,x); printf (“%d %d\n”,x,y);

# include “stdio.h”main( ){ int x,y; x = 5; y = 6; printf (“%d\n”,x); printf (“%c\n”,x); printf (“%d %d\n”,x,y);

printf (“%d\n”,125); printf (“%c\n”,125); printf (“The total is $ %6.2f\n”,12.5); printf (“The total is $ %6.3f\n”,12.5);}

printf (“%d\n”,125); printf (“%c\n”,125); printf (“The total is $ %6.2f\n”,12.5); printf (“The total is $ %6.3f\n”,12.5);}

Page 14: การโปรแกรมภาษา  C

14

การร�บัขั้�อม!ลเบั��องต้�น การร�บัขั้�อม!ลเบั��องต้�น : : ฟั0งก.ชั�น ฟั0งก.ชั�น scanf ( )scanf ( )

ฟั-งก�ชุ�น scanf( ) เป#นฟั-งก�ชุ�นทุ &ใชุ�ในการร�บัขี�อม1ลจากการกด้ค ย�บัอร�ด้ทุ &อย1!ในร1ปรห�ส ASCII ไปเก2บัไว�ในต�วแปรทุ &ก'าหนด้ และสามารถระบั�ชุน�ด้ขี�อม1ลทุ &เก2บัได้�

control string : เป#นการก'าหนด้ชุน�ด้ขีองขี�อม1ลทุ &จะเก2บัในต�วแปร

&variable list : เป#นการชุ �ไปทุ &แอด้เด้รสหน!วยความจ'าขีองต�วแปร

scanf (“control string”,&variable list,…………..) ;scanf (“control string”,&variable list,…………..) ;

# include “stdio.h”main( ){ int num ; scanf (“%d”,&num) ;}

# include “stdio.h”main( ){ int num ; scanf (“%d”,&num) ;}

Page 15: การโปรแกรมภาษา  C

15

การร�บัขั้�อม!ลเบั��องต้�น การร�บัขั้�อม!ลเบั��องต้�น : : ฟั0งก.ชั�น ฟั0งก.ชั�น scanf ( )scanf ( )

ฟั-งก�ชุ�น scanf( ) สามารถระบั�ประเภทุขีองขี�อม1ลทุ &จะร�บัเขี�าได้�อ กด้�วย เชุ!น

เม%&อ run โปรแกรม และทุด้ลองป=อนค!า abcdtye และกด้ Enterผัลล�พิธ์�ทุ &ได้�จะเป#น abcd เทุ!าน��น ส!วน tye จะไม!ถ1กจ�ด้เก2บัใน

ต�วแปร str เน%&องจาก ม การระบั�ว!าให�ร�บัเพิ ยงแค! abcd เทุ!าน��น

# include “stdio.h”main( ){ int i ; char str[80] ; scanf (“%[abcdefg]”,str) ;}

# include “stdio.h”main( ){ int i ; char str[80] ; scanf (“%[abcdefg]”,str) ;}

Page 16: การโปรแกรมภาษา  C

16

ต้�วอย'างโปรแกรมอ$นเทอร.แอกท�ฟัต้�วอย'างโปรแกรมอ$นเทอร.แอกท�ฟั# include “stdio.h”main( ){

int feet,inches;printf(“Enter number of feet “);scanf(“%d”,&feet);inches = feet * 12;printf(“%d inches”,inches);

}

# include “stdio.h”main( ){

int feet,inches;printf(“Enter number of feet “);scanf(“%d”,&feet);inches = feet * 12;printf(“%d inches”,inches);

}

Enter number of feet 560 inchesEnter number of feet 560 inches

ผัลร�นขีองโปรแกรมค%อfeet

inches

5

60

Page 17: การโปรแกรมภาษา  C

17

ฟั0งก.ชั�นอ�+นๆท�+ใชั�ในการร�บัขั้�อม!ลเขั้�าฟั0งก.ชั�นอ�+นๆท�+ใชั�ในการร�บัขั้�อม!ลเขั้�า getchar( ) : ใชุ�ส'าหร�บัป=อนต�วอ�กษร 1 ต�วผั!านทุางแป=นพิ�มพิ�และแสด้ง

ต�วอ�กษรบันจอภาพิ พิร�อมทุ��งต�องกด้ Enter เพิ%&อทุ'างานต!อ เชุ!น ch = getchar( );

getche( ) : ใชุ�ส'าหร�บัป=อนต�วอ�กษรผั!านทุางแป=นพิ�มพิ�และแสด้งต�วอ�กษรบันจอภาพิ แต!เม%&อป=อนขี�อม1ลแล�วไม!ต�องกด้ Enter เชุ!น ch = getche( );

getch( ) : ใชุ�ส'าหร�บัป=อนต�วอ�กษรผั!านทุางแป=นพิ�มพิ�และไม!แสด้งต�วอ�กษรบันจอภาพิ แต!เม%&อป=อนขี�อม1ลแล�วไม!ต�องกด้ Enter เชุ!น ch = getch( );

gets( ) : ใชุ�ส'าหร�บัขี�อม1ลชุน�ด้ String ซู4&งป=อนทุางแป=นพิ�มพิ�โด้ยม ร1ปแบับัด้�งน � gets(str); เม%&อโปรแกรมทุ'างานถ4งค'าส� &งน �จะหย�ด้เพิ%&อให�ป=อนขี�อความ เม%&อป=อนเสร2จแล�วกด้

Enter ขี�อความทุ��งหมด้จะถ1กเก2บัไว�ในอาร�เรย�สตร�งก� str

Page 18: การโปรแกรมภาษา  C

18

การใส'หัมายเหัต้5การใส'หัมายเหัต้5 ((CommentComment)) หมายเหต� (Comment) : เป#นขี�อความทุ &ใส!ไว�ตาม

ส!วนต!าง ๆ ขีองโปรแกรม เพิ%&ออธ์�บัายหร%อบั�นทุ4กขี�อความให�ผั1�พิ�ฒนาโปรแกรมม ความเขี�าใจในส!วนต!าง ๆ ขีองโปรแกรม โด้ยม ว�ธ์ การใส!หมายเหต�ด้�งน �

1. // ใชุ�ก'าหนด้บัรรทุ�ด้ทุ &จะเป#นหมายเหต�ทุ��งบัรรทุ�ด้ เชุ!น// This is my Program

2. /*…*/ ใชุ�ก'าหนด้หมายเหต�เป#นชุ!วง ๆ โด้ยหมายเหต�จะอย1!ระหว!างเคร%&องหมาย /* ก�บั */ เชุ!น

/* This is my Program */

Page 19: การโปรแกรมภาษา  C

19

การใส'หัมายเหัต้5 การใส'หัมายเหัต้5 ((CommentComment))

// เป#นโปรแกรมทุ &ใชุ�ในการค'านวณหาหน!วยทุ &เป#นน��ว# include “stdio.h”main( ){

int feet,inches;printf(“Enter number of feet “); // ส�&งให�แสด้งขี�อความออกทุางจอภาพิscanf(“%d”,&feet); /* ส��งให�โปรแกรมร�บัค!าจากผั1�ใชุ� * /inches = feet * 12;printf(“%d inches”,inches);

}/* ต�วอย!างน �เป#นว�ธ์ การใส!หมายเหต�ให�ก�บัโปรแกรม * /

// เป#นโปรแกรมทุ &ใชุ�ในการค'านวณหาหน!วยทุ &เป#นน��ว# include “stdio.h”main( ){

int feet,inches;printf(“Enter number of feet “); // ส�&งให�แสด้งขี�อความออกทุางจอภาพิscanf(“%d”,&feet); /* ส��งให�โปรแกรมร�บัค!าจากผั1�ใชุ� * /inches = feet * 12;printf(“%d inches”,inches);

}/* ต�วอย!างน �เป#นว�ธ์ การใส!หมายเหต�ให�ก�บัโปรแกรม * /

Page 20: การโปรแกรมภาษา  C

20

การเล�อกท"าการเล�อกท"า ขี�อม1ลทุางลอจ�ก (logical data ) จะม อย1! 2 ค!าค%อ ค!าทุ &เป#นจร�ง (true) และ

เป#นเทุ2จ(false) ในภาษาซู จะใชุ� 1 แทุนค!าทุ &เป#น จำร$ง และ 0 แทุนค!าทุ &เป#น เท6จำ

ต้�วด"าเน$นการ

การกระท"า หัร�อกระบัวนการ

&& And : ถ�าค!าทุ��งสองเป#นจร�งผัลล�พิธ์�จะเป#นจร�ง

|| Or : ถ�าค!าทุ��งสองเป#นเทุ2จผัลล�พิธ์�จะเป#นเทุ2จ

! Not : เปล &ยนค!าจากจร�งเป#นเทุ2จ จากเทุ2จเป#นจร�ง

= = เทุ!าก�บั!= ไม!เทุ!าก�บั<= น�อยกว!าหร%อเทุ!าก�บั>= มากกว!าหร%อเทุ!าก�บั> มากกว!า< น�อยกว!า

Page 21: การโปรแกรมภาษา  C

21

การเล�อกท"าการเล�อกท"า# include "stdio.h"int main(void){ int a = 5; int b = -3; int c = 0; printf(“ %2d && %2d is %2d\n”,a,b, a &&b ); printf(“ %2d && %2d is %2d\n”,a,c, a &&c ); printf(“ %2d && %2d is %2d\n”,c,a, c && a); printf(“ %2d || %2d is %2d\n”,a,c, a || c); printf(“ %2d || %2d is %2d\n”,c,a, c || a); printf(“ %2d || %2d is %2d\n”,c,c, c || c); printf(“!%2d && !%2d is %2d\n”a,c, !a && !c); printf(“!%2d && !%2d is %2d\n”a,c, !a && c);

# include "stdio.h"int main(void){ int a = 5; int b = -3; int c = 0; printf(“ %2d && %2d is %2d\n”,a,b, a &&b ); printf(“ %2d && %2d is %2d\n”,a,c, a &&c ); printf(“ %2d && %2d is %2d\n”,c,a, c && a); printf(“ %2d || %2d is %2d\n”,a,c, a || c); printf(“ %2d || %2d is %2d\n”,c,a, c || a); printf(“ %2d || %2d is %2d\n”,c,c, c || c); printf(“!%2d && !%2d is %2d\n”a,c, !a && !c); printf(“!%2d && !%2d is %2d\n”a,c, !a && c);

printf(“%2d && !%2d is %2d\n”,a,c, a && !c);return 0;}

printf(“%2d && !%2d is %2d\n”,a,c, a && !c);return 0;}

ผัลล�พิธ์� ค%อ 5 && -3 is 1 5 && 0 is 0 0 && 5 is 0 5 | | 0 is 1 0 | | 5 is 1 0 | | 0 is 0!5 && !0 is 0!5 && 0 is 0 5 && !0 is 1

5 && -3 is 1 5 && 0 is 0 0 && 5 is 0 5 | | 0 is 1 0 | | 5 is 1 0 | | 0 is 0!5 && !0 is 0!5 && 0 is 0 5 && !0 is 1

Page 22: การโปรแกรมภาษา  C

22

การเล�อกท"าแบับัทางเด�ยว การเล�อกท"าแบับัทางเด�ยว ((ifif))if (condition) {…action statement….}if (condition) {…action statement….}

เง%&อนไขีเง%&อนไขี

สเตตเมนด้�สเตตเมนด้�จร�ง

เทุ2จ

if (age >=18) printf(“of age\n”);printf(“good luck”);if (age >=18) printf(“of age\n”);printf(“good luck”);

if (age >=18) { printf(“of age\n”); }printf(“good luck”);if (age >=18) { printf(“of age\n”); }printf(“good luck”);

of agegood luckof agegood luck

good luckgood luckof agegood luckof agegood luck

ต้�วอย'าง

ผลล�พัธิ.age = 25 age = 14 age = 18

Page 23: การโปรแกรมภาษา  C

23

การเล�อกท"าแบับัทางเด�ยว การเล�อกท"าแบับัทางเด�ยว ((ifif))

#include “stdio.h”main( ){ int magic = 123; //ก'าหนด้ค!า123ให�ต�วแปร int guess; printf(“Enter your guess : “); scanf(“%d”,&guess); if(guess == magic) printf(“*** Right ***”);}

#include “stdio.h”main( ){ int magic = 123; //ก'าหนด้ค!า123ให�ต�วแปร int guess; printf(“Enter your guess : “); scanf(“%d”,&guess); if(guess == magic) printf(“*** Right ***”);}

#include “stdio.h”#include “conio.h”main( ){ int answer; clrscr( ); printf(“What is 3+4 = “); scanf(“%d”,&answer); if(answer == 3+4) printf(“OK”); printf(“Error”); getch( );}

#include “stdio.h”#include “conio.h”main( ){ int answer; clrscr( ); printf(“What is 3+4 = “); scanf(“%d”,&answer); if(answer == 3+4) printf(“OK”); printf(“Error”); getch( );}

โปรแกรมทุ & 42. โปรแกรมทุ & 43

Page 24: การโปรแกรมภาษา  C

24

การเล�อกท"าแบับัสองท$ศทาง การเล�อกท"าแบับัสองท$ศทาง ((ifif--elseelse))

if (condition) {….statement1….}else {….statement 2….}

if (condition) {….statement1….}else {….statement 2….}

เง%&อนไขีเง%&อนไขี

สเตตเมนด้� 1สเตตเมนด้� 1จร�ง เทุ2จ

if (score >= 60) printf(“You pass”);else printf (“Your fail”);printf(“Have a nice day”);

if (score >= 60) printf(“You pass”);else printf (“Your fail”);printf(“Have a nice day”);

You failHave a nice dayYou failHave a nice day

You passHave a nice dayYou passHave a nice day

ต้�วอย'าง

ผลล�พัธิ.score = 54 score = 73

สเตตเมนด้� 2สเตตเมนด้� 2

if (score >= 60) { printf(“You pass”);} else { printf (“Your fail”);}printf(“Have a nice day”);

if (score >= 60) { printf(“You pass”);} else { printf (“Your fail”);}printf(“Have a nice day”);

Page 25: การโปรแกรมภาษา  C

25

การเล�อกท"าแบับัสองท$ศทาง การเล�อกท"าแบับัสองท$ศทาง ((ifif--elseelse))

#include “stdio.h”#include “conio.h”main( ){ int num; clrscr( ); printf(“enter a number”); scanf(“%d”,&num); if (num<0) printf(“number is negative”); else printf(“number is positive”); getch( );}

#include “stdio.h”#include “conio.h”main( ){ int num; clrscr( ); printf(“enter a number”); scanf(“%d”,&num); if (num<0) printf(“number is negative”); else printf(“number is positive”); getch( );}

#include “stdio.h”int Number;float cost;main( ){ printf(“Enter number …..”); scanf(“%d”,&Number); if (Number >= 10) cost = Number * 6.5; else cost = Number * 7; printf(“Cost = %2.2f\n”,cost);}

#include “stdio.h”int Number;float cost;main( ){ printf(“Enter number …..”); scanf(“%d”,&Number); if (Number >= 10) cost = Number * 6.5; else cost = Number * 7; printf(“Cost = %2.2f\n”,cost);}

โปรแกรมทุ & 45 โปรแกรมทุ & 46

Page 26: การโปรแกรมภาษา  C

26

การใชั�ค"าส�+ง การใชั�ค"าส�+ง if if หัลาย ๆค"าส�+งซี�อนหัลาย ๆค"าส�+งซี�อนก�นก�น((nested if statementnested if statement))

Expression 1Expression 1

Expression 2Expression 2Statement 3Statement 3

Statement 1Statement 1Statement 2Statement 2

if (expression 1)

if (expression 2)

statement 1;

else

statement 2;

else

statement 3;

if (expression 1)

if (expression 2)

statement 1;

else

statement 2;

else

statement 3;

Page 27: การโปรแกรมภาษา  C

27

การใชั�ค"าส�+ง การใชั�ค"าส�+ง if if หัลาย ๆค"าส�+งซี�อนหัลาย ๆค"าส�+งซี�อนก�นก�น((nested if statementnested if statement))

#include “stdio.h”main( ){ int a,b; printf(“Please enter two integer : “); scanf(“%d%d”,&a,&b); if(a<=b) if(a<b) printf(“%d < %d\n”,a,b); else printf(“%d == %d\n”,a,b); else printf(“%d > %d\n”,a,b);}

#include “stdio.h”main( ){ int a,b; printf(“Please enter two integer : “); scanf(“%d%d”,&a,&b); if(a<=b) if(a<b) printf(“%d < %d\n”,a,b); else printf(“%d == %d\n”,a,b); else printf(“%d > %d\n”,a,b);}

โปรแกรมทุ & 412.#include “stdio.h”int x;main( ){ clrscr( ); printf(“Enter Score(0..100) :”); scanf(“%d”,&x); if (x >= 90) printf(“EXCELLENT”); else if(x >= 80) printf(“Good ”); else if(x >= 70) printf(“FAIR ”); else printf(“FAIL “);}

#include “stdio.h”int x;main( ){ clrscr( ); printf(“Enter Score(0..100) :”); scanf(“%d”,&x); if (x >= 90) printf(“EXCELLENT”); else if(x >= 80) printf(“Good ”); else if(x >= 70) printf(“FAIR ”); else printf(“FAIL “);}

โปรแกรมทุ & 413.

Page 28: การโปรแกรมภาษา  C

28

การเล�อกท"าแบับัหัลายทางการเล�อกท"าแบับัหัลายทาง (switch)(switch)

switch (expression){ case constant1 : statement sequence

break; case constant2 : statement sequence

break; case constant3 : statement sequence

break; .......... .......... [ default :

statement sequence ]}

switch (expression){ case constant1 : statement sequence

break; case constant2 : statement sequence

break; case constant3 : statement sequence

break; .......... .......... [ default :

statement sequence ]}

ค'าส�&ง switch น �จะกระทุ'าค!าใน expression ถ�าผัลล�พิธ์�ทุ &ได้�เทุ!าก�บัค!าคงทุ &ค!าใด้หล�ง case โปรแกรมก2จะไปทุ'า statement sequence หล�งค!าคงทุ &น� �น ถ�าไม!เทุ!าก�บัค!าใด้เลยโปรแกรมจะไปทุ'า statment sequence หล�ง default

ค!าคงทุ &หล�ง case จะต�องเป#น int หร%อ char เทุ!าน��น

constant 1constant 1 ชุ�ด้ค'าส�&ง 1ชุ�ด้ค'าส�&ง 1

constant 2constant 2

constant 3constant 3

ชุ�ด้ค'าส�&ง 2ชุ�ด้ค'าส�&ง 2

ชุ�ด้ค'าส�&ง 3ชุ�ด้ค'าส�&ง 3

defaultdefault

จร�ง

จร�ง

จร�ง

Page 29: การโปรแกรมภาษา  C

29

การเล�อกท"าแบับัหัลายทาง การเล�อกท"าแบับัหัลายทาง ((switchswitch))

#include “stdio.h”#include “conio.h”main( ){ int i; clrscr( ); printf(“Enter a number between 1 and 4: “); scanf(“%d”,&i); switch(i) { case 1 : printf(“one”); break; case 2 : printf(“two”); break; case 3 : printf(“three”); break; case 4 : printf(“four”); break; default:

printf(“unrecognized number”); } getch( );}

#include “stdio.h”#include “conio.h”main( ){ int i; clrscr( ); printf(“Enter a number between 1 and 4: “); scanf(“%d”,&i); switch(i) { case 1 : printf(“one”); break; case 2 : printf(“two”); break; case 3 : printf(“three”); break; case 4 : printf(“four”); break; default:

printf(“unrecognized number”); } getch( );}

โปรแกรมทุ & 415.#include “stdio.h”#include “conio.h”main( ){ char ch; clrscr(); printf(“Enter the letter: “); ch = getche( ); switch(ch) {

case ‘a’ :case ‘e’ :case ‘i’ :case ‘o’ :case ‘u’ :

printf(“is a vowel\n”);break;

default :printf(“is a consonant”);

} getch();}

#include “stdio.h”#include “conio.h”main( ){ char ch; clrscr(); printf(“Enter the letter: “); ch = getche( ); switch(ch) {

case ‘a’ :case ‘e’ :case ‘i’ :case ‘o’ :case ‘u’ :

printf(“is a vowel\n”);break;

default :printf(“is a consonant”);

} getch();}

โปรแกรมทุ & 416.

Page 30: การโปรแกรมภาษา  C

30

การท"าซี"�า การท"าซี"�า (for)(for)for (expr1;expr2;expr3) { <statement>;}

expr1 : เป#นส!วนทุ &ใชุ�ในการก'าหนด้ค!าเร�&มต�นexpr2 : เป#นส!วนทุ &ใชุ�ในการตรวจสอบัเง%&อนไขีexpr3 : เป#นส!วนทุ &ใชุ�ในการเพิ�&มค!าหร%อลด้ค!าให�ก�บัต�วแปรเร�&มต�น

for (expr1;expr2;expr3) { <statement>;}

expr1 : เป#นส!วนทุ &ใชุ�ในการก'าหนด้ค!าเร�&มต�นexpr2 : เป#นส!วนทุ &ใชุ�ในการตรวจสอบัเง%&อนไขีexpr3 : เป#นส!วนทุ &ใชุ�ในการเพิ�&มค!าหร%อลด้ค!าให�ก�บัต�วแปรเร�&มต�น

for (counter=1 ; counter <= 10 ; counter++){ printf(“%d”,counter);}

for (counter=1 ; counter <= 10 ; counter++){ printf(“%d”,counter);}

counter = 1counter = 1

counter <= 10counter <= 10printf(“%d”,counter);printf(“%d”,counter);

counter ++counter ++

จร�ง

เทุ2จ

Page 31: การโปรแกรมภาษา  C

31

การท"าซี"�า การท"าซี"�า (for)(for)

#include “stdio.h”int i;main( ){ for (i = 1 ; i <= 5 ; i++ )

printf(“number %d\n”,i);}

#include “stdio.h”int i;main( ){ for (i = 1 ; i <= 5 ; i++ )

printf(“number %d\n”,i);}

โปรแกรมทุ & 51.#include “stdio.h”#include “conio.h”int x,y;main( ){ clrscr( ); for (i=1 ; i<= 12 ; i++)

printf(“ 2 x %d = %d\n”,i,2*i);}

#include “stdio.h”#include “conio.h”int x,y;main( ){ clrscr( ); for (i=1 ; i<= 12 ; i++)

printf(“ 2 x %d = %d\n”,i,2*i);}

โปรแกรมทุ & 52.

int x;for (x = 100 ; x > 0 ; x--) printf(“%d ”,x);

int x;for (x = 100 ; x > 0 ; x--) printf(“%d ”,x);

ต�วอย!างการลด้ค!าให�ก�บัต�วแปร

int x;for (x = 0 ; x <= 100 ; x = x+5) printf(“%d ”,x);

int x;for (x = 0 ; x <= 100 ; x = x+5) printf(“%d ”,x);

ต�วอย!างการเพิ�&มค!าหร%อลด้ค!าคร��งละหลาย ๆค!า

#include “stdio.h”int SUM , Num , i;main( ){

SUM = 0;printf(“Input Number = “);scanf(“%d”,&Num);for(i = 1 ; i<Num ; i++)

SUM = SUM + i;printf(“SUM = %d\n”,SUM);

}

#include “stdio.h”int SUM , Num , i;main( ){

SUM = 0;printf(“Input Number = “);scanf(“%d”,&Num);for(i = 1 ; i<Num ; i++)

SUM = SUM + i;printf(“SUM = %d\n”,SUM);

}

โปรแกรมทุ & 53.

Page 32: การโปรแกรมภาษา  C

32

การท"าซี"�า การท"าซี"�า (for)(for)#include “stdio.h”#include “conio.h”main( ){ int i; clrscr( ); printf(“Enter an integer: “); scanf(“%d”,&i); for(;i;i--) printf(“%d “,i); getch( );}

#include “stdio.h”#include “conio.h”main( ){ int i; clrscr( ); printf(“Enter an integer: “); scanf(“%d”,&i); for(;i;i--) printf(“%d “,i); getch( );}

โปรแกรมทุ & 57.#include “stdio.h”#include “conio.h”main( ){ int i; char ch; ch = ‘a’; clrscr( ); for (i=0;ch!=‘q’;i++){ printf(“pass: %d\n”,i); ch = getch(); }}

#include “stdio.h”#include “conio.h”main( ){ int i; char ch; ch = ‘a’; clrscr( ); for (i=0;ch!=‘q’;i++){ printf(“pass: %d\n”,i); ch = getch(); }}

โปรแกรมทุ & 56

#include “stdio.h”#include “conio.h”main( ){ int i; clrscr(); for(i=1;i<100;i++) { printf(“%d “,i); if (i==10) break; }}

#include “stdio.h”#include “conio.h”main( ){ int i; clrscr(); for(i=1;i<100;i++) { printf(“%d “,i); if (i==10) break; }}

โปรแกรมทุ & 510

ค'าส�&ง break เป#นค'าส�&งทุ &ใชุ�ในการบั�งค�บัให�โปรแกรมออกจากล1ป

ทุ�นทุ

ค'าส�&ง break เป#นค'าส�&งทุ &ใชุ�ในการบั�งค�บัให�โปรแกรมออกจากล1ป

ทุ�นทุ

Page 33: การโปรแกรมภาษา  C

33

การท"าซี"�า การท"าซี"�า ((whilewhile))

while (test condition) { <statement>;}

while (test condition) { <statement>;}

test conditiontest condition

statementstatement

no

yes

whilen = 7 ;while (n >= 0){ printf (“%d\n”,n); n = n – 5; printf(“Hi %d\n”,n);}

n = 7 ;while (n >= 0){ printf (“%d\n”,n); n = n – 5; printf(“Hi %d\n”,n);}

char ch ;ch = “\0” ;while (ch != ‘A’) ch = getche();

char ch ;ch = “\0” ;while (ch != ‘A’) ch = getche();

Page 34: การโปรแกรมภาษา  C

34

การท"าซี"�า การท"าซี"�า ((whilewhile))

#include “stdio.h”#include “conio.h”main( ){ char ch; clrscr( ); ch = getche(); while (ch!=‘q’)

ch = getche(); printf(“\n found the q”0; getch( );}

#include “stdio.h”#include “conio.h”main( ){ char ch; clrscr( ); ch = getche(); while (ch!=‘q’)

ch = getche(); printf(“\n found the q”0; getch( );}

โปรแกรมทุ & 514.#include “stdio.h”#include “conio.h”main( ){ clrscr(); n = 0 ; sum = 0 ; while (sum <= 1000) { n = n + 1; sum = sum + (n * n) ; } printf(“Sum first goes over 1000 when you add %d squared \n”,n); printf(“Sum is %d\n”,sum);}

#include “stdio.h”#include “conio.h”main( ){ clrscr(); n = 0 ; sum = 0 ; while (sum <= 1000) { n = n + 1; sum = sum + (n * n) ; } printf(“Sum first goes over 1000 when you add %d squared \n”,n); printf(“Sum is %d\n”,sum);}

โปรแกรมทุ & 515.

Page 35: การโปรแกรมภาษา  C

35

การท"าซี"�า การท"าซี"�า ((do whiledo while)) do {

<statement>; }while (test condition) ;

do {

<statement>; }while (test condition) ;

test conditiontest condition

statementstatement

no

yes

do#include “stdio.h”main( ){ int counter = 1 ; do { printf(“%d “,counter); }while (++counter <= 10);}

#include “stdio.h”main( ){ int counter = 1 ; do { printf(“%d “,counter); }while (++counter <= 10);}

#include “stdio.h”main( ){ int num; do { scanf(“%d”,&num); }while(num > 100);}

#include “stdio.h”main( ){ int num; do { scanf(“%d”,&num); }while(num > 100);}

โปรแกรมทุ & 516. โปรแกรมทุ & 517

Page 36: การโปรแกรมภาษา  C

36

ค"าส�+ง ค"าส�+ง continuecontinue ค"าส�+ง continue : เป#นค'าส�&งทุ &ตรงขี�ามก�บัค'าส�&ง

break ซู4&งค'าส�&ง continue สามารถใชุ�ได้�ใน while , for , do/while ม ล�กษณะการทุ'างานค%อ เม%&อโปรแกรมทุ'างานมาถ4งค'าส�&ง continue จะทุ'าล1ปต!อไปโด้ยไม!ทุ'าสเตด้เมนต�ทุ &ตามหล�ง continue #include “stdio.h”

main( ) { int x; for (x=1; x<=10 ; x++) { if (x==5) continue; printf(“%d “,x); } printf(“\nUsed continue to skip printing the value 5\n”);}

#include “stdio.h”main( ) { int x; for (x=1; x<=10 ; x++) { if (x==5) continue; printf(“%d “,x); } printf(“\nUsed continue to skip printing the value 5\n”);}

โปรแกรมทุ & 520