Pascal Winter 2010/11 234319 Course.

Slides:



Advertisements
Similar presentations
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Advertisements

CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Block-Structured Procedural Languages Lecture 11: Dolores Zage.
PASCAL. HISTORY OF PASCAL Developed by Niklaus Wirth a member of the International Federation of Information Processing(IFIP) To provide features that.
Pascal Course Spring Introduction Designed: 1968/9 by Niklaus Wirth Published: 1970 Imperative, structural, procedural Static and strong.
1 The CONST definition CONST Pi = , City = ‘New York’; Constant identifiers are used when you do not want the value of an identifier to change why.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
Pascal language Slides of Omar Al-Nahal. Components of Pascal Language Components of Pascal Language 1. Pascal Character set: - English Letters. - Decimal.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Introduction to Pascal The Basics of Program writing.
Ch. 5 Ch. 51 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (more notes) Dr. Carter Tiernan.
Pascal CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Pascal Course Spring Introduction Designed: 1968/9 by Niklaus Wirth Published: 1970 Imperative, structural, procedural Static and strong.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Ch. 5 Ch. 51 jcmt Summer 2003Programming Languages CSE3302 Programming Languages (more notes) Summer 2003 Dr. Carter Tiernan.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Array : 1-dimension อนันต์ ผลเพิ่ม Anan Phonphoem
Introduction to programming in java Lecture 21 Arrays – Part 1.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types (ADTs) Brent M. Dingle Texas A&M University Chapter 8 – Sections 2 and 3 (and some from Mastering.
Principles of programming languages 10: Object oriented languages
Functions + Overloading + Scope
User-Written Functions
Chapter 7: Expressions and Assignment Statements
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
The CONST definition CONST Pi = , City = ‘New York’;
Introduction to Computer Science / Procedural – 67130
Complex data types Complex data types: a data type made of a complex of smaller pieces. Pascal has four very commonly used complex data types: strings,
Principles of programming languages 4: Parameter passing, Scope rules
Chapter 7: Expressions and Assignment Statements
[Array, Array, Array, Array]
Conditionals & Boolean Expressions
Chapter 8: Control Structures
Lecture 6 C++ Programming
Engineering Innovation Center
CMP 131 Introduction to Computer Programming
CMPE 152: Compiler Design February 6 Class Meeting
Introduction to the C Language
Arrays, For loop While loop Do while loop
Elizabeth Pruett, Eric Gonzalez and Nick Puig
Computer Science and an introduction to Pascal
Building Java Programs Chapter 2
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
CMPE 152: Compiler Design September 18 Class Meeting
One-Dimensional Array Introduction Lesson xx
Building Java Programs
CSC 533: Programming Languages Spring 2015
Arrays, Part 1 of 2 Topics Definition of a Data Structure
[Array, Array, Array, Array]
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CISC/CMPE320 - Prof. McLeod
Building Java Programs
Building Java Programs
Course Overview PART I: overview material PART II: inside a compiler
Arrays In this section of notes you will be introduced to a composite type where all elements must be of the same type (homogeneous): arrays Homogeneous.
Building Java Programs
Building Java Programs
Building Java Programs
CMPE 152: Compiler Design February 21 Class Meeting
Building Java Programs
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Building Java Programs
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Pascal Winter 2010/11 234319 Course

Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing Static binding We will use: FreePascal 2.4.0 http://www.freepascal.org/download.var These concepts will be explained in the lectures Winter 2010/11 234319 Course

A basic Pascal program program HelloWorld; { Definitions are placed here - types, variables, procedures, functions, … } begin WriteLn(‘Hello World!’); { More statements can be added here } end. Winter 2010/11 234319 Course

A basic Pascal program Program Heading program HelloWorld; { Definitions are placed here - types, variables, procedures, functions, … } begin WriteLn(‘Hello World!’); { More statements can be added here } end. Program Heading Winter 2010/11 234319 Course

A basic Pascal program Block program HelloWorld; { Definitions are placed here - types, variables, procedures, functions, … } begin WriteLn(‘Hello World!’); { More statements can be added here } end. Block Winter 2010/11 234319 Course

A basic Pascal program Declaration Part program HelloWorld; { Definitions are placed here - types, variables, procedures, functions, … } begin WriteLn(‘Hello World!’); { More statements can be added here } end. Declaration Part Winter 2010/11 234319 Course

A basic Pascal program Statement Part program HelloWorld; { Definitions are placed here - types, variables, procedures, functions, … } begin WriteLn(‘Hello World!’); { More statements can be added here } end. Statement Part Winter 2010/11 234319 Course

Data Types Pascal has 4 primitive types: integer, boolean, real, char We can also create our own types: Enumerated types: type Color = (Red, Green, Blue, Yellow); type MonthType = (January, February, ... ,December); Enumerated types are comparable: Red < Blue = true, succ(Red) = Green, pred(Blue) = Green, ord(Yellow) = 3 Winter 2010/11 234319 Course

Data Types - cont. Subrange types: type Letter = ‘A’ .. ’Z’; Index = 3 .. 8; ColorList = Red .. Blue; Records (Complex types like C structs): type date = record day : 1 .. 31; month : MonthType; year : 1900 .. 2100; end; Winter 2010/11 234319 Course

Arrays in Pascal Pascal arrays are defined as follow: array [<index-type>] of <element-type> May have multiple indexes (dimensions): array [1..5 , 2..6] of … 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); !!! Winter 2010/11 234319 Course

