Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
S.3 Pascal Notes4 Shatin Tsung Tsin Secondary School S.3 Computer and Technology Pascal Programming Selection Statement in Pascal (If…then…else…) Consider solving quadratic equation. x2 + 3x + 2 = 0 (x + 1)(x +2) = 0 x = -1 or x = -2 We may solve the equation by “Quadratic Equation Formula” x1 = 2 − b + b 2 − 4ac − 3 + 3 − 4(1)(2) − 3 + 1 − 3 + 1 = = = = −1 2a 2(1) 2 2 − 3 − 3 2 − 4(1)(2) − 3 − 1 − 3 − 1 − b − b 2 − 4ac x2 = == = = = −2 2a 2(1) 2 2 Ex.1 program quadratic; var a, b, c : integer; x1, x2 : real; begin writeln(‘Input the coefficient of quadratic equation’); write(‘a = ’); readln(a); write(‘b = ’); readln(b); write(‘c = ’); readln(c); x1 := (-b + sqrt(b*b – 4*a*c))/2/a; x2 := (-b – sqrt(b*b – 4*a*c))/2/a; writeln; writeln(‘the roots of equation’); writeln(‘x1 = ’,x1:0:1); writeln(‘x2 = ’,x2:0:1); end. Save the file as “quad1.pas” and press [Ctrl] + [F9] to execute the program But in some cases, we cannot solve the equations. e.g. x2 + x + 1 =0 There is no solution for x. It is because: − 1 + 12 − 4(1)(1) − 3 + − 3 − b + b 2 − 4ac == = x= 2a 2(1) 2 x= − 1 − 12 − 4(1)(1) − 3 − − 3 − b − b 2 − 4ac == = 2a 2(1) 2 We cannot find −3 p.1/4 S.3 Pascal Notes4 That is when b2 – 4ac < 0, we cannot solve the equation and there will be no solution for x. Logical decision: If b2 – 4ac < 0 then writeln(‘no solution’); else calculate the roots true b2-4ac<0 false Ex.2 program quadratic; writeln(‘no var solution’); a, b, c : integer; x1, x2 : real; begin writeln(‘Input the coefficient of quadratic equation’); write(‘a = ’); readln(a); write(‘b = ’); readln(b); write(‘c = ’); readln(c); if (b*b – 4*a*c) < 0 then begin writeln; writeln(‘no solution’); end else begin x1 := (-b + sqrt(b*b – 4*a*c))/2/a; x2 := (-b – sqrt(b*b – 4*a*c))/2/a; writeln; writeln(‘the roots of equation’); writeln(‘x1 = ’,x1:0:1); writeln(‘x2 = ’,x2:0:1); end; end. Save the file as “quad2.pas” and press [Ctrl] + [F9] to execute the program Ex.3 program pos_or_neg; var a : integer; begin writeln(‘Input a number’); readln(a); if a >= 0 then writeln (‘It is a positive number’); else writeln (‘It is a negative number’); end. Save the file as “test1.pas” and press [Ctrl] + [F9] to execute the program p.2/4 Calculate the roots S.3 Pascal Notes4 Homework: Write a program to decide an input mark of student is ‘pass’ or ‘fail’ i) Define the variable “mark” as integer ii) Input the value of mark iii) If the mark is less than 50, it is fail. Otherwise, it is pass. iv) The output should be as follows: Input a mark Input a mark 37 70 fail! pass! Save the file as “assig04.pas” and press [Ctrl] + [F9] to execute and test the program Ex.4 program CalculateWages; const bonus = 500; var day_of_abs, wages : integer; begin wages := 4000; writeln(‘Enter the number of days absent’); readln(day_of_abs); if day_of_abs = 0 then wages := wages + bonus; writeln(‘The wages are $’, wages,); end. Save the file as “wage.pas” and execute it. day_of_abs =0 wages:=wages + bonus; false writeln(‘The wages are …’); Nested if…then…else statement Ex. 5 program pos_zero_neg; var a : integer; begin writeln(‘Input a number’); readln(a); if a = 0 then writeln (‘It is zero) else if a < 0 then writeln (‘It is negative’) else writeln (‘It is positive); end. Save the file as “test2.pas” and press [Ctrl] + [F9] to execute the program Relational operators Relational operator Meaning = equal <> not equal to > greater than >= greater than or equal to < less than <= less than or equal to Compound Boolean expression p q true true true false false true false false Boolean expression ‘A’ = ‘a’ ‘A’ <> ‘a’ 3>1 3 >= 1 3<1 3 <= 1 p and q true false false false p.3/4 true Truth value false true true true false false p or q true true true false S.3 Pascal Notes4 p true false not p false true Order of precedence Operator not, *, /, div, mod, and +, -, or =, <>, >, >=, <, <= Priority 1 (highest) 2 3 4 (lowest) e.g. 25 + 3 * 10 25 + 30 55 true >= 50 >= 50 >= 50 e.g. not true or true and false false or true and false false or false false e.g. not (4 <= 5) not true false false and (3 <> 2) and true and true e.g. ((‘c’ < ‘a’) (false false false or (‘c’ > ‘f’)) and (‘c’ <> ‘u’) or false) and true and true Ex. 6 program mark_grade; var mark : integer; begin write(‘Enter exam mark: ‘); readln(mark); if mark >= 90 then writeln (‘A’); if (mark >= 80) and (mark < 90) then writeln (‘B’); if (mark >= 70) and (mark < 80) then writeln (‘C’); if (mark >= 60) and (mark < 70) then writeln (‘D’); if (mark >= 50) and (mark < 60) then writeln (‘E’); if mark < 50 then writeln (‘F’); end. Save the file as “grade.pas” and press [Ctrl] + [F9] to execute the program Homework: Write a program to decide the biggest number from 3 input numbers i) Define the variable a, b and c as integer ii) Input the value of a, b and c iii) Decide which is the biggest one iv) The output should be as follows: Input three numbers Input three numbers Input three numbers 147 492 826 The biggest number is 7 The biggest number is 9 The biggest number is 8 Save the file as “assig05.pas” and press [Ctrl] + [F9] to execute and test the program p.4/4