การควบคุมภาษา C

41
Chapter 3 Chapter 3 C Language Control C Language Control Mr.Warawut Khangkhan Twitter: http://twitter.com/awarawut Facebook: http://www.facebook.com/AjWarawut E-Mail: [email protected] Mobile: 083-0698-410

description

การใช้

Transcript of การควบคุมภาษา C

Page 1: การควบคุมภาษา C

Chapter 3Chapter 3C Language ControlC Language Control

Mr.Warawut Khangkhan

Twitter: http://twitter.com/awarawut

Facebook: http://www.facebook.com/AjWarawut

E-Mail: [email protected]

Mobile: 083-0698-410Mobile: 083-0698-410

Page 2: การควบคุมภาษา C

ContentsContents

� Decision / Selection

� Loop / Repetition

� Break and Continue Statement

Mr.Warawut Khangkhan 2Chapter 3 C Language Control

Page 3: การควบคุมภาษา C

DECISION / SELECTIONDECISION / SELECTION

Mr.Warawut Khangkhan Chapter 3 C Language Control 3

Page 4: การควบคุมภาษา C

Decision / SelectionDecision / Selection

� if then

� if then else

� Nested if

� switch� switch

Mr.Warawut Khangkhan Chapter 3 C Language Control 4

Page 5: การควบคุมภาษา C

if thenif then

Format:

if (expression)statement;

Mr.Warawut Khangkhan Chapter 3 C Language Control 5

Page 6: การควบคุมภาษา C

if thenif then

Format:

if (expression) {statement-1;statement-2;statement-2;…}

Mr.Warawut Khangkhan Chapter 3 C Language Control 6

Page 7: การควบคุมภาษา C

Example if1.cExample if1.c

#include <stdio.h>#include <stdlib.h>

int main( ) {

int i;

scanf(“%d”, &i);

if (i > 0)

printf(“a positive number was entered\n”);

system(“PAUSE”);

return 0;

}

Mr.Warawut Khangkhan Chapter 3 C Language Control 7

Page 8: การควบคุมภาษา C

Example if2.cExample if2.c

#include <stdio.h>#include <stdlib.h>

int main( ) {

int i;

scanf(“%d”, &i);

if (i < 0) {

printf(“a negative number was entered\n”);

i = -i;}

system(“PAUSE”);

return 0;

} Mr.Warawut Khangkhan Chapter 3 C Language Control 8

Page 9: การควบคุมภาษา C

if then elseif then else

Mr.Warawut Khangkhan Chapter 3 C Language Control 9

Page 10: การควบคุมภาษา C

if then elseif then else

Format:

if (expression) statement-true;elseelsestatement-false;

Mr.Warawut Khangkhan Chapter 3 C Language Control 10

Page 11: การควบคุมภาษา C

if then elseif then else

Mr.Warawut Khangkhan Chapter 3 C Language Control 11

Page 12: การควบคุมภาษา C

if then elseif then else

Format:

if (expression) {statement-1;statement-2; ……} else {statement-3;statement-4;…}

Mr.Warawut Khangkhan Chapter 3 C Language Control 12

Page 13: การควบคุมภาษา C

Example ifthen1.cExample ifthen1.c

#include <stdio.h>#include <stdlib.h>

int main( ) {

int i;

scanf(“%d”, &i);scanf(“%d”, &i);

if (i > 0)

printf(“a negative number was entered\n”);

elseprintf(“a negative number was entered\n”);

system(“PAUSE”);

return 0;

} Mr.Warawut Khangkhan Chapter 3 C Language Control 13

Page 14: การควบคุมภาษา C

Nested ifNested if

Mr.Warawut Khangkhan Chapter 3 C Language Control 14

Page 15: การควบคุมภาษา C

Nested ifNested if

Format:

if (expression-1)statement-1;else if (expression-2)else if (expression-2)statement-2;else if (expression-3)statement-3;elsestatement-4;

Mr.Warawut Khangkhan Chapter 3 C Language Control 15

Page 16: การควบคุมภาษา C

Example Example nestedif.cnestedif.c

#include <stdio.h>#include <stdlib.h>