Functions and Procedures Pascal functions always return a value function myFunc(…) : int; begin … myFunc := 13; {note how we set the value} end; A function that doesn’t return anything is a procedure. procedure myProc(…); Winter 2010/11 234319 Course

A simple problem… Given a range of positive numbers: Summarize all numbers in range that divide by 3 or 5. Print the result. Winter 2010/11 234319 Course

Version 1 program Sum; function sumOfMatching(s, e : integer) : integer; var sum, i : integer; begin sum := 0; for i := s to e do if ( (i mod 3 = 0) or (i mod 5 = 0) ) then sum := sum + i; end; sumOfMatching := sum; WriteLn( sumOfMatching(1,1000) ); end. Winter 2010/11 234319 Course

Version 1 What if s<0? e<0? Auxiliary Function? program Sum; function sumOfMatching(s, e : integer) : integer; var sum, i : integer; begin sum := 0; for i := s to e do if ( (i mod 3 = 0) or (i mod 5 = 0) ) then sum := sum + i; end; sumOfMatching := sum; WriteLn( sumOfMatching(1,1000) ); end. What if s<0? e<0? Auxiliary Function? Winter 2010/11 234319 Course

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; sum := 0; for i := s to e do begin if ( isMatching(i) ) then sum := sum + i; sumOfMatching := sum; begin WriteLn( sumOfMatching(1,1000) ); end. What if s>e? Winter 2010/11 234319 Course

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; sum := 0; for i := s to e do begin if ( isMatching(i) ) then sum := sum + i; sumOfMatching := sum; begin WriteLn( sumOfMatching(1,1000) ); end. Winter 2010/11 234319 Course

Version 3 What is the difference? Can it be done in C/C++? 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; sum := 0; for i := s to e do begin if ( isMatching(i) ) then sum := sum + i; sumOfMatching := sum; begin WriteLn( sumOfMatching(1,1000) ); end. What is the difference? Can it be done in C/C++? ‘3’ and ‘5’ should be inputs / consts… Winter 2010/11 234319 Course

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; sum := 0; for i := s to e do begin if (isMatching(i,div1,div2)) then sum:=sum+i; sumOfMatching := sum; begin WriteLn( sumOfMatching(1,1000, 3, 5) ); end. Winter 2010/11 234319 Course

‘div1’ and ‘div2’ are already known to nested function ‘isMatching’! 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; sum := 0; for i := s to e do begin if (isMatching(i,div1,div2)) then sum:=sum+i; sumOfMatching := sum; begin WriteLn( sumOfMatching(1,1000, 3, 5) ); end. ‘div1’ and ‘div2’ are already known to nested function ‘isMatching’! Winter 2010/11 234319 Course

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; sum := 0; for i := s to e do begin if ( isMatching(i) ) then sum := sum + i; sumOfMatching := sum; begin WriteLn( sumOfMatching(1,1000, 3, 5) ); end. Winter 2010/11 234319 Course

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. Winter 2010/11 234319 Course

Version 6 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; ... Winter 2010/11 234319 Course

Version 6 – cont. Notice the syntax – ‘@’ ... function sumOfMatching( s, e : positiveInt ; isMatching : matcher ) : integer; var sum, i : Integer; begin for i := s to e do begin if ( isMatching(i) ) then sum := sum + i; end; WriteLn( sumOfMatching(1, 1000, @m1) ); end. Notice the syntax – ‘@’ Winter 2010/11 234319 Course

So why learn Pascal?? Still quite useful Safety Speed and Size Mixing types leads to errors. No type casting and no pointer arithmetic. Speed and Size Pascal compiler still fast. Teaching Purposes Shown to be easier to learn. Less overhead and fewer ways for a student to get a program into trouble. Winter 2010/11 234319 Course

So why learn Pascal?? – cont. Still in the market Many small-scale freeware, shareware, and open-source programs are written in Pascal or in Delphi (“Object Pascal”). We will use Pascal and Pascal-like languages in many examples and most likely in some of the exam questions. So you need to know the basics of it…  Winter 2010/11 234319 Course