Pascal Programming 101
Pascal Programming 101
Pascal
integer; writeln('Hello');
stores 123 to num, errorpos = 0
(because no error occurs) writeln(age)
end.
Assignment statement A := 1;
Format: copy(string, start position, number of copy chars) Functions Assignment operator :=
copy('TRUE LIGHT', 1, 4) gives 'TRUE'
copy('TRUE LIGHT', 6, 5) gives 'LIGHT' copy
Variable A is like a box in computer
count := 0;
repeat function cube(N : integer) : real;
count := count + 1; begin
write(count)
repeat ... until loop 先做後問 cube := N*N*N
until count >= 5;
useful for validation, e.g. repeat input until a
positive value is entered
Iteration statement User-defined function end;
case N of
2, 3, 5, 7: writeln('prime');
4, 6, 8..10 : writeln('composite')
random gives real number between 0 and 1
Nested if statement else writeln('1 is special')
Nested If if <condition 1> end;
then if <condition 2> random(N) gives integer between 0 and N-1
then <statement 1>
else <statement 2> Random number random(B - A + 1) + A gives an integer
else if <condition 3> between A and B inclusively
generator
then <statement 3>
else <statement 4>;