Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 st semester 2004 1 Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.

Similar presentations


Presentation on theme: "1 st semester 2004 1 Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering."— Presentation transcript:

1

2 1 st semester 2004 1 Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th http://www.cpe.ku.ac.th/~aphirak Computer Engineering Department Kasetsart University, Bangkok THAILAND

3 1 st semester 2004 2 Outlines Pascal Program Structure Constant/Variable STATEMENT EXPRESSION

4 1 st semester 2004 3 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

5 1 st semester 2004 4 Pascal PROGRAM STRUCTURE program myFirstProgram; const myStudentId = 1234567; 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

6 1 st semester 2004 5 Pascal PROGRAM STRUCTURE PROGRAM HEADING DECLARATIONS NONAME00.PAS PROGRAM BODY PROGRAM HEADING

7 1 st semester 2004 6 PROGRAM HEADING Place at the beginning of file Parameter input - keyboard output - monitor Heading Syntax program YOURPROGNAME (input, output); program YOURPROGNAME ;

8 1 st semester 2004 7 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);

9 1 st semester 2004 8 Pascal PROGRAM STRUCTURE NONAME00.PAS PROGRAM HEADING DECLARATIONS PROGRAM BODY PROGRAM HEADING DECLARATIONS

10 1 st semester 2004 9 Declarations 1. CONSTANT 2. VARIABLE

11 1 st semester 2004 10 CONSTANT Value cannot be changed Syntax Const identifier1 = constant expression 1 ; … indentifier_n = constant expression n ;

12 1 st semester 2004 11 Constant Declaration Example Const MyLargeInteger = 9999; {value is 9999} MySmallInteger = -MyLargeInteger; {value is -9999} Star = ‘*’; {value is symbol *} FirstMonth = ‘January’; {value is string ‘January’}

13 1 st semester 2004 12 VARIABLE Referred to an individual cell in main memory Value can be changed Syntax VAR identifier1 : data type; identifier3, identifier2 : data type ;

14 1 st semester 2004 13 VARIABLE Declaration Example Var X, Y : Real; AREA: Real; AREA Y XNONAME00.PAS MAIN MEMORY

15 1 st semester 2004 14 VARIABLE Data type CHAR INTEGER REAL BOOLEAN

16 1 st semester 2004 15 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’

17 1 st semester 2004 16 Example CHAR data type Declaration Var ch1 : char; st1 : string; ch1 st1 MAX=255

18 1 st semester 2004 17 INTEGER data type Value can be positive or negative number In i386, integer variable can keep value from -32768 to 32767 Bigger than integer is longint data type -2147483648 to 2147483647 Can be used with arithmetic operation

19 1 st semester 2004 18 Example Integer data type Declaration Var num1 : integer; num2, num3 : integer ; num4 : longint; num6, num7 : longint;

20 1 st semester 2004 19 REAL data type Integer + Fraction Example 999 888. Integer Fraction

21 1 st semester 2004 20 REAL Data type Can be used in following notation Decimal notation Example 2.50, 99.99 Scientific notation number x 10 exponent Are expressed in Pascal using the syntax E Example 2.5E+00, 9.999+E1

22 1 st semester 2004 21 Real data type Can be used with +, -, *, / and comparison Example.2345  Invalid 1,250.00  Invalid -1250.00  Valid 50E10  Valid

23 1 st semester 2004 22 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

24 1 st semester 2004 23 CONCLUSION FROM PREVIOUS

25 1 st semester 2004 24 Pascal PROGRAM STRUCTURE NONAME00.PAS PROGRAM HEADING DECLARATIONS PROGRAM BODY PROGRAM HEADING DECLARATIONS PROGRAM BODY

26 1 st semester 2004 25 PROGRAM BODY Begin statement1; statement2; … statementn;End. PROGRAM BODY

27 1 st semester 2004 26 EXPRESSION ARITHMETIC EXPRESSION BOOLEAN EXPRESSION

28 1 st semester 2004 27 Arithmetic Expression Operators: + - * / DIV (truncated divide) MOD (remainder after division) Example 11/2 = 5.5 11 DIV 2 = 5 11 MOD 2 = 1

