Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pascal Course 234319Spring 20141. Introduction Designed: 1968/9 by Niklaus Wirth Published: 1970 Imperative, structural, procedural Static and strong.

Similar presentations


Presentation on theme: "Pascal Course 234319Spring 20141. Introduction Designed: 1968/9 by Niklaus Wirth Published: 1970 Imperative, structural, procedural Static and strong."— Presentation transcript:

1 Pascal Course 234319Spring 20141

2 Introduction Designed: 1968/9 by Niklaus Wirth Published: 1970 Imperative, structural, procedural Static and strong typing Static binding Designed to allow simple way to handle complex data structures efficiently, and be easy to learn. We will use: – FreePascal 2.4.0 http://www.freepascal.org/download.var http://www.freepascal.org/download.var Spring 2014Course 234319 These concepts will be explained in the lectures 2

3 program HelloWorld; uses crt; const hello = 'Hello, World'; procedure sayHello(c:char); begin WriteLn(hello, c); end; begin sayHello('!'); readkey; end. A basic Pascal program Spring 2014Course 2343193

4 Built-in Data Types Primitive types: – integer, boolean, real, char. Modern implementations supply more: – Longint, shortint, extended, cardinal, byte, word… output: – Write(1, 2, “hello “, “World”); – WriteLn(x, y); Input: – Read(x); – ReadLn(y); Spring 2014Course 2343194

5 Spring 2014Course 2343195 Flow Control If x < 2 then write(x); The expression between the “if” and “then” must be of type Boolean. If x < 2 then begin write(x); end If x < 2 then write(x) // no semicolon! Else write(y);

6 Spring 2014Course 2343196 Flow Control case i of 1 : write(“A”); 2 : write(“B”); 3 : write(“C”) // no semicolon end Case works well with more complicated data types, as we will see.

7 Spring 2014Course 2343197 Flow Control While x < 5 do begin read(x); end; Repeat read(x); Until x > 5; 15: Write(x) Goto 15; For i := 1 to 10 do WriteLn(i); For i := 10 downto 1 do begin WriteLn(i); end;

8 Data Types We can create our own types: – Enumerated types: type Color = (Red, Green, Blue, Yellow); Enumerated types are ordered: Red < Blue = true ord(Yellow) = 3 – Subrange types: type Letter = ‘A’.. ’Z’; Index = 3.. 8; BasicColor = Red.. Blue; – Subset types: type Rainbow = set of Color; Spring 2014Course 2343198 succ(Red) = Green pred(Blue) = Green

9 Data Types - cont. – Records (similar to C structs - one of Pascal’s original inventions) type date = record day : 1.. 31; month : ( January, February, March, April, May, June, July, August, September, October, November, December); year : 1900.. 2100; end; – Variant records Spring 2014Course 2343199

10 Arrays in Pascal Spring 2014Course 23431910 Pascal arrays are defined as follow: array [ ] of Multidimensional arrays: array [1..5, 8..10] of boolean; Example: var A : array [1..5] of real; var pens : array [Red..Green] of record width : 1..3; kind : (Regular, Bold); end; For col := Red to Yellow do writeLn(pens[col].width); !!!

11 Functions and Procedures Pascal functions always return a value function myFunc(a:integer; b:real) : real; begin myFunc := a * b; //note how we set the value end; – Functions cannot be called as a standalone statement. – In this example, `a` and `b` are passed by-value. A function that doesn’t return anything is a procedure. procedure myProc(var a : boolean); begin WriteLn(“Hello, World”); a := true; // we change the argument itself end; Spring 2014Course 23431911

12 A simple problem… Given a range of positive numbers: – Summarize all numbers in range that divide by 3 or 5. – Print the result. Spring 2014Course 23431912

13 program Sum; function sumOfMatching(s, e : integer) : integer; var sum, i : integer; begin sum := 0; for i := s to e do begin if (i mod 3 = 0) or (i mod 5 = 0) then sum := sum + i; end; sumOfMatching := sum; end; begin WriteLn( sumOfMatching(1,300) ); end. Version 1 Spring 2014Course 23431913

