การควบคุมภาษา C
-
Upload
warawut -
Category
Technology
-
view
620 -
download
0
description
Transcript of การควบคุมภาษา 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
ContentsContents
� Decision / Selection
� Loop / Repetition
� Break and Continue Statement
Mr.Warawut Khangkhan 2Chapter 3 C Language Control
DECISION / SELECTIONDECISION / SELECTION
Mr.Warawut Khangkhan Chapter 3 C Language Control 3
Decision / SelectionDecision / Selection
� if then
� if then else
� Nested if
� switch� switch
Mr.Warawut Khangkhan Chapter 3 C Language Control 4
if thenif then
Format:
if (expression)statement;
Mr.Warawut Khangkhan Chapter 3 C Language Control 5
if thenif then
Format:
if (expression) {statement-1;statement-2;statement-2;…}
Mr.Warawut Khangkhan Chapter 3 C Language Control 6
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
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
if then elseif then else
Mr.Warawut Khangkhan Chapter 3 C Language Control 9
if then elseif then else
Format:
if (expression) statement-true;elseelsestatement-false;
Mr.Warawut Khangkhan Chapter 3 C Language Control 10
if then elseif then else
Mr.Warawut Khangkhan Chapter 3 C Language Control 11
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
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
Nested ifNested if
Mr.Warawut Khangkhan Chapter 3 C Language Control 14
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
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
switchswitch
Mr.Warawut Khangkhan Chapter 3 C Language Control 17
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
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
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
LOOP / REPETITIONLOOP / REPETITION
Mr.Warawut Khangkhan Chapter 3 C Language Control 21
Loop / RepetitionLoop / Repetition
� while loop
� do ... while loop
� for loop
Mr.Warawut Khangkhan Chapter 3 C Language Control 22
while loopwhile loop
Format:
while (expression)statement;
Mr.Warawut Khangkhan Chapter 3 C Language Control 23
while loopwhile loop
Format:
while (expression) {statement-1;statement-2;statement-2;…}
Mr.Warawut Khangkhan Chapter 3 C Language Control 24
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
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
do ... while loopdo ... while loop
Format:
dostatement;while (expression);while (expression);
Mr.Warawut Khangkhan Chapter 3 C Language Control 27
do ... while loopdo ... while loop
Format:
do {statement-1;statement-2;statement-2;…} while (expression);
Mr.Warawut Khangkhan Chapter 3 C Language Control 28
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
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
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
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
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
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
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
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
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
BREAK AND CONTINUE BREAK AND CONTINUE STATEMENTSTATEMENTSTATEMENTSTATEMENT
Mr.Warawut Khangkhan Chapter 3 C Language Control 38
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
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
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