29 1 st semester 2004 28 Arithmetic Expression 45.00 (width + length) (12*(top – bottom)) 34+54/78*12

30 1 st semester 2004 29 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)

31 1 st semester 2004 30 Precedence rules for arithmetic operators Examples X:=A+B/C; WRONG X:=A+(B/C); X:=(A+B)/C; TRUE

32 1 st semester 2004 31 Boolean Expression Two possible values: True, False 1.Relation Operator =,, <>, = 2.Boolean Operator AND, OR, NOT 15 = 34 False 15.05 < 17 True 34 34.00002 True

33 1 st semester 2004 32 Truth table AND OR T TTT T FFT F TFT F FFF

34 1 st semester 2004 33 Boolean Expression (30 12) (30 < 35) OR (99 <= 99.80) F F T  = T T T  =

35 1 st semester 2004 34 STATEMENT ASSIGNMENT STATEMENT OUTPUT STATEMENT INPUT STATEMENT

36 1 st semester 2004 35 ASSIGNMENT STATEMENT For storing the value or a computational result into the variable. Assignment Statement variable := expression; Example AREA := X * Y;

37 1 st semester 2004 36 ASSIGNMENT STATEMENT EXAMPLE From SUM := SUM + ITEM; 100100ITEMSUM Before ASSIGNMENT + 200 SUM After ASSIGNMENT

38 1 st semester 2004 37 ASSIGNMENT STATEMENT EXAMPLE From AREA := X * Y; * AREAXY10020? Before ASSIGNMENT 100202000AREAXY After ASSIGNMENT

39 1 st semester 2004 38 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);

40 1 st semester 2004 39 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

41 1 st semester 2004 40 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

42 1 st semester 2004 41 Output statement example Write (‘price =‘, price); Write (‘total = ‘, price); Writeln (‘the total is’,’ ‘, total, ‘unit’); Writeln (‘hello’,’how are you’, ‘doing’);

43 1 st semester 2004 42 Formatted output Integer : width Real_data : width Real_data : width : decimal String_data : width

44 1 st semester 2004 43 Format: Integer Data type Total:=2500; Writeln(total:6);1Line1 1234 5 6 Line1 00521234 5 6 Line1

45 1 st semester 2004 44 Format: Real Data type PI:=-3.14159; Writeln(PI:9);1Line1 1234 5 9 Line1 687 41.3- 123 4 Line1 E 5 +0 0 76 89

46 1 st semester 2004 45 Format: Real Data type PI:=3.14159; Writeln(PI:5:2);1Line1 1234 5 Line1 41.31234 5 Line1

47 1 st semester 2004 46 Format: String Data type st1:=‘ABC’; Writeln(st1:5);1Line1 1234 5 Line1 CBA1234 5 Line1

48 1 st semester 2004 47 WHY Output looks like below?

49 1 st semester 2004 48 WHY Output looks like below?

50 1 st semester 2004 49 INPUT STATEMENT Use following two command for get input from the device into variable read() readln() Syntax read (parameter1, parameter2, … ); readln (parameter1, parameter2, …);

51 1 st semester 2004 50 INPUT STATEMENT Example read( studentid, course, section); IF We have input  43650357 204111 15

52 1 st semester 2004 51 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.

53 1 st semester 2004 52 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

54 1 st semester 2004 53 CONCLUSION FROM PREVIOUS

55 1 st semester 2004 54 Conclusions

56 1 st semester 2004 55 CONCLUSION program myFirstProgram; const myStudentId = 1234567; 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

57 1 st semester 2004 56 EXAMPLE PROGRAM 1

58 1 st semester 2004 57 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

59 1 st semester 2004 58 EXAMPLE PROGRAM 2 OUTPUT

60 1 st semester 2004 59 EXAMPLE PROGRAM 3 OUTPUT

61 1 st semester 2004 60 Error 3: Unknown Identifier num6 is not defined in declaration part

62 1 st semester 2004 61 Error 85: “;” expected. Current Cursor ;

63 1 st semester 2004 62 Error 94: “.” expected Have to use.(dot) for the end of program body

64 1 st semester 2004 63 What will we learn in Next Class? Flowchart If the Else


Download ppt "1 st semester 2004 1 Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering."

Similar presentations


Ads by Google