Selection Structure Statement Sequential and Control Structure Statement



































- Slides: 35

����� � Selection Structure Statement

Sequential and Control Structure Statement 4 ��������� (Sequential structure statement) ���������� 1 2 �������� n 4 �������� (Control structure statement) �����������������


IF-THEN Statement 4 IF - THEN: ���������������� THEN �������� IF <Boolean expression> THEN ������� Format: Statement 1 A; Statement 2; �������� ��� IF-THEN statement ����

������ IF-THEN Example A: IF menuitem = '1' THEN writeln(‘Adding student(s)… ‘); Example B: write(‘Enter your age: ‘); readln(Age); IF Age < 20 THEN begin writeln(‘You are not adult!’); writeln(‘You have to wait until you are 20’) end; ���� : ������ IF-THEN ����������� THEN �����������

Flow Chart ��������� IFTHEN

IF-THEN-ELSE Statement 4 IF-THEN-ELSE statement �������������������� (Boolean Expression) ����� IF �������� (True) ��������� (False) ���������� IF {Boolean expression} THEN ��������� 1 {statement to be executed on true} Format: ���������� ELSE {statement to be 2 executed ELSE ������ �� on false}; �������������������� THEN

Flow Chart ������� IFTHEN-ELSE

IF-THEN-ELSE Statement Ex 1: IF menuiten = '1' THEN writeln(‘Adding student(s)… ‘) ELSE writeln(‘Deleting student(s)… ‘); EX 2: ���������������������� ���� 50 ������ (Flunk) ������� 50 ������������� (Pass) Read (Mark); IF Mark < 50 THEN writeln (‘You flunk!’)


IF-THEN-ELSE Statement Ex 4: ���������������� BEGIN No. Data : = true; IF No. Data THEN writeln(‘Program terminated!’) ELSE Begin read(Num); writeln(Num) End END.

IF-THEN-ELSE Statement Ex 5: ���������������� ��� /������� BEGIN �������� read(Salary; ( ���� IF (Salary <= 10000) THEN OT_Rate : = 1. 5 ELSE OT_Rate : = 1. 75 ; END.

IF-THEN-ELSE Statement Ex 6: ���������������� ��� /������� BEGIN �������� read(Salary; ( ���� IF (Salary <= 10000) THEN OT_Rate : = 1. 5 ELSE OT_Rate : = 1. 75 ; END.


������ IF (Nested IF Statements) IF {Boolean expression} THEN Statement 1 ELSE IF {Boolean expression} THEN Statement 2 Format: ELSE IF {Boolean expression} THEN Statement 3 ELSE. . . IF {Boolean Expression} THEN Statement. N-1 ELSE Statement. N;

������ IF (Nested IF Statements) ตวอยาง 7: ใหอานชนปของนกศกษา แลวใหพมพขอความเพอแสดงวานกศกษามสถานภาพเป นนกศกษาระดบใด Read (Year); IF (Year = 1) THEN Student. Status : = ‘Freshman’ ELSE IF (Year = 2) THEN Student. Status : = ‘Sophomore’ ELSE IF (Year = 3) THEN Student. Status : = ‘Junior’ ELSE Student. Status : = ‘Senior’; Writeln (‘Your status is : ‘, Student. Status);



������ IF (Nested IF Statements) ตวอยาง 10: หาคาเลข 3 จำนวนทเกบในตวแปร AB และ C BEGIN ทมคามากทสด IF (A > B) THEN IF (A > C) THEN largest : = A ELSE largest : = C ELSE IF (B > C) THEN largest : = B ELSE largest : = C;





����� 4 ���������������������� 22 161 ��� F PROGRAM Example. IF (input, output); VAR Age, Weight : integer; Sex : char; BEGIN write('Enter Age and Weight: '); readln(Age, Weight); write('Enter Sex: '); readln(Sex); IF (Age > 20 ) AND (Weight >150) THEN IF Sex = 'F' THEN writeln('Ha') ELSE writeln('Ho'); END.



CASE 4 ���� 11: ���������� CASE (CASE Statement) ���������� char CASE menuitem of '1' : Integer. Math; '2' : Real. Math; '3' : Complex. Numbers ELSE writeln('Program terminating. . . BYE'); END;


4 ������ 12 ������� IF ��� BEGIN read(Num); IF (Num >= 1) AND (Num <= 5) THEN A : = A+1 ELSE IF (Num=6) THEN B : = B+1 ELSE IF (Num=7) OR (Num=8) THEN C : = C + 1; END.

4 ���� 13: ����������������� Read (Year); { Class. Code เปนขอมลชนด integer } CASE Year OF 1 : BEGIN writeln (‘Freshman’); wrtieln (‘Still Young’); END; 2 : writlen (‘Sophomore’); 3 : writeln (‘Junior’); 4 : BEGIN writeln (‘Senior’); wrtieln (‘Too Old’) END: ����� ELSE �������������� Code, please re-enter!’); ������� 13 writeln(‘Invalid ������ IFYear Statement

4 ���� 14: �������������������� read (Age); CASE (Age < 18) OF true : writeln(‘You are not ole enough to vote’); false : BEGIN writeln(‘You are old enough to vote’); writeln(‘Who do you vote for? ’) END;


4 ������� CASE �� Turbo Pascal ������ ELSE ����������� ELSE 4 Turbo Pascal (��������� ) ����������� ��� ���������� read (Day); CASE Day OF 1, 7 : BEGIN writeln (‘Weekend’); writeln (‘Do laundry’) END; 2. . 6 : writeln (‘Go to work’) ELSE writeln (‘Invalid number’)


Nested CASE Statements 4 ������ CASE �������������������������� ���� readln(Score); CASE round(Score) OF 0. . 100 : CASE Score OF 0. . 49 : writeln(‘Flunk’); 50. . 59 : writeln(‘Poor’); 60. . 69 : writeln(‘Average’); 70. . 79 : writeln(‘Good’); 80. . 89 : writeln(‘Great’); 90. . 100 : writeln(‘Excellent’); END ELSE writeln(‘Invalid Score!’)