14 Version 1 program Sum; function sumOfMatching(s, e : integer) : integer; var sum, i : integer; begin sum := 0; for i := s to e do begin if (i mod 3 = 0) or (i mod 5 = 0) then sum := sum + i; end; sumOfMatching := sum; end; begin WriteLn( sumOfMatching(1,300) ); end. What if s<0? e<0? Auxiliary Function? Spring 2014Course 23431914

15 Version 2 program Sum; type positiveInt = 1..MAXINT; function isMatching(i : integer) : boolean; begin isMatching := (i mod 3 = 0) or (i mod 5 = 0); end; function sumOfMatching(s, e : positiveInt) : integer; var sum, i : integer; begin sum := 0; for i := s to e do begin if isMatching(i) then sum := sum + i; end; sumOfMatching := sum; end; begin WriteLn( sumOfMatching(1,300) ); end. What if s>e? Spring 2014Course 23431915

16 Version 3 program Sum; type positiveInt = 1..MAXINT; function SumOfMatching(s, e : positiveInt) : Integer; var sum, i : integer; function isMatching(i : integer) : boolean; begin isMatching := (i mod 3 = 0) or (i mod 5 = 0); end; begin sum := 0; for i := s to e do begin if isMatching(i) then sum := sum + i; end; sumOfMatching := sum; end; begin WriteLn( sumOfMatching(1,300) ); end. Spring 2014Course 23431916 Can it be done in C/C++?

17 Version 4 program Sum; type positiveInt = 1..MAXINT; function sumOfMatching(s,e,div1,div2:positiveInt):integer; var sum, i : integer; function isMatching(i, d1, d2 : integer) : boolean; begin isMatching := ((i mod d1=0) or (i mod d2=0)); end; begin sum := 0; for i := s to e do begin if isMatching(i,div1,div2) then sum:=sum+i; end; sumOfMatching := sum; end; begin WriteLn( sumOfMatching(1,300, 3, 5) ); end. ‘div1’ and ‘div2’ are already known to nested function ‘isMatching’. Spring 2014Course 23431917

18 Version 5 program Sum; type positiveInt = 1..MAXINT; function sumOfMatching(s,e,div1,div2:positiveInt):integer; var sum, i : Integer; function isMatching(i : Integer) : boolean; begin isMatching:=(i mod div1=0) or (i mod div2=0); end; begin sum := 0; for i := s to e do begin if isMatching(i) then sum := sum + i; end; sumOfMatching := sum; end; begin WriteLn( sumOfMatching(1,300, 3, 5) ); end. Spring 2014Course 23431918

19 A more general solution We can also change ‘ isMatching ’ to receive a matcher - a pointer to a function, as an argument, and call it with any integer→boolean function we desire. Spring 2014Course 23431919

20 program Sum; type positiveInt = 1..MAXINT; type matcher = function ( i:integer ) : boolean; { defining a matcher } function m1( i : integer ) : boolean; begin m1 := (i mod 7 = 0) or (i mod 13 = 0); end;... Spring 2014Course 234319 Version 6 20

21 ... function sumOfMatching( s, e : positiveInt ; isMatching : matcher ) : integer; var sum, i : Integer; begin sum := 0; for i := s to e do begin if isMatching(i) then sum := sum + i; end; sumOfMatching := sum; end; begin WriteLn( sumOfMatching(1, 300, @m1) ); end. Spring 2014Course 234319 Version 6 – cont. Notice the syntax – ‘@’ Generally “address of” 21

22 Multiline: – { … } – {* … *} – (* … *) Single line – // … Spring 2014Course 234319 Some comments 22

23 Exceptions Generics Tuples Foreach loops Named parameters Classes… Coroutines / generators Closure Garbage collection Varags, variable-length arrays Spring 2014Course 234319 What’s missing? 23


Download ppt "Pascal Course 234319Spring 20141. Introduction Designed: 1968/9 by Niklaus Wirth Published: 1970 Imperative, structural, procedural Static and strong."

Similar presentations


Ads by Google