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

Slides:



Advertisements
Similar presentations
Months of the year December January November October February
Advertisements

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.
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.
Programming Languages and Paradigms
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chubaka Producciones Presenta :.
James Tam Introduction To Defining New Types in Pascal In this section of notes you will learn about programmer-defined types.
James Tam Introduction To Defining New Types in Pascal In this section of notes you how and why programmers can define new types.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Chapter 6 Control Structures.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Pascal By: Liane Tom. Outline o Background o Data types and Syntax o Procedures and Functions o Advantages o Disadvantages.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
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.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
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.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Pascal language Slides of Omar Al-Nahal. Components of Pascal Language Components of Pascal Language 1. Pascal Character set: - English Letters. - Decimal.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
INFORMATION TECHNOLOGY CSEC CXC 10/25/ PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
ISBN Chapter 8 Statement-Level Control Structures.
1 CS Programming Languages Class 11 September 26, 2000.
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.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
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.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Pascal Programming Written by Leung King Yung. Simple Program 1 begin end.
DATE POWER 2 INCOME JANUARY 100member X 25.00P2, FEBRUARY 200member X 25.00P5, MARCH 400member X 25.00P10, APRIL 800member.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
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.
Structures and Enumerations Introduction Structure definitions Nested structure Referring and initializing structure elements Passing structures to a function.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
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.
Introduction to C# By: Abir Ghattas Michel Barakat.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Reusable parts of Code Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer
COMP Loop Statements Yi Hong May 21, 2015.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
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.
Imperative Programming Statements and invariants.
structured statically typed imperative wide-spectrum object-oriented high-level computer programming language extended from Pascal and other languages.
Def: A control structure is a control statement and
Written by Al.So. Software solutions
Pascal Winter 2010/ Course.
Chapter 8: Control Structures
A Very Brief Overview of Pascal
2300 (11PM) September 21 Blue line is meridian..
McDonald’s calendar 2007.
McDonald’s calendar 2007.
2015 January February March April May June July August September
Presentation transcript:

Pascal Course Spring 20141

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 Spring 2014Course 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 separated by a semicolon } end. Winter 2010/ Course Statement Part 7

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

Data Types - cont. 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 succ(Red) = Green pred(Blue) = Green

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

Spring 2014Course 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);

Spring 2014Course 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.

Spring 2014Course 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;

Arrays in Pascal Spring 2014Course 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); !!!

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

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

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

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

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

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 Can it be done in C/C++?

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

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

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

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 Version 6 24

... 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, ); end. Spring 2014Course Version 6 – cont. Notice the syntax – Generally “address of” 25

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

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