180 likes | 196 Views
Learn advanced techniques in Pascal programming for output and input statements, including list formatting, field width control, and real value notation. Explore arithmetic and logical expressions, character manipulation, and precedence rules. Practice with examples provided.
E N D
CH 3: Constructing a Pascal Program OUTPUT STATEMENTS List items one by one without blanks between them on same line write(<Item1>, <Item2>, <Item3>, ….., <ItemN>) List items one by one without blanks between them on same line and cursor moves to next line. writeln(<Item1>, <Item2>, <Item3>, ….., <ItemN>)
FORMATTING TECHNIQUES Formatting Field Width – number of characters spacing used to display the output write(<Item1>:<field width>, <Item2>:<field width> , ….., <ItemN>:<field width>) writeln(<Item1>:<field width>, <Item2>:<field width> , ….., <ItemN>:<field width>)
Formatting real value in Decimal Notation write(<Item1>:<field width>:<decimal places>, ….., <ItemN>:<field width>:<decimal places>) writeln(<Item1>:<field width>:<decimal places>, ….., <ItemN>:<field width>:<decimal places>)
INPUT STATEMENTS readln(<variable 1>, <varible 2>, ….., <variable N>) Ex 3.4 Program Book1; Var bookname, author: string; Begin readln(bookname); readln(author); writeln(bookname, ‘ is written by ‘, author) End.
3.3 EXPRESSIONS 9 4 39 36 3 X A ) ARITHMETIC EXPRESSIONS ARITHMETIC OPERATORS ARITHMETIC FUNCTIONS
LOGICAL EXPRESSION Relational operator – compare values of 2 operands and return a value of type boolean Different types can not ne compared!
Example: (X>=6) AND (X <=10) 10 6 Not (( X >= 6 ) AND (X <= 10 )) 10 6 Not ( X >= 6 ) OR Not(X <= 10 ) 10 6 6 ( X < 6 ) OR (X > 10 ) 10
Example: 6 ( X < 6 ) OR ( X > 10 ) 10 Not ( (X < 6) OR ( X < 10) ) 10 6 Not (X < 6 ) AND Not(X < 10) 10 6 (X>= 6) AND (X <=10) 10 6
Activities 1. Change uppercase to lower case CHR() ORD() Program UpperToLowerCase; Var upperletter, lowerletter : char; begin upperletter := ‘A’; lowerletter := chr( ord (upperletter )+32); writeln(‘The lower letter is ‘, lowerletter) end. 2. Change lower case to uppercase Program LowerToUpperCase; Var upperletter, lowerletter : char; begin lowerletter := ‘a’; upperletter := chr( ord (lowerletter )-32); writeln(‘The upper letter is ‘, upperletter) end.
3. Write down the output for the five writeln output statements of the following Program displayname; Var name, C, D : string; Begin name := ‘Mr. Chan Mei Shan Esther’; writeln (length (S) ); 24 C := copy( S, 5, 1) + copy( S, 10,1) + copy( S,14, 1); writeln(C); CMS writeln (length(C)); 3D := concat (copy(S, 19, 6), ‘ ‘, copy(S, 5, 4)); writeln(D); Esther Chan writeln(length(D)); 11 end,.
ORDER OF PRECEDENCE ON EXPRESSIONS -(5+1) * 2 – (5 - 2) (10>3+7) or (20 = 4 * 5) -(6) * 2 – (3) (10>10) or (20 = 20) -6*2 – 3 FALSE or TRUE -12 – 3 TRUE -15 Ex 3.7 Ex 3.6
Worksheet Q1- 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.
Q2: Write a program that accepts coordinates of 2 points and then calculate distance between the two points . A (Ax,Ay) d B (Bx,By) 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 d be the distance between the two points. program distanceFormula; var d, Ax, Ay, Bx, By: real; begin writeln(‘Enter the coordinate of point A’); readln( ___ , ___ ); writeln(‘Enter the coordinate of point B’); readln( ___ , ___ ); d :=________________________________; writeln(‘the distance between the points is ’, d:0:2) end.