Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing.

Slides:



Advertisements
Similar presentations
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Advertisements

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
1 Data Type Anan Phonphoem Data Type Ordinal TypeNon-ordinal Type StandardUser Define Enumerated Subrange Integer Char Boolean Real.
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.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
J. Michael Moore Arrays CSCE 110 From James Tam’s material.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
Pascal By: Liane Tom. Outline o Background o Data types and Syntax o Procedures and Functions o Advantages o Disadvantages.
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.
Introduction. In today’s session… What is programming? Why should I learn programming? Course Outline Introduction to Programming Language Introduction.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
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.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
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.
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
Pascal language Slides of Omar Al-Nahal. Components of Pascal Language Components of Pascal Language 1. Pascal Character set: - English Letters. - Decimal.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
INFORMATION TECHNOLOGY CSEC CXC 10/25/ PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
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.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Programming, an introduction to Pascal
Pascal Course Spring Introduction Designed: 1968/9 by Niklaus Wirth Published: 1970 Imperative, structural, procedural Static and strong.
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
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.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Array : 1-dimension อนันต์ ผลเพิ่ม Anan Phonphoem
1 CSC103: Introduction to Computer and Programming Lecture No 17.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
Data Types (1) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
Data Types Chapter 6: Data Types Lectures # 12. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
structured statically typed imperative wide-spectrum object-oriented high-level computer programming language extended from Pascal and other languages.
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.
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’;
Principles of programming languages 4: Parameter passing, Scope rules
Pascal Winter 2010/ Course.
A Very Brief Overview of Pascal
CMP 131 Introduction to Computer Programming
Computer Science and an introduction to Pascal
Building Java Programs
CSC 533: Programming Languages Spring 2015
CMPE 152: Compiler Design February 21 Class Meeting
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Presentation transcript:

Pascal CourseWinter 2010/111

Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing Static binding We will use: – FreePascal Winter 2010/ Course These concepts will be explained in the lectures 2

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/ Course3

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/ Course Program Heading 4

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/ Course Block 5

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/ Course Declaration Part 6

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/ Course Statement Part 7

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/ Course8

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 : ; month : MonthType; year : ; end; Winter 2010/ Course9

Arrays in Pascal Winter 2010/ Course10 Pascal arrays are defined as follow: array [ ] of May have multiple indexes: array [1..5, 8..10] 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); !!!

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(…); begin … end; Winter 2010/ Course11

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/ Course12

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,1000) ); end. Version 1 Winter 2010/ Course13

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,1000) ); end. What if s<0? e<0? Auxiliary Function? Winter 2010/ Course14

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,1000) ); end. What if s>e? Winter 2010/ Course15

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,1000) ); end. Winter 2010/ Course16

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,1000) ); end. ‘3’ and ‘5’ should be inputs / consts… What is the difference? Can it be done in C/C++? Winter 2010/ Course17

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,1000, 3, 5) ); end. Winter 2010/ Course18

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,1000, 3, 5) ); end. ‘div1’ and ‘div2’ are already known to nested function ‘isMatching’! Winter 2010/ Course19

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,1000, 3, 5) ); end. Winter 2010/ Course20

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/ Course21

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/ Course Version 6 22

... 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;... end; begin WriteLn( sumOfMatching(1, ); end. Winter 2010/ Course Version 6 – cont. Notice the syntax – 23