CH 4 SELECTION CONTROL STRUCTURE IF Condition THEN









































- Slides: 41
CH 4 – SELECTION CONTROL STRUCTURE IF <Condition> THEN <statement A> ELSE <statement B> ; false Condition? statement B true statement A Selection between 2 actions Example 4. 1 Program Pass. Or. Fail; var mark : integer; begin START write(‘Enter Mark: ‘); readln(Mark); ‘Enter Mark’ Input mark Mark>= >=50 50 then ifif. Mark writeln(‘Pass’) else writeln(‘Fail’) end. false Mark >= 50? true Output “Pass” Output “Fail” END
IF <Condition> THEN BEGIN <statement A 1>; <statement A 2>; <statement A 3> END ELSE BEGIN <statement B 1>; <statement B 2>; <statement B 3> END; compound statements More than one statement is required to be executed when the condition is/is not satisfied. false compound statements Condition? true statement B 1 statement A 1 statement B 2 statement A 2 statement B 3 statement A 3
compound statements IF <Condition> THEN BEGIN <statement A 1>; <statement A 2>; <statement A 3> END ELSE BEGIN <statement B 1>; <statement B 2>; <statement B 3> END; Program Convert; var choice: integer; height : real; begin write(‘Enter a choice: (1, for inch to cm, 2 for cm to inch) ‘); readln(choice); START ‘Enter your choice’ Input choice false true choice =1? choice = =1 1 then ifif choice begin Enter height writeln(‘Enter your height in inch: ’); cm inch readln(Height); writeln(Height: 0: 2, ‘ inch=‘, ‘, Height* *2. 54: 0: 2, ‘ cm’) ‘ writeln(Height: 0: 2, Input height cm’) end in inch incm end else begin output height writeln(‘Enter your height in cm: ’); inch cm readln(Height); ‘ cm= ‘, Height/2. 54: 0: 2, ‘ inch’) writeln(Height: 0: 2, end writeln(Height: 0: 2, ‘ cm= ‘, Height/2. 54: 0: 2, ‘ inch’) end END end.
To do or not to do an action statement A false IF <condition> THEN <statement A>; true Condition? START display “Enter conduct” Input conduct Conduct>90? false program Check. Merit; Var conduct : integer; Begin write(‘Enter conduct: ‘); readln(conduct); conduct> >9090 then ififconduct writeln(‘You a writeln(‘You have ahave MERIT’); writeln(‘Bye’) end. Bye END true output ‘merit’
multiple selection Condition? false IF <condition> THEN <statement A>; true statement A 0 -49 Poor 50 -69 Satisfactory 70 -89 Good 90 -100 Excellent START display “Enter conduct” program Multiple. Selection; var conduct : integer; Begin write(‘Enter conduct: ‘); readln(conduct); if (conduct >=0) and (conduct < 50) then writeln(‘Poor’); if (conduct >=50) and (conduct < 70) then writeln(‘Satisfactory’); if (conduct >=70) and (conduct < 90) then writeln(‘Good’); if (conduct >= 90) then writeln(‘Excellent’) end. input conduct>=0 and <50? false conduct>=50 and <70? false conduct>=70 and <90? false conduct=90 false END true output ‘poor’ true output ‘satisfactory’ true output ‘Good’ true output ‘Excellent’
compound statements To do or not to do a series of actions IF <condition> THEN BEGIN <statement A 1>; <statement A 2>; <statement A 3> END; Program division; var x, d, q : integer; begin writeln(‘enter number and divisor’); readln(x, d); ififdd<> <>00 then begin q : = x/d; writeln(‘The quotient is ‘, q: 10: 2) writeln(‘The result is ‘, q: 10: 2) end end. true Condition? statement A 1 false statement A 2 statement A 3 START display “Enter number and divisor” input number and divisor D <> 0? true Divide number by divisor false Display result END
GAMBLING- BIG AND SMALL (a) A user guesses Big or Small or All equal (b) Program generates 3 numbers randomly (representing results of 3 dice throws). record the result as small in the variable result if the sum of the 3 numbers is less than 10. record the result as big in the variable result if the sum of the 3 numbers is greater than 10 record the result as equal in the variable result if the 3 numbers are the same. (c)Compare your guess with the result. If they match, you will win. Otherwise you lose. Program gamble; var dice 1, dice 2, dice 3: integer; result, ans : char; begin writeln(‘guess B(ig) or S(mall) or A(ll) ? ’); readln(ans); randomize; dice 1 : = random(6)+1; dice 2 : = random(6)+1 ; dice 3 : = random(6)+1; if (dice 1+dice 2+dice 3) < 10 THEN result : = ‘S’; if (dice 1+dice 2+dice 3) > 10 THEN result : = ‘B’; if (dice 1=dice 2) AND (dice 2 =dice 3) THEN result : = ‘A’; if ans = result then writeln(‘you win’) else writeln(‘you lose’) end.
Example : guessing if a number is even or odd. program Guess. Even. Odd; var num : integer; ans , result: char; begin randomize; num : = random(48)+1; if num mod 2 = 0 then result : = ‘E’ else result : = ‘O’; writeln(‘Guess even or odd? E or O’); readln(ans); if ans = result then writeln(‘win’) else writeln(‘loss’) end.
Example : computer draws a character from A to E. Users guesses. If the guess is correct, display correct message. Otherwise, display incorrect message. program drawcharacter; var ch, ans : char; begin writeln('guess a character from A to E. . '); readln(ans); randomize; writeln('drawing a character from A to E. . . '); ch : =chr( 65+random(5)); writeln('the character drawn is ', ch) if ch = ans then writeln('correct') else writeln('incorrect');
MILLIONAIRE The program assign you $0 initially. Then it generates 2 numbers from 1 to 3. Then you guess the two numbers. If each guess is correct, you gain $5000 and continue next guess. Otherwise, you must leave! Program MILLIONAIRE; var num 1, num 2, guess, money: integer; begin randomize; num 1 : = random(3)+1; num 2 : = random(3)+1 ; money: =0; writeln(‘Guess the first number); readln(guess); if guess = num 1 then begin money : = money+1000; writeln(‘Guess the second number); readln(guess); if guess = num 2 then money : = money+2000 else writeln(‘You must leave’) end.
Homework Q 1. Write a program that asks you to enter length in kilometer. asks you to enter a choice of converting the length into either meter or centimeter. Display the length in the specified unit of measurement. The output screen is as follows: First run Second run Enter length in km 4 Enter a choice: 1. km -> m 2 answer in cm is 400000 2. km->cm Enter length in km 4 Enter a choice: 1. km -> m 1 answer in m is 4000 2. km->cm
program convertkm; var km, m, cm : real; choice : integer; begin writeln(‘enter length in km’); readln(km); writeln(‘enter a choice 1. km -> m 2. km’ ); readln(choice); if choice = 1 then begin m : = km * 1000; writeln(‘answer in m is ’ , m: 0: 2) end else begin cm : = km * 100000; writeln(‘answer in cm is ’ , cm: 0: 2) end.
Q 2. Write a program that (a) Display a menu of a coke and a sprit and asks you to make a choice. (b) Asks you to enter amount of money. (c) Display the change according to the choice of drinks made, if any. (d) If not enough money is inserted, a message is displayed. The output screen is as follows: First run: 1. COKE $5 2. SPRITE $6 1 Enter your money: 7 the change is $ 2 Second run: 1. COKE $5 2. SPRITE $6 1 Enter your money: 4 Not enough money! Third run: 1. COKE $5 2. SPRITE $6 1 Enter your money: 5
program vendermachine; varchoice, price, money : integer; begin writeln(‘ 1. coke $5 2. sprite $6’); readln(choice); if choice = 1 then price : = 5 else price : = 6; writeln(‘enter your money: ’ ); readln(money); if money > price then writeln(‘the change is $’ , money-price); if money < price then writeln(‘not enough money!’ ); end.
Q 3 Write a program that (a) Display the amount in your saving bank account. (b) Asks you to enter amount of money to withdraw. (c)Display the message if you withdraw money with an amount greater than your savings. Otherwise, display your amount in your saving after withdrawal. The output screen is as follows: First run: you have $10000 savings in your bank enter hk $ for withdraw: 12000 you have not enough saving!! Second run: you have $10000 savings in your bank enter hk $ for withdraw: 8000 now in your account $2000 Third run: you have $10000 savings in your bank enter hk $ for withdraw: 10000 you can not take all money out! Must have money
program withdrawal; var balance, money : integer; begin balance : = 10000; writeln(‘you have $’, balance, ‘ savings in your bank’); writeln(‘enter hk $ for withdraw: ’); readln(money); if money > balance then writeln(‘you have not enough saving!!’); if money = balance then writeln(‘you can not take all money out! must have money’ ); if money < balance then writeln(‘now in your account $’, balance-money ); end.
program withdrawal; var balance, money : integer; begin balance : = 10000; writeln(‘you have $’, balance, ‘ savings in your bank’); writeln(‘enter hk $ for withdraw: ’); readln(money); if money > balance then writeln(‘you have not enough saving!!’) else if money = balance then writeln(‘you can not take all money out! must have money’ ) else writeln(‘now in your account $’, balance-money ); end.
Q 4 Given that $1 HK = $0. 12837 US $1 HK = $1. 05 RMB(人民幣) Write a program that Asks you to enter your amount in HK currency. Asks you to make a choose from converting HK dollar to either US dollars or RMB. Display your amount in the new currency. The output screen is as follows: First run: Second run: How many HK dollars you want to change? 400 Which currency you want? 1. US 2. RMB 1 You have US $51. 35 How much HK dollars you want to change? 400 Which currency you want? 1. US 2. RMB 2 You have RMB $420. 00
program currencychange; var hkdollar : real; choice : integer; begin writeln(‘How many HK dollars you want to change? ’); readln(hkdollar); writeln(‘Which currency you want? 1. US 2. RMB’ ); readln(choice); if choice = 1 then writeln(‘You have US $’ , hkdollar* 0. 12837 : 0: 2) else writeln(‘You have RMB $’ , hkdollar*1. 05: 0: 2) end.
NESTED IF STATEMENTS IF <Condition C 1> THEN IF <Condition C 2> THEN <statement A 1> ELSE <statement A 2> ELSE IF <Condition C 3> THEN <statement B 1> ELSE <statement B 2>; false Condition C 3? statement B 2 true Condition C 1? true false Condition C 2? statement B 1 statement A 2 0 -49 Poor 50 -69 Satisfactory 70 -89 Good 90 -100 Excellent true statement A 1
NESTED IF STATEMENTS START Ex 4. 6 program Multiple. Selection; var conduct : integer; Begin write(‘Enter conduct: ‘); readln(conduct); if conduct < 50 then writeln(‘Poor’) else if conduct < 70 then writeln(‘Satisfactory’) else if conduct < 90 then writeln(‘Good’) else writeln(‘Excellent’) display “Enter conduct” input conduct false true Conduct < 50 display “Poor” false Conduct < 70? true display “satisfactory” false Conduct < 90? display “good” true display “excellent” end. END
WORKSHEET-CH 4 Q 1 (a) Draw a flowchart for the following program. (b) Rewrite the following program as (i) one IF-THEN-ELSE statement (ii) two IF-THEN statement PROGRAM MC; VAR ans : INTEGER; BEGIN WRITELN(‘How many days in a week? ’); WRITELN(‘ 1. six 2. seven 3. eight’); READLN(ans); IF ans = 1 then writeln(‘wrong’); IF ans = 2 then writeln(‘right’); IF ans = 3 then writeln(‘wrong’); END. START display “How many days in a week” 1. six 2 seven 3. eight input choice Choice =1? false Choice=2? false Choice=3? false true output ‘wrong’ true output ‘right’ true output ‘wrong’ END (c) Modify the above program to enter A, B, C as choice instead of integers. Output screen: How many days in a week? A. six B. seven C. eight B right
b(i) IF. . THEN program mc; var ans : integer; begin writeln(‘how many days in a week? ’); writeln(‘ 1. six 2. seven 3. eight’); readln(ans); if (ans = 1) or (ans=3) then writeln(‘wrong’); if ans = 2 then writeln(‘right’); end. c) b(ii) program mc; var ans : integer; begin writeln(‘how many days in a week? ’); writeln(‘ 1. six 2. seven 3. eight’); readln(ans); if (ans = 1) or (ans=3) then writeln(‘wrong’) else writeln(‘right’); end. b(ii) program mc; var ans : char; begin writeln(‘how many days in a week? ’); writeln(‘A. six B. seven C. eight’); readln(ans); if (ans = ‘A’) or (ans=‘C’) then writeln(‘wrong’) else writeln(‘right’); end. IF –THEN-ELSE program mc; var ans : integer; begin writeln(‘how many days in a week? ’); writeln(‘ 1. six 2. seven 3. eight’); readln(ans); if ans = 2 then writeln(‘right’) else writeln(‘wrong’); end.
Q 2 (a) Draw a flowchart for the following program. (a) START (b) Rewrite the following program as IF-THEN program yesorno; ‘are there 7 days in a week? ’ var ans : char; Input answer begin writeln(‘are there 7 days in a week? (y/n)’); false true Answer=Yes? (b) readln(ans); if ans = ‘y’ then display “right” Display ‘wrong’ writeln(‘right’); else if ans = ‘n’ then END writeln(‘wrong’); end. (c) program Enter. Password; (c) Write a program to enter a password var and check if it is valid. password : string; begin Output screen: writeln(‘Enter your password’); Enter your password? readln(password); 1 st run teacher if password = ‘student’ then Wrong! writeln(‘Correct’) else Enter your password? 2 nd run student writeln(‘Wrong’); end. Correct!
Q 3 Use the following program (find the area of circle) as a reference program circlearea; const pi = 3. 1416; var r : real; begin writeln(‘enter radius: ’); readln(r); if r > 0 then writeln(‘circle area is ’, pi*r*r : 0: 2) else writeln(‘radius can not be < =0’); end. Write a program that asks you to enter length in km and display it length in metre. Output screen: 1 st run Enter length in km? 0 Length must be > 0 ! 2 nd run Enter length in km? 3 The length is 3000 m program convertkm; var km : real; begin writeln(‘Enter length in km? ’); readln(km); if km > 0 then writeln(‘The length is ’, km*1000: 0: 2, ‘m’) else writeln(‘Length must be > 0!’); end.
START Q 4 (a) Draw a flowchart for the following program. (b) Rewrite the following program as IF-THEN Ask for 2 numbers’ Input 2 numbers x, y program choiceofcalculation; var choice : char; x, y, z. : integer; Input ‘+’ or ‘-’ false begin writeln(‘enter two numbers’); readln(x, y); writeln(‘add or multiply? + or -’); readln(choice); if choice = ‘+’ then begin z : = x + y; writeln(‘the sum is ’, z); end else end. Adding or subtract the numbers? choice =‘+’? z=x-y Display the subtraction true z=x+y Display the sum if choice = ‘+’ then END begin z : = x + y; writeln(‘the sum is ’, z); end; begin if choice = ‘-’ then z : = x - y; writeln(‘the difference is’, z); begin z : = x - y; end; writeln(‘the difference is’, z); end;
Q 5 Fill in the blanks of the following program to validate the two sides PROGRAM Pyth. Theorem; VAR A, B : real; BEGIN WRITELN(‘Enter side A for a right-angled triangle : ’); READLN(A, B); IF (A > 0) and (B > 0) THEN WRITELN(‘Hypothuse is ’, sqrt(A*A+B*B)) ELSE WRITELN(‘Length of sides must be >0’); END.
Q 6 The following program asks you to enter a month and display the number of days for that month. Rewrite the following program as (a) IF-THEN, (b) IF-THEN-ELSE (c) CASE (a) program calender; var m : integer; begin writeln(‘enter a month. ’); readln(m); writeln(‘no. of days in the month is ’); if m=1 then if (m=1) or (m=3) or (m=5) or (m=7) or (m=8) or (m-10) or (m=12) then writeln(31); if m=3 then if m=2 then writeln(31); writeln(28); if m=5 then if (m=4) or (m=6) or (m=9) or (m=11) then writeln(31); writeln(30); if m=7 then end. writeln(31); if m=8 then (b) writeln(31); program calender; if m=10 then var writeln(31); m : integer; if m=12 then begin writeln(31); writeln(‘enter a month. ’); if m=2 then readln(m); writeln(28); writeln(‘no. of days in the month is ’); if m=4 then if (m=1) or (m=3) or (m=5) or (m=7) or (m=8) or (m-10) or (m=12) then writeln(30); writeln(31) if m=6 then else writeln(30); if m=2 then if m=9 then writeln(28) writeln(30); if m=11 then else writeln(30);
Q 6 The following program asks you to enter a month and display the number of days for that month. Rewrite the following program as (a) IF-THEN, (b) IF-THEN-ELSE (c) CASE program calender; var m : integer; begin writeln(‘enter a month. ’); readln(m); writeln(‘no. of days in the month is ’); if m=1 then writeln(31); if m=3 then writeln(31); if m=5 then writeln(31); if m=7 then writeln(31); if m=8 then writeln(31); if m=10 then writeln(31); if m=12 then writeln(31); if m=2 then writeln(28); if m=4 then writeln(30); if m=6 then writeln(30); if m=9 then writeln(30); if m=11 then writeln(30); (c) program calender; var m : integer; begin writeln(‘enter a month. ’); readln(m); writeln(‘no. of days in the month is ’); case m of 1, 3, 5, 7, 8, 10, 12 : writeln(31); 2 : writeln(28); 4, 6, 911 : writeln(30) end.
Q 7 The program randomly generate two numbers and ask you to guess if first number is greater than the second, the second number is greater than the first number or both are equal to each other. Finally the program display your guesses status. Output screen: Generating 2 number now Enter your choice: 1. first number > second number 2. Second number > first number 3. First number = second number 2 Correct. The first number is 5 The second number is 8
Q 8 The following program generate a number from 1 to 48 , ask you to enter your number and display if you guess right or wrong. program marksix; var num, ans : integer; begin randomize; num : = random(48)+1; writeln(‘enter a lotto number’); readln(ans); if ans = num then writeln(‘win’) else writeln(‘loss’) end. Q 8 Refer to the above program. Write a program that 1. generates 3 numbers randomly (representing results of 3 dice throws), 2. ask user to guess Big or Small. ‘Big’ if sum of the 3 numbers is greater than 10. ‘Small’ if the sum is less than or equal to 10. 3. Display the message about if the user wins or not.
Q 9: Write a program that asks you to enter 3 sides of a triangle and then check if it is a right angle triangle. A, B and C are variables for the three sides of a triangle. ASQ, BSQ, CSQ are variables for the squares of the three sides. PROGRAM Test. Right. Angled. Triangle; VAR A, B, C, ASQ, BSQ, CSQ : integer; BEGIN writeln(‘Enter 3 sides of a triangle’); readln(A, B, C); ASQ : = A*A; BSQ : = B*B; CSQ : = C*C; if CSQ=ASQ+BSQ then writeln(‘IT is a right-angled triangle’) else writeln(‘It is not a right-angled triangle’); END.
Q 10. Write a program that accepts coordinates of 2 points on a line, check if the slope is undefined (Ax=Bx). Otherwise calculate the slope of line passing through these two points. A (Ax, Ay) Let Ax, Ay be the variables for the coordinates of point A Let Bx, By be the variables for the coordinates of point B. Let m be the slope of the line passing through the two points A and B. program slope; var m, Ax, Ay, Bx, By: real; begin writeln(‘Enter the coordinate of point A’); readln(Ax, Ay); writeln(‘Enter the coordinate of point B’); readln (Bx, By); if Ax=Bx then writeln(‘slope is undefined’) else begin m : = (Ay-By)/(Ax-Bx) ; writeln(‘the slope of line passing through points is ’, m: 0: 2) end. B (Bx, By)
Worksheet Q 1 Fill in the blanks. The program ask you to enter a uppercase letter and convert it into lowercase PROGRAM CONVERTTOLOWERCASE; VAR Ch : char ; BEGIN READLN(Ch); IF (Ch >= 'A') and (Ch <= ‘Z’ ) then WRITELN(CHR(ord(Ch)+32)); END. Letter ASCII number A 65 a 97 B 66 b 98 C 67 c 99 . . Z 90 z 122
Assignment To solve a quadratic equation Ax 2 + Bx + C = 0, we use the formula. or Write a program that input 3 coefficients Check if coefficient A is zero or not. If yes, stop the program. Otherwise check if B 2 -4 AC < 0, display “no real roots” message. check if B 2 -4 AC = 0, display one real root. check if B 2 -4 AC > 0, display the two real roots. First Run: Enter coefficients A, B and C for Ax*x+Bx+C=0: 2 5 -3 The real roots are -3 and 0. 5 Second Run: Enter coefficients A, B and C for Ax*x+Bx+C=0: 1 3 3 No reat roots! Third Run: Enter coefficients A, B and C for Ax*x+Bx+C=0: 4 4 1 The real root is -0. 5
program quadratic; var a, b, c, d : real; begin writeln('enter coefficients a, b and c for the qradratic equation ax*x+bx+c=0'); readln(a, b, c); if a <> 0 then begin d : = b*b-4*a*c; if d = 0 then writeln('the real root is ', -b/(2*a): 0: 1 ); if d > 0 then writeln('the roots are ', (-b+sqrt(d))/(2*a): 0: 1, ' and ', (-bsqrt(d))/(2*a): 0: 1); if d < 0 then writeln('no real roots!') end
program quadratic; var a, b, c, d : real; begin writeln('enter coefficients a, b and c for the qradratic equation ax*x+bx+c=0'); readln(a, b, c); if a <> 0 then begin d : = b*b-4*a*c; if d = 0 then writeln('the real root is ', -b/(2*a): 0: 1 ) else if d > 0 then writeln('the roots are ', (-b+sqrt(d))/(2*a): 0: 1, ' and ', (-b-sqrt(d))/(2*a): 0: 1) else writeln('no real roots!') end
Assignment The following program draws a character From A to E and asks user to guess. If guess is correct, display correct Otherwise, display incorrect program drawcharacter; var ch, ans : char; begin writeln('guess a character from A to E. . . '); readln(ans); randomize; writeln('drawing a character from A to E. . . '); ch : =chr( 65+random(5)); if ch = ans then writeln('correct') else writeln('incorrect'); writeln('the character drawn is ', ch) end. Write a program that draws a character from or . User then guess the draw result. If guess is correct, display correct Otherwise, display incorrect Output: Enter 1. 2 2. 3. Drawing begins. . Correct. The result is : 4. to guess
Ex 4. 7 Program Multiple. Selection 3; Var conduct : integer; Begin write(‘Enter your conduct’); readln(conduct); case Conduct of 0. . 49: writeln(‘Poor’); 50 -69: writeln(‘Satistifactory’); 70 -89: writeln(‘Good’); 90 -100: writeln(‘Excellent’) end End.
Ex 4. 8 Program Calculator; Var X, Y : integer; operator: char; Begin write(‘Enter 2 numbers’); readln(X, Y); write(‘Enter operator (+, -, * or /): ’); readln(operator); case operator of ‘+’: writeln(X+Y); ‘-’: writeln(X-Y); ‘*’: writeln(X*Y); ‘/’ : writeln(X/Y) end End.
Ex 4. 8 Program Activities 42; Var number : integer; Begin write(‘Enter a lucky number ’); readln(number); case number of 1 : writeln(‘you win a car!’) 2, 3, 4 : writeln(‘you win a diamound ring!’); 5, 6, 8, 9, 10: writeln(‘you win a golden ring!’); 11. . 20 : writeln(‘you win a pen’); 21. . 100 : writeln(‘Sorry!you have no prize’); end End.