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.
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.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
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.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Chapter 6 Control Structures.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
James Tam Introduction To Defining New Types in Pascal In this section of notes you how and why programmers can define new types.
P Pathophysiology Calendar. SundayMondayTuesdayWednesdayThursdayFridaySaturday January 2012.
 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.
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.
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.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
2007 Monthly Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
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.
Subroutines in Computer Programming Svetlin Nakov Telerik Corporation
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.
1 CMPE 104 Pascal (Modified slides of Tom Rethard)
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.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
ISBN Chapter 8 Statement-Level Control Structures.
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.
Programming, an introduction to Pascal
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.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
Oberon Oral Exam English. index The creator The history The use The syntax An example Conclusion.
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.
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
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.
Introduction to C# By: Abir Ghattas Michel Barakat.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
COMP Loop Statements Yi Hong May 21, 2015.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
structured statically typed imperative wide-spectrum object-oriented high-level computer programming language extended from Pascal and other languages.
Written by Al.So. Software solutions
Pascal Winter 2010/ Course.
A Very Brief Overview of Pascal
Dictation practice 2nd Form Ms. Micaela-Ms. Verónica.
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
2300 (11PM) September 21 Blue line is meridian..
McDonald’s calendar 2007.
MONTHS OF THE YEAR January February April March June May July August
McDonald’s calendar 2007.
Habitat Changes and Fish Migration
2015 January February March April May June July August September
Habitat Changes and Fish Migration
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

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

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

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;

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

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 20

... 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” 21

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

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