int main( ) {

int score;

printf(“Enter score : “);printf(“Enter score : “);scanf(“%d”, &score);if (score > 79)printf(“Very Good\n”);

else if (score > 49)printf(“Good\n”);

elseprintf(“Bad\n”);

}Mr.Warawut Khangkhan Chapter 3 C Language Control 16

Page 17: การควบคุมภาษา C

switchswitch

Mr.Warawut Khangkhan Chapter 3 C Language Control 17

Page 18: การควบคุมภาษา C

switchswitch

Format:

switch (expression) {case const-expr.1: statement-1; break;case const-expr.2: statement-2; break;case const-expr.2: statement-2; break;case const-expr.3: statement-3; break;…[default: statement-default;]}

Mr.Warawut Khangkhan Chapter 3 C Language Control 18

Page 19: การควบคุมภาษา C

Example switch1.cExample switch1.c

#include <stdio.h>#include <stdlib.h>

int main( ) {char ch;

printf(“Enter char : “);ch = getchar( );ch = getchar( );

switch (ch) {case 'a': case 'A': printf(“Addition\n”); break;case 's': case 'S': printf(“Subtract\n”); break;case 'q': case 'Q': printf(“Quit\n”); break;default: printf(“Other Character\n”);

}

system(“PAUSE”);return 0;

} Mr.Warawut Khangkhan Chapter 3 C Language Control 19

Page 20: การควบคุมภาษา C

Example switch2.cExample switch2.c

#include <stdio.h>#include <stdlib.h>

int main( ) {int n = ?;

swicth (n) {case 2: n++;case 2: n++;case 4: n++; break;case 6: n++; break;case 8: n++;default: n++;

}printf(“N = %d\n”, n);

system(“PAUSE”);return 0;

} Mr.Warawut Khangkhan Chapter 3 C Language Control 20

Page 21: การควบคุมภาษา C

LOOP / REPETITIONLOOP / REPETITION

Mr.Warawut Khangkhan Chapter 3 C Language Control 21

Page 22: การควบคุมภาษา C

Loop / RepetitionLoop / Repetition

� while loop

� do ... while loop

� for loop

Mr.Warawut Khangkhan Chapter 3 C Language Control 22

Page 23: การควบคุมภาษา C

while loopwhile loop

Format:

while (expression)statement;

Mr.Warawut Khangkhan Chapter 3 C Language Control 23

Page 24: การควบคุมภาษา C

while loopwhile loop

Format:

while (expression) {statement-1;statement-2;statement-2;…}

Mr.Warawut Khangkhan Chapter 3 C Language Control 24

Page 25: การควบคุมภาษา C

Example while1.cExample while1.c

#include <stdio.h>#include <stdlib.h>

int main( ) {int num = 1;

while (num <= 10) {printf(“%d\n”, num);num++;}

while (num <= 10)printf(“%d\n”, num++);

system(“PAUSE”);return 0;

}

Mr.Warawut Khangkhan Chapter 3 C Language Control 25

Page 26: การควบคุมภาษา C

Example while2.cExample while2.c

#include <stdio.h>#include <stdlib.h>

int main( ) {int num = 1;int sum = 0;

while (num <= 10) {sum += num; // sum = sum + num;num++; // num = num + 1;

}

printf(“Summary of 1-10 = %d\n”, sum);

system(“PAUSE”);return 0;

}Mr.Warawut Khangkhan Chapter 3 C Language Control 26

Page 27: การควบคุมภาษา C

do ... while loopdo ... while loop

Format:

dostatement;while (expression);while (expression);

Mr.Warawut Khangkhan Chapter 3 C Language Control 27

Page 28: การควบคุมภาษา C

do ... while loopdo ... while loop

Format:

do {statement-1;statement-2;statement-2;…} while (expression);

Mr.Warawut Khangkhan Chapter 3 C Language Control 28

Page 29: การควบคุมภาษา C

Example dowhile1.cExample dowhile1.c

#include <stdio.h>#include <stdlib.h>

int main( ) {int num = 1;

do

do {printf(“%d\n”, num);num++;} while (num <= 10);

doprintf(“%d\n”, num++);

while (num <= 10);

system(“PAUSE”);return 0;

}

Mr.Warawut Khangkhan Chapter 3 C Language Control 29

Page 30: การควบคุมภาษา C

Example dowhile2.cExample dowhile2.c

