Make your own free website on Tripod.com

6
IF, CASE, GO WHILE loop


IF
     繤觷繡͡зҧҧ˹ 2 ҧ

ٻẺ 1

IF ͹ THEN 觷 1 ELSE 觷 2;

      ͹繨ԧӤ觷 1 ѧ THEN ͹Ӥ觷 2 ѧ ELSE

ٻẺ 2

IF ͹ THEN ;

      ͹繨ԧͧзӤѧ THEN

ҧ

PROGRAM EXAM12;
VAR CODE,NAME:STRING;
SCORE:INTEGER;
GRADE:STRING;
BEGIN
WRITELN(ENTER SCORE:);
READLN(SCORE);
IF SCORE>=80 THEN WRITELN(GRADE=A) ELSE
IF SCORE>=70 THEN WRITELN(GRADE=B) ELSE
IF SCORE>=60 THEN WRITELN(GRADE=C) ELSE
IF SCORE>=50 THEN WRITELN(GRADE=D) ELSE
WRITELN(GRADE=F);
END.

CASE
     繤觷᷹¹ IF Statements Ѻ͹ ͧ¹ IF ѹ¤

ٻẺ

CASE ͵͹ OF
ҵ 1 : 1;
ҵ 2 : 2;
.
.
.
.
ҵ n : n;
ELSE
;
END;

ҧ


PROGRAM EXAM13;
VAR A,B,X:INTEGER;
Y:REAL;
BEGIN
WRITELN(ENTER NUMBER);
READLN(A,B);
WRITELN(CHOOSE NUMBER (1,2,3,4) :);
READLN(X);
CASE X OF
1 : Y := A+B;
2 : Y := A-B;
3 : Y := A*B;
4 : Y := A/B;
ELSE
WRITELN(Y=0);
END;
WRITELN(Y=,Y);
END.

GOTO
     繤觷价㴷˹͹ Ш繡â仢ҧ˹͢ҧѧ ¹ҨѹⴴդШзٻẺͧçҧ

ҧ

PROGRAM EXAM14;
USES CRT;
VAR M:INTEGER;
LABEL X,Y;
BEGIN
CLRSCR;
X:WRITELN(ENTER MARK);
READLN(M);
IF M=99 THEN GOTO Y;
WRITE(RESULT= );
CASE M OF
90..100 : WRITELN(GRADE=A);
80-89 : WRITELN(GRADE=B);
70-79 : WRITELN(GRADE=C);
60-69 : WRITELN(GRADE=D);
END;
GOTO X;
Y : WRITELN(END PROGRAM);
READLN;
END.

ŷҡ RUN

ENTER MARK

- ṹ ǡ ENTER
ṹ ҵͧش ṹ 99 ENTER ͧТ ENDPROGRAM

While loop
     繤觷ͺ͹䢷 loop Шзӵ觵ҧ loop ͹䢷˹͹áѧ繨ԧ

ٻẺ

WHILE ͹ DO ;

ҧ

PROGRAM EXAM15;
VAR COUNT:INTEGER;
BEGIN
COUNT:=1;
WHILE COUNT <= 5 DO
BEGIN
WRITELN(HELLO);
COUNT:=COUNT+1;
END;
WRITELN(GOODBYE);
END.

ŷҡ RUN

HELLO
HELLO
HELLO
HELLO
HELLO
GOODBYE


Ẻ֡Ѵ
ͧ
IF, CASE, GO WHILE loop


1. ¹ԴҹӻлҢͧ١Ҩӹǹ 15 ͹䢴ѧ
ӵӡ 20 unit Թ
ҧ 20-40 unit Դ unit 3.00 ҷ
ҧ 41-60 unit Դ unit 4.00 ҷ
ҡ 60 unit Դ unit 6.00 ҷ

ҧ output
---------------------------------------------------------------------------------------------------------
SUPAB SANGKEAW
71/6 DUSIT
BANGKOK 10300

BEFORE NUMBER 12360 UNIT
AFTER NUMBER 12430 UNIT
TOTAL 70 UNIT
PAYMENT 420 UNIT
---------------------------------------------------------------------------------------------------------