1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND
1 st semester Outlines Pascal Program Structure Constant/Variable STATEMENT EXPRESSION
1 st semester Identifiers – AGAIN FROM Lecture1 Symbolic names for program elements Rules for constructing identifiers Letters, digits, and under score (_) First character letter Can be long (63 char) Reserved words are not allowed
1 st semester Pascal PROGRAM STRUCTURE program myFirstProgram; const myStudentId = ; var courseTaken: integer; begin write(‘Please Enter a number of courses’); read(‘courseTaken’); writeln(‘Student’, myStudentId, ‘ takes ’, courseTaken, ‘ courses in this semester’); end. Program Heading Declarations Program Body
1 st semester Pascal PROGRAM STRUCTURE PROGRAM HEADING DECLARATIONS NONAME00.PAS PROGRAM BODY PROGRAM HEADING
1 st semester PROGRAM HEADING Place at the beginning of file Parameter input - keyboard output - monitor Heading Syntax program YOURPROGNAME (input, output); program YOURPROGNAME ;
1 st semester PROGRAM HEADING Example Valid PROGRAM Heading program calulate_grade (input, output); program square_root (input, output); program lab3; Invalid PROGRAM Heading program registration (input, output) program polynomial (input, ); program calculate-area (input, output);
1 st semester Pascal PROGRAM STRUCTURE NONAME00.PAS PROGRAM HEADING DECLARATIONS PROGRAM BODY PROGRAM HEADING DECLARATIONS
1 st semester Declarations 1. CONSTANT 2. VARIABLE
1 st semester CONSTANT Value cannot be changed Syntax Const identifier1 = constant expression 1 ; … indentifier_n = constant expression n ;
1 st semester Constant Declaration Example Const MyLargeInteger = 9999; {value is 9999} MySmallInteger = -MyLargeInteger; {value is -9999} Star = ‘*’; {value is symbol *} FirstMonth = ‘January’; {value is string ‘January’}
1 st semester VARIABLE Referred to an individual cell in main memory Value can be changed Syntax VAR identifier1 : data type; identifier3, identifier2 : data type ;
1 st semester VARIABLE Declaration Example Var X, Y : Real; AREA: Real; AREA Y XNONAME00.PAS MAIN MEMORY
1 st semester VARIABLE Data type CHAR INTEGER REAL BOOLEAN
1 st semester CHAR data type A single character Letter Digit Symbol Character value in program must be enclosed with single quote gA1* EX. ‘g’ ‘A’ ‘1’ ‘*’ A sequence of character is known as STRING Harry PotterProtocol System EX. ‘Harry Potter’, ‘Protocol System’
1 st semester Example CHAR data type Declaration Var ch1 : char; st1 : string; ch1 st1 MAX=255
1 st semester INTEGER data type Value can be positive or negative number In i386, integer variable can keep value from to Bigger than integer is longint data type to Can be used with arithmetic operation
1 st semester Example Integer data type Declaration Var num1 : integer; num2, num3 : integer ; num4 : longint; num6, num7 : longint;
1 st semester REAL data type Integer + Fraction Example Integer Fraction
1 st semester REAL Data type Can be used in following notation Decimal notation Example 2.50, Scientific notation number x 10 exponent Are expressed in Pascal using the syntax E Example 2.5E+00, E1
1 st semester Real data type Can be used with +, -, *, / and comparison Example.2345 Invalid 1, Invalid Valid 50E10 Valid
1 st semester BOOLEAN data type TRUEFALSE Value can be TRUE or FALSE Use Frequently in Control Statement Example Variable A is the boolean data type. We can use following 11>2 A := 11>2; True
1 st semester CONCLUSION FROM PREVIOUS
1 st semester Pascal PROGRAM STRUCTURE NONAME00.PAS PROGRAM HEADING DECLARATIONS PROGRAM BODY PROGRAM HEADING DECLARATIONS PROGRAM BODY
1 st semester PROGRAM BODY Begin statement1; statement2; … statementn;End. PROGRAM BODY
1 st semester EXPRESSION ARITHMETIC EXPRESSION BOOLEAN EXPRESSION
1 st semester Arithmetic Expression Operators: + - * / DIV (truncated divide) MOD (remainder after division) Example 11/2 = DIV 2 = 5 11 MOD 2 = 1
1 st semester Arithmetic Expression (width + length) (12*(top – bottom)) 34+54/78*12
1 st semester Precedence rules for arithmetic operators 1.( ) parentheses 2.Unary + and – 3.*, /, DIV, MOD 4.+ – 5.If equal precedence, left to right Examples -a+j/-w = (-a) + (j / (-w)) C*23/6+23mod2 = ((C*23)/6) + (23 mod 2)
1 st semester Precedence rules for arithmetic operators Examples X:=A+B/C; WRONG X:=A+(B/C); X:=(A+B)/C; TRUE
1 st semester Boolean Expression Two possible values: True, False 1.Relation Operator =,, <>, = 2.Boolean Operator AND, OR, NOT 15 = 34 False < 17 True True
1 st semester Truth table AND OR T TTT T FFT F TFT F FFF
1 st semester Boolean Expression (30 12) (30 < 35) OR (99 <= 99.80) F F T = T T T =
1 st semester STATEMENT ASSIGNMENT STATEMENT OUTPUT STATEMENT INPUT STATEMENT
1 st semester ASSIGNMENT STATEMENT For storing the value or a computational result into the variable. Assignment Statement variable := expression; Example AREA := X * Y;
1 st semester ASSIGNMENT STATEMENT EXAMPLE From SUM := SUM + ITEM; ITEMSUM Before ASSIGNMENT SUM After ASSIGNMENT
1 st semester ASSIGNMENT STATEMENT EXAMPLE From AREA := X * Y; * AREAXY10020? Before ASSIGNMENT AREAXY After ASSIGNMENT
1 st semester OUTPUT STATEMENT Use following two command for output to the screen write ( ); After write, remember the location Start at that location for the next time writeln ( ); When done, go to new line and left side Syntax Write(parameter1, parameter2, …, parametern); Writeln(parameter1, parameter2, …, parametern);
1 st semester EXAMPLE WRITE Write(‘price=‘); {statement 1} Write(’10bt’); {statement 2} Line1 Before statement 1 Line1 rpeci= After statement 1 Line1 rpecib01=t After statement 2
1 st semester EXAMPLE WRITELN Writeln(‘price=‘); {statement 1} Write(’10bt’); {statement 2} Line1 Before statement 1 After statement 1 Line1 rp eci= Line2 b01t After statement 2 Line1 rp eci= Line2
1 st semester Output statement example Write (‘price =‘, price); Write (‘total = ‘, price); Writeln (‘the total is’,’ ‘, total, ‘unit’); Writeln (‘hello’,’how are you’, ‘doing’);
1 st semester Formatted output Integer : width Real_data : width Real_data : width : decimal String_data : width
1 st semester Format: Integer Data type Total:=2500; Writeln(total:6);1Line Line Line1
1 st semester Format: Real Data type PI:= ; Writeln(PI:9);1Line Line Line1 E
1 st semester Format: Real Data type PI:= ; Writeln(PI:5:2);1Line Line Line1
1 st semester Format: String Data type st1:=‘ABC’; Writeln(st1:5);1Line Line1 CBA Line1
1 st semester WHY Output looks like below?
1 st semester WHY Output looks like below?
1 st semester INPUT STATEMENT Use following two command for get input from the device into variable read() readln() Syntax read (parameter1, parameter2, … ); readln (parameter1, parameter2, …);
1 st semester INPUT STATEMENT Example read( studentid, course, section); IF We have input
1 st semester INPUT STATEMENT How to input if we use following statement? read(data1, data2); Data type 1 Data type 2 INPUT VALUE in Data1 VALUE in Data2 REMARK INTEGER 13□151315Blank is skip INTEGERCHAR13□M13□Blank is char INTEGERSTRING13□AX□13□AX□Blank is part of STRING For INTEGER, Blank is need for separate numbers. For CHAR/STRING, no blank is need for separate.
1 st semester INPUT STATEMENT What will happened? If Number of input > number of variable Exceed input will be lost If Number of input < number of variable Program will pause and wait for the require input
1 st semester CONCLUSION FROM PREVIOUS
1 st semester Conclusions
1 st semester CONCLUSION program myFirstProgram; const myStudentId = ; var courseTaken: integer; begin write(‘Please Enter a number of courses’); read(‘courseTaken’); writeln(‘Student’, myStudentId, ‘ takes ’, courseTaken, ‘ courses in this semester’); end. Program Heading Declarations Program Body
1 st semester EXAMPLE PROGRAM 1
1 st semester EXAMPLE PROGRAM 2 Problem Write a program for find the area of the circle. r AREA := Pi * Radius * Radius; a = ¶r 2 CONSTANT VARIABLE VARIABLE
1 st semester EXAMPLE PROGRAM 2 OUTPUT
1 st semester EXAMPLE PROGRAM 3 OUTPUT
1 st semester Error 3: Unknown Identifier num6 is not defined in declaration part
1 st semester Error 85: “;” expected. Current Cursor ;
1 st semester Error 94: “.” expected Have to use.(dot) for the end of program body
1 st semester What will we learn in Next Class? Flowchart If the Else