#include <stdio.h>#include <stdlib.h>

int main( ) {int num = 1;int sum = 0;

do {sum += num; // sum = sum + num;num++; // num = num + 1;

} while (num <= 10);

printf(“Summary of 1-10 = %d\n”, sum);

system(“PAUSE”);return 0;

}Mr.Warawut Khangkhan Chapter 3 C Language Control 30

Page 31: การควบคุมภาษา C

for loopfor loop

Format:

for (expr.1; expr.2; expr.3)statement;

oror

for (expr.1; expr.2; expr.3) {statement-1;statement-2;…}

Mr.Warawut Khangkhan Chapter 3 C Language Control 31

Page 32: การควบคุมภาษา C

Example for1.cExample for1.c

#include <stdio.h>#include <stdlib.h>

int main( ) {int num;

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

for (num = 1; num <= 10; num++)printf(“%d\n”, num);

system(“PAUSE”);return 0;

}

Mr.Warawut Khangkhan Chapter 3 C Language Control 32

Page 33: การควบคุมภาษา C

Example for2.cExample for2.c

#include <stdio.h>#include <stdlib.h>

int main( ) {int num = 1;

for ( ; num <= 10; ) {printf(“%d\n”, num;num++;

}

system(“PAUSE”);return 0;

}Mr.Warawut Khangkhan Chapter 3 C Language Control 33

Page 34: การควบคุมภาษา C

Example for3.cExample for3.c

#include <stdio.h>#include <stdlib.h>

int main( ) {int num;int sum = 0;

for (num = 1; num <= 10; num++) {sum += num; // sum = sum + num;

}

printf(“Summary of 1-10 = %d\n”, sum);

system(“PAUSE”);return 0;

}

Mr.Warawut Khangkhan Chapter 3 C Language Control 34

Page 35: การควบคุมภาษา C

Change with for loop to while loopChange with for loop to while loop

for (expr.1; expr.2; expr.3)statement;

expr.1;

1 2

4

Mr.Warawut Khangkhan Chapter 3 C Language Control 35

expr.1;while (expr.2) {

statement;expr.3;

}

34

Page 36: การควบคุมภาษา C

Change with for loop to do .. while Change with for loop to do .. while looploopfor (expr.1; expr.2; expr.3)

statement;

expr.1;

1 2

4

Mr.Warawut Khangkhan Chapter 3 C Language Control 36

expr.1;do {

statement;expr.3;

} while (expr.2);

34

Page 37: การควบคุมภาษา C

Change with while loop to Change with while loop to do .. while loopdo .. while loopexpr.1;

while (expr.2) {statement;expr.3;

1

2

expr.3;}

Mr.Warawut Khangkhan Chapter 3 C Language Control 37

expr.1;do {

statement;expr.3;

} while (expr.2);

3

4

Page 38: การควบคุมภาษา C

BREAK AND CONTINUE BREAK AND CONTINUE STATEMENTSTATEMENTSTATEMENTSTATEMENT

Mr.Warawut Khangkhan Chapter 3 C Language Control 38

Page 39: การควบคุมภาษา C

Break and Continue StatementBreak and Continue Statement

break statementbreak statement� ���������� ���������������� ������ �������� ��������������� �� !" �������#���������������� ������ ���������$� ������������ % ������������ %

continue statementcontinue statement

� �������� ��������� while, do..while ��� for ��������������� �� !" ������������&''!��������� ������(��������������

Mr.Warawut Khangkhan Chapter 3 C Language Control 39

Page 40: การควบคุมภาษา C

Example Example break.cbreak.c

#include <stdio.h>#include <stdlib.h>

int main( ) {int num;

for (num = 1; num <= 10; num++)if (num == 5)

break;elseprintf(“%d”, num);

system(“PAUSE”);return 0;

}Mr.Warawut Khangkhan Chapter 3 C Language Control 40

Page 41: การควบคุมภาษา C

Example Example continue.ccontinue.c

#include <stdio.h>#include <stdlib.h>

int main( ) {int num;

for (num = 1; num <= 10; num++)if (num == 5)

continue;elseprintf(“%d”, num);

system(“PAUSE”);return 0;

}Mr.Warawut Khangkhan Chapter 3 C Language Control 41