PASCAL 11 OVERVIEW OF PASCAL LANGUAGE PASCAL, like BASIC, is a computer language However, PASCAL, unlike BASIC, is a Blocked Structured Language (BASIC is known as unstructured) Let look at a simple {BLOCK I) ----------------------------------------------------------------------------- {BLOCK II} num_two = 2; num_three = 3; sum : integer; ----------------------------------------------------------------------------- {BLOCK III} sum : = num_two + num_three; writeln (num_two, +, num_three is, sum ); end EXAMPLE is a simple PASCAL program When the program is executed, the following result will be displayed on the terminal 2 + 3 is 5 12 GENERAL FORMAT OF PASCAL PROGRAM (1) Program Heading (2) Declaration Section (3) Execution Section 13 GENERAL FORMAT OF PROGRAM HEADING (1) Program Identifier Identify the PASCAL program (2) List of External Files Specify a list of files which will be used by the program to communicate with the outside world 14 GENERAL FORMAT OF DECLARATION SECTION Declaration Section is consisted of (1) Label declaration (2) Constant declaration (3) Type declaration (4) Variable declaration (5) Procedure declaration (6) Function declaration 15 GENERAL FORMAT OF EXECUTION SECTION statement_1; statement_2; end CCASS / Computer Studies / Pascal Programming / Overview P1
There are different s of program statements : (1) Basic Statements (2) Control Statements i) Conditional Statements ii) Iterative Statements (3) Sequential File Handling Statements (4) Call subprogram statements (5) Comment Statements 2 OVERVIEW OF DECLARATION SECTION 21 CONSTANT DECLARATION identifier_1 = scalar_ant_1; identifier_2 = scalar_ant_2; pi = 31416; end Valid ant declaration g = 098; {real number} kelvin = -273; {integer} myname = ary ; {string} star = * ; {character} Iloveyou = true; {boolean} twopi = 20 * 31416; {arithmatic expression is accepted} Invalid ant declaration myrange = 1 9; {range is not allowed} Note A ant value cannot be changed by any part of the program min_value = 10; min_value : = 2; {illegal} end CCASS / Computer Studies / Pascal Programming / Overview P2
22 Data Type Array Structure Record File Subrange Data Type User-defined Enumerated User-defined Scalar Integer Standard Real Boolean Character Standard Data Type 221 Integer Valid PASCAL integer ants 28000, -28000, 1, 0, 20 maxint (predefined PASCAL ant represent highest integer number) Invalid PASCAL integer ants 1,400 (comas inserted) 123 (decimal inserted) 222 Real Number a) Decimal notation Valid PASCAL Real ants (in decimal notation) 31415927, -250, +0188, 01, 11 etc Invalid PASCAL Real ants (in decimal notation) 1 (no leading zero) 3 (no decimal point and fractional part) 1,0000 (comas is inserted) b) Scientific notation 223 Boolean a) true b) false Valid PASCAL Real ants (in Scientific notation) 5e6 (this represent 500000 or 5 10 6 ) -1e3 (this represent -1000 or -1 10 3 ) 1e-9 (this represent 0000000001 or 1 10 9 ) Invalid PASCAL Real ants (in Scientific notation) -23e2 (decimal after e) 224 Character - (char in short form) enclosed by a pair of SINGLE quotation marks Valid PASCAL Characters 'a' 'b' 'c' '*' '%' ' '' ' Invalid PASCAL Characters a (no quotation marks) "a" (use of double quotation mark is invalid) CCASS / Computer Studies / Pascal Programming / Overview P3
23 Variable Declaration - Define an object that can assume different value during the execution of program identifier_1 : data 1; identifier_2 : data 2; name_of_employee_1 : string; name_of_employee_2 : string; salary_of_employee_1 : real; paid_or_not : boolean; account_of_employee_1 : integer; sex_of_employee_1 : char; For the scalar standard data, the declaration of the iable can be directly made However, for all other data, the iable declaration must be made together with the declaration (will be discussed later) examscore = 0100; exam_result = examscore; end 24 Type Declaration 241 Scalar Standard Type - declaration is usually not needed 242 Scalar User-Defined Type (I) real Scalar Subrange Data Type - is a data composed of a specific range of any other standard or user-defined scalar s, except _name_identifier_1 = lower_limit_1upper_limit_1; _name_identifier_2 = lower_limit_2upper_limit_2; examscore = 0100; letter = 'a' 'z' ; exam_result : examscore; #iable declaration will be discussed later exam_result : = 60; end (II) Scalar Enumerated User-Defined Data Type CCASS / Computer Studies / Pascal Programming / Overview P4
- a series of data s defined by user _name_identifier_1 = (identifier_1, identifier_2, ) ; _name_identifier_2 = (identifier_3, identifier_4, ) ; day_of_week_ = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday) day : day_of_week_; if ( ( day = Saturday ) or ( day = Sunday ) ) then writeln ( t is weekend ); for day := Monday to Friday do writeln ( oday is a school day ); end Common errors of Scalar Enumerated User-Defined Data Type a_ = ( brother, sister, son, mother ); b_ = ( brother, daughter, father ); 243 Structure Data Type (I) Array (II) Record (III) File (I) Array - a collection of data objects of identical by a single name _name_identifier_1 = array [t1 t2, t3 t4, ] of base1; _name_identifier_2 = array [t1 t2, t3 t4, ] of base2; 1 a = array [13] of real ; my_number : a; end 2 CCASS / Computer Studies / Pascal Programming / Overview P5
program example_2 (input,output); employee_name_ = array [1100] of string; employee_salary_ = array [1100] of integer; employee_name : employee_name_; employee_salary : employee_salary_; end 3 sample_ = array [-5020] of integer; sample : sample_; 4 day_of_the_week = (sun, mon, tue, wed, thur, fri, sat); weekday_ = array [day_of_the_week] of integer; weekday : weekday_; 5 lower_limit = -1 * maxint; upper_limit = maxint; x = array [lower_limit upper_limit] of integer; hello : x; Note i) The lower limit must be smaller than that of the upper limit ii) Both lower limit and upper limit can be negative iii) The base can be array (ie array of array) # Declaration of Record and File structure data will be discussed in later chapters CCASS / Computer Studies / Pascal Programming / Overview P6