1 st semester 2004 1 basic pascal elements อภิรักษ์...
TRANSCRIPT
11st semester 2004
Basic Pascal Elements
อภิ�รั�กษ์� จั�นทรั�สรั�างAphirak Jansang
[email protected]://www.cpe.ku.ac.th/~aphirak
Computer Engineering DepartmentKasetsart University, Bangkok THAILAND
21st semester 2004
Outlines
Pascal Program Structure Constant/Variable STATEMENT EXPRESSION
31st semester 2004
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
41st semester 2004
Pascal PROGRAM STRUCTUREprogram 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
51st semester 2004
Pascal PROGRAM STRUCTURE
PROGRAM HEADING
DECLARATIONS
NONAME00.PASNONAME00.PAS
PROGRAM BODY
PROGRAM HEADING
61st semester 2004
PROGRAM HEADING
Place at the beginning of file Parameter
input - keyboard output - monitor
Heading Syntax program YOURPROGNAME (input,
output); program YOURPROGNAME ;
71st semester 2004
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);
81st semester 2004
Pascal PROGRAM STRUCTURE
NONAME00.PASNONAME00.PAS
PROGRAM HEADING
DECLARATIONS
PROGRAM BODY
PROGRAM HEADING
DECLARATIONS
91st semester 2004
Declarations
1. CONSTANT 2. VARIABLE
101st semester 2004
CONSTANT
Value cannot be changed Syntax
Constidentifier1 = constant expression 1;………indentifier_n = constant expression n;
111st semester 2004
Constant Declaration Example
ConstMyLargeInteger = 9999; {value is 9999}
MySmallInteger = -MyLargeInteger; {value is -9999}
Star = ‘*’; {value is symbol *}
FirstMonth = ‘January’; {value is string ‘January’}
121st semester 2004
VARIABLE
Referred to an individual cell in main memory
Value can be changed Syntax
VARidentifier1 : data type;
identifier3, identifier2 : data type;
131st semester 2004
VARIABLE Declaration Example
VarX, Y : Real;AREA: Real;
AREAAREA
YY
XX
NONAME00.PASNONAME00.PAS
MAIN MEMORY
141st semester 2004
VARIABLE Data type
CHAR INTEGER REAL BOOLEAN
151st semester 2004
CHAR data type A single character
Letter Digit Symbol
Character value in program must be enclosed with single quote EX. ‘gg’ ‘AA’ ‘11’ ‘**’
A sequence of character is known as STRING EX. ‘Harry PotterHarry Potter’, ‘Protocol SystemProtocol System’
161st semester 2004
Example CHAR data type Declaration
Varch1 : char;st1 : string;
ch1ch1
st1st1
MAX=255MAX=255
171st semester 2004
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
181st semester 2004
Example Integer data type Declaration
Varnum1 : integer;num2, num3 : integer
;num4 : longint;num6, num7 :
longint;
191st semester 2004
REAL data type Integer + Fraction
Example
999999 888888..
Integer Fraction
201st semester 2004
REAL Data type
Can be used in following notation Decimal notation Example 2.50, 99.99 Scientific notation
number x 10exponent
Are expressed in Pascal using the syntax
<number> E <exponent>
Example 2.5E+00, 9.999+E1
211st semester 2004
Real data type
Can be used with +, -, *, / and comparison
Example .2345 Invalid 1,250.00 Invalid -1250.00 Valid 50E10 Valid
221st semester 2004
BOOLEAN data type
Value can be TRUETRUE or FALSEFALSE Use Frequently in Control Statement Example
Variable A is the boolean data type.We can use following
A := 11>211>2; True
231st semester 2004
CONCLUSION FROM PREVIOUS
241st semester 2004
Pascal PROGRAM STRUCTURE
NONAME00.PASNONAME00.PAS
PROGRAM HEADING
DECLARATIONS
PROGRAM BODY
PROGRAM HEADING
DECLARATIONS
PROGRAM BODY
251st semester 2004
PROGRAM BODY
BeginBeginstatement1;statement2;…statementn;
End.End.
PROGRAM BODYPROGRAM BODY
261st semester 2004
EXPRESSION
ARITHMETIC EXPRESSION BOOLEAN EXPRESSION
271st semester 2004
Arithmetic Expression Operators:
+ - * / DIV (truncated divide) MOD (remainder after division)
Example 11/2 = 5.5 11 DIV 2 = 5 11 MOD 2 = 1
281st semester 2004
Arithmetic Expression
45.00 (width + length) (12*(top – bottom)) 34+54/78*12
291st semester 2004
Precedence rules for arithmetic operators
1. ( ) parentheses2. Unary + and –3. *, / , DIV , MOD4. + –5. If equal precedence, left to rightExamples-a+j/-w = (-a) + (j / (-w))
C*23/6+23mod2 = ((C*23)/6) + (23 mod 2)
301st semester 2004
Precedence rules for arithmetic operators
Examples
C
BAX
X:=A+B/C;
WRONG
X:=A+(B/C);
X:=(A+B)/C;TRUETRUE
311st semester 2004
Boolean Expression Two possible values: True, False1. Relation Operator
= , < , > , <> , <= , >=2. Boolean Operator
AND , OR , NOT
15 = 34 False
15.05 < 17 True
34 < > 34.00002 True
321st semester 2004
Truth table
AND ORT T T TT F F TF T F TF F F F
331st semester 2004
Boolean Expression
(30 < 15) AND (54 < > 12)
(30 < 35) OR (99 <= 99.80)
FF FFTT ==
TT TTTT ==
341st semester 2004
STATEMENT
ASSIGNMENT STATEMENT OUTPUT STATEMENT INPUT STATEMENT
351st semester 2004
ASSIGNMENT STATEMENT
For storing the value or a computational result into the variable.
Assignment Statementvariable := expression;
ExampleAREA := X * Y;
361st semester 2004
ASSIGNMENT STATEMENT EXAMPLE
From
SUM := SUM + ITEM;SUM := SUM + ITEM;
100100 100100
ITEMITEMSUMSUMBefore ASSIGNMENTBefore ASSIGNMENT
++
200200
SUMSUMAfter ASSIGNMENTAfter ASSIGNMENT
371st semester 2004
ASSIGNMENT STATEMENT EXAMPLE
From
AREA := X * Y;AREA := X * Y;
**
AREAAREAXXYY
100100 2020 ??Before ASSIGNMENTBefore ASSIGNMENT
100100 2020 20002000
AREAAREAXXYY
After ASSIGNMENTAfter ASSIGNMENT
381st semester 2004
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
SyntaxWrite(parameter1, parameter2, …, parametern);Writeln(parameter1, parameter2, …,
parametern);
391st semester 2004
EXAMPLE WRITE Write(‘price=‘); {statement 1}Write(’10bt’); {statement 2}
Line1
Before statement 1Before statement 1
Line1 rp eci = After statement 1After statement 1
Line1 rp eci b01= t
After statement 2After statement 2
401st semester 2004
EXAMPLE WRITELN Writeln(‘price=‘); {statement 1}Write(’10bt’); {statement 2}
Line1
Before statement 1Before statement 1
After statement 1After statement 1Line1 rp eci =
Line2
b01 t
After statement 2After statement 2Line1 rp eci =
Line2
411st semester 2004
Output statement example
Write (‘price =‘, price); Write (‘total = ‘, price); Writeln (‘the total is’,’ ‘, total,
‘unit’); Writeln (‘hello’,’how are you’, ‘doing’);
421st semester 2004
Formatted output Integer : width Real_data : width Real_data : width : decimal String_data : width
431st semester 2004
Format: Integer Data typeTotal:=2500;Writeln(total:6);
11Line1Line1
11 22 33 44 55 66Line1Line1
0052
11 22 33 44 55 66Line1Line1
441st semester 2004
Format: Real Data typePI:=-3.14159;Writeln(PI:9);
11Line1Line1
11 22 33 44 55 99Line1Line1
66 8877
41.3-
11 22 33 44Line1Line1 E
55
+ 0 0
7766 88 99
451st semester 2004
Format: Real Data typePI:=3.14159;Writeln(PI:5:2);
11Line1Line1
11 22 33 44 55Line1Line1
41.3
11 22 33 44 55Line1Line1
461st semester 2004
Format: String Data typest1:=‘ABC’;Writeln(st1:5);
11Line1Line1
11 22 33 44 55Line1Line1
CBA
11 22 33 44 55Line1Line1
471st semester 2004
WHY Output looks like below?
481st semester 2004
WHY Output looks like below?
491st semester 2004
INPUT STATEMENT
Use following two command for get input from the device into variable read() readln()
Syntax read (parameter1, parameter2, … ); readln (parameter1, parameter2, …);
501st semester 2004
INPUT STATEMENT
Exampleread( studentid, course, section);
IF We have input 43650357 204111 15
511st semester 2004
INPUT STATEMENT How to input if we use following
statement?read(data1, data2);
Data type Data type 11
Data type Data type 22
INPUTINPUT VALUE VALUE in in
Data1Data1
VALUE VALUE in in
Data2Data2
REMARKREMARK
INTEGER INTEGER 13□15 13 15 Blank is skip
INTEGER CHAR 13□M 13 □ Blank is char
INTEGER STRING 13□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.
521st semester 2004
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
531st semester 2004
CONCLUSION FROM PREVIOUS
541st semester 2004
Conclusions
551st semester 2004
CONCLUSIONprogram 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
561st semester 2004
EXAMPLE PROGRAM 1
571st semester 2004
EXAMPLE PROGRAM 2
Problem Write a program for find the area of the circle.
rr
AREA := Pi * Radius * Radius;
a = ¶r2CONSTANTCONSTANT VARIABLEVARIABLE
VARIABLEVARIABLE
581st semester 2004
EXAMPLE PROGRAM 2
OUTPUTOUTPUT
591st semester 2004
EXAMPLE PROGRAM 3
OUTPUTOUTPUT
601st semester 2004
Error 3: Unknown Identifier
num6 is not definednum6 is not definedinin
declaration partdeclaration part
611st semester 2004
Error 85: “;” expected.
Current CursorCurrent Cursor
;;
621st semester 2004
Error 94: “.” expected
Have to use .(dot)Have to use .(dot)for the end for the end
of of program bodyprogram body
631st semester 2004
What will we learn in Next Class?
FlowchartIf the Else