Prepared by Michel Barakat 1. Preface  A computer is nothing but a very dumb machine that has the ability to perform mathematical operations very rapidly.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Intermediate Code Generation
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 10.
1 CIS 205 Practice Test George Lamperti A word that has a predefined meaning in a C++ program and cannot be used as a variable name is known as.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Introduction to a Programming Environment
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
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.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Lists in Python.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Chapter 8: Arrays.
INFORMATION TECHNOLOGY CSEC CXC 10/25/ PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Basic Program Construction
I Power Higher Computing Software Development High Level Language Constructs.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Pointers *, &, array similarities, functions, sizeof.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Strings Chapter 5.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Programming Fundamentals Enumerations and Functions.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Windows Programming Lecture 03. Pointers and Arrays.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Bill Tucker Austin Community College COSC 1315
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Definition of the Programming Language CPRL
User-Written Functions
Introduction to the C Language
Java Primer 1: Types, Classes and Operators
Introduction to C++.
Arrays, For loop While loop Do while loop
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Introduction to Primitive Data types
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Coding Concepts (Basics)
Chapter 2: Introduction to C++.
Classes, Objects and Methods
Variables in C Topics Naming Variables Declaring Variables
Introduction to Primitive Data types
Presentation transcript:

Prepared by Michel Barakat 1

Preface  A computer is nothing but a very dumb machine that has the ability to perform mathematical operations very rapidly and very accurately, but it can do nothing without the aid of a program written by a human being. Moreover, if the human being writes a program that turns good data into garbage, the computer will very obediently, and very rapidly, turn the good data into garbage. 2

WHAT IS SO GOOD ABOUT PASCAL?  Some computer languages allow the programmer to define constants and variables in a very random manner and then combine data in an even messier manner.  Some programming languages would allow you to do just such an addition and obediently print out the meaningless answer.  Since Pascal requires you to set up your constants and variables in a very precise manner, the possibility of such a meaningless answer is minimized. 3

How to write using PASCAL?  The first line is required in the standard Pascal definition and contains the program name which can be any name you like.  The first word program is the first of the reserved words mentioned earlier and it is the indicator to the Pascal compiler that this is the name of the program.  The second word is the program name itself. It could be anything the programmer desires.  Most of the statements in PASCAL end with a semicolon “;” 4

Identifiers  The identifiers in PASCAL are: Program name Procedure and functions names Type definitions Constants and variables names  Any combination of alphabetic and numeric characters with no blanks embedded can be used as a name for the identifier, however, the identifier must always start with an alphabetic character.  Upper and lower case characters may be mixed at will. 5

The words “begin” and “end”  We saw the reserved word program at the beginning of this tutorial. The two next reserved words are begin and end  These words are used mainly for bracketing. So just like we use “{“ and “}” in JAVA, begin and end are the signal for the compiler in order to know where the operation is starting and ending.  Moreover, in PASCAL, we use the period following the word end (i.e. end.) to let the compiler know that it has reached the end of executable statements and therefore it has to finish compiling.  Every Pascal program will have one, and only one period in it, and that one period will be at the end of the program. 6

The word “Writeln”  The special word Writeln is not a reserved word but is defined by the system to do a very special job for you, namely to output a line of data to the monitor. Writeln('bla bla'); 7

The word “Write”  Same as “Writeln”, the word “Write” is not a reserved word.  It is designed to output something on the monitor without returning any carriage to the first character of the next line. Write(‘bla bla'); 8

Comments  Professional programmers uses lots of commented programs in order to explain the semantics of the code.  To comment in PASCAL, we use either the (* comments inside *) or the { comments inside }  Some compilers allow to do the following (* comments } or { comments*) but it is considered as one of the bad programming habits. 9

Simple Data Types  PASCAL introduces us to 8 different data types integer (Whole numbers from to 32767) byte (The integers from 0 to 255 ) Real (Floating point numbers from 1E-38 to 1E+38) Boolean (Can only have the value TRUE or FALSE) Char (Any character in the ASCII character set) Shortint (The integers from -128 to 127) Word (The integers from 0 to 65535) Longint (The integers from to ) 10

The word var  The reserved word var is used to define a variable before it can be used anywhere in the program.  For example: var Count : integer; Total : integer; Alphabet : char; 11

Basic arithmetical operations  Just like any other programming language, the mathematical operators are the same except for the quality sign. In PASCAL, instead of “=“, we use the “:=“ sign.  So the operations X:=X+1 would be read as X gets the value of X plus 1. 12

Relational operators  Relational operators are those to compare any two variables of same type in the program and return the result as a boolean TRUE or FALSE  PASCAL’s relational operators list: = (equal to) <> (not equal to) > (greater than) < (less than) >= (greater than or equal to) <= (less than or equal to) 13

Boolean variables  The boolean variable is only allowed to take on two values which are TRUE and FALSE.  We use boolean variables for loop controls, end of file indicators, or any other TRUE or FALSE conditions in the program. 14

The words “or” and “and”  Mainly, we use those two reserved words for handling Booleans. It’s either using two boolean expressions with the word and or with the word or 15 andTRUEFALSE TRUE FALSE orTRUEFALSE TRUE FALSETRUEFALSE

Loops and control structures  The for Loop This is used to repeat a single statement any number of times we desire. Any simple variable of type integer, byte, or char can be used for the loop index but due to the requirement that everything must be defined prior to use in Pascal, the loop index must be defined in a var statement. To increment the index, we use the reserved word to. i.e. for Count := 1 to 10 do {this will be the header of a for loop} As for decrementing, we use the reserved word downto. i.e. for Count := 7 downto 2 do Following the word do will be a: ○ Single statement that will be executed or a ○ Compound statement that have to be bracketed with the begin end pair 16

The if statement  Any condition that can be reduced to a Boolean answer is put between the if then pair of words.  If the resulting expression resolves to TRUE, then the single Pascal statement following the reserved word then is executed if it resolves to FALSE, then the single statement is skipped over.  if it resolves to FALSE, then the single statement is skipped over.  As for a compound statement, it have to be bracketed with a begin end pair 17

The if then else block  The if then else construct is one of the most used, most useful, and therefore most important aspects of Pascal. And it goes as follows: if Two = 2 then Writeln(‘bla bla') else Writeln(‘blaaaaaaaaaaa'); 18

The repeat until loop  Two more reserved words are defined here, namely repeat and until. And it goes like: var Count : integer; Count := 4; repeat Write('This is in the '); Write('repeat loop, and '); Write('the index is',Count:4); Writeln; Count := Count + 2; until Count = 20; 19

The while loop  This uses the while do reserved words and will execute one Pascal statement (or one compound statement bounded with begin and end) continuously until the boolean expression between the two words becomes FALSE. for example: var Counter : integer; begin Counter := 4; while Counter < 20 do begin Write('In the while loop, waiting '); Write('for the counter to reach 20. It is',Counter:4); Writeln; Counter := Counter + 2; end; end. 20

Procedures  Procedures are like methods in JAVA except for that we can define them as simple as the following: procedure Write_My_Name; begin Writeln('This is my name: Michel'); end;  As for calling a procedure, we simply need to state its name. 21

 Procedures can also be called by reference or by value. But they have to be first defined in a way to be called in a such way. i.e. By value: var Index : integer; procedure Print_Data_Out(Puppy : integer); begin Writeln('This is the print routine',Puppy:5); Puppy := 12; end; And to call this procedure: Print_Data_Out(Index); { since there is no var declaration for the parameter in the header of the procedure, the system makes a copy of Index, and passes the copy to the procedure which can do anything with it, using its new name, Puppy, but when control returns to the main program, the original value of Index is still there. } 22

By reference: var Index : integer; procedure Print_And_Modify(var Cat : integer); begin Writeln('This is the print and modify routine',Cat:5); Cat := 35; end; And to call this procedure: Print_And_Modify(Index); { The formal parameter name, Cat in the procedure, is actually another name for the actual variable named Index in the main program. A passed parameter with a var in front of it is therefore a two way situation. This is a "call by reference" since a reference to the original variable is passed to the procedure. } 23

Formal and actual parameters  The parameters listed within the parentheses of the procedure header are called the formal parameters, whereas the parameters listed within the parentheses of the procedure call in the main program are referred to as the actual parameters. 24

By reference or by value???  It may seem to you that it would be a good idea to simply put the word var in front of every formal parameter in every procedure header to gain maximum flexibility, but using all "call by references" could actually limit the flexibility. There are two reasons to use "call by value" variables when we can. The first is simply to protect some data from being corrupted by the procedure. This is becoming a very important topic in Software Engineering known as "information hiding" and is the primary basis behind Object Oriented Programming. Secondly is the ability to use a constant in the procedure call. 25

Procedures can always call other procedures in their definitions. The silliest example is calling the writeline procedure when defining some random procedure. 26

Functions  The major difference between a function and a procedure is that the function returns a single value and is called from within a mathematical expression, a Writeln command, or anywhere that it is valid to use a variable, since it is really a variable itself.  A function begins with the reserved word function. Let’s consider the following function: function Quad_Of_Sum(Number1,Number2 : integer) : integer; begin Quad_Of_Sum := 4*(Number1 + Number2); end; { there are two input variables inside the parenthesis pair being defined as integer variables, and following the parenthesis is a colon and another integer. The last integer is used to define the type of the variable being returned to the main program. } 27

Recursion  Recursion is also a technique allowed in Pascal. And here is an example: var Count : integer; procedure Print_And_Decrement(Index : integer); begin Writeln('The value of the index is ',Index:3); Index := Index - 1; if Index > 0 then Print_And_Decrement(Index); {recursive part} end; 28

Arrays  The square braces are used in Pascal to denote a subscript for an array variable.  The array definition is standard, namely a variable name, followed by a colon and the reserved word array, with the range of the array given in square brackets followed by another reserved word of and finally the type of variable for each element of the array.  To define an array, we do the following: var Automobiles : array[1..12] of integer; The variable Automobiles is defined as an integer variable but in addition, it is defined to have twelve different integer variables, namely Automobile[1], Automobile[2], Automobile[3],.. Automobile[12]. 29

 var index : integer; index := 1 Automobiles[Index] := Index + 10; this is how we deal with the data stored in the array. In this case, the array automobiles at index 1 holds the value

DOUBLY INDEXED ARRAYS  A doubly indexed array is defined as an array from 1 to n, but instead of it being a simple data type, it is itself another array from 1 to m of type integer. the following code represents a doubly indexed array: It’s either: var Checkerboard : array[1..8] of array[1..8] of integer; Or: var Checkboard : array[1..8,1..8] of integer; 31

The label declaration  In the Pascal definition, a label is a number from 0 to 9999 that is used to define a point in the program to which you wish to jump. All labels must be defined in the label definition part of the program before they can be used. Then a new reserved word goto is used to jump to that point in the program. Let us look at a demonstration in a program and it’s output in the next slides. 32

label 274,Repeat_Loop,Help,Dog; var Counter : byte; (* This limits us to a maximum of 255 *) begin Writeln('Start here and go to "help"'); goto Help; Dog: Writeln('Now go and end this silly program'); goto 274; Repeat_Loop: for Counter := 1 to 4 do Writeln('In the repeat loop'); goto Dog; Help: Writeln('This is the help section that does nothing'); goto Repeat_Loop; 274: Writeln('This is the end of this spaghetti code'); end. 33

The output will be as the following: Start here and go to "help" This is the help section that does nothing In the repeat loop Now go and end this silly program This is the end of this spaghetti code 34

strings  According to the Pascal definition, a string is simply an array of 2 of more elements of type char, and is contained in an array defined in a var declaration as a fixed length. the strings are defined as types in a type declaration even though they could have been completely defined in a var part of the declaration. the following code illustrates the way to define strings: 35

type Long_String = array[1..25] of char; String10 = array[1..10] of char; String12 = array[1..12] of char; var First_Name : String10; Last_Name : String12; Full_Name : Long_String; To assign a string to First_Name: First_Name := 'John '; {6 spaces are must be added after the name John in order to complete the array chosen for First_Name which is String10 in this case.} 36

Scalars  A scalar, also called an enumerated type, is a list of values which a variable of that type may assume. Look at the example below: type Days = (Mon,Tue,Wed,Thu,Fri,Sat,Sun); Time_Of_Day = (Morning,Afternoon,Evening,Night); var Day : Days; Time : Time_Of_Day; 37

Sets  Consider the following code: type Goodies = (Ice_Cream,Whipped_Cream,Banana,Nuts,Cherry, Choc_Syrup,Strawberries,Caramel,Soda_Water, Salt,Pepper,Cone,Straw,Spoon,Stick); Treat = set of Goodies; var Sundae : Treat; Banana_Split : Treat; Soda : Treat;  A scalar variable is defined first, in this case the scalar type named Goodies. A set is then defined with the reserved words set of followed by a predefined scalar type. Several variables are defined as sets of Treat, after which they can individually be assigned portions of the entire set.  Then we can assign the variables to something from the goodies: Ice_Cream_Cone := [Ice_Cream,Cone]; Soda := [Straw,Soda_Water,Ice_Cream,Cherry]; 38

Records  We come to the grandaddy of all data structures in Pascal, the record. A record is composed of a number of variables, any of which can be of any predefined data type, including other records. the next slide demonstrates how we can use Records. 39

type Description = record Year : integer; Model : string[20]; Engine : string[8]; end; var Truck : Description; Cars : array[1..10] of Description; Index : integer; begin (* main program *) Truck.Year := 1988; Truck.Model := 'Pickup'; Truck.Engine := 'Diesel'; In this example, the record Description holds a Year, a model and an Engine. To close this record, we must type the reserved word end followed by a semicolomn (end;) We then created variables of type Description which is the record To modify a variable which is of type description, we have to state its name.the name of the desired description to be modified. i.e. Truck.Year := 1988; 40

Standard Input/Output  We saw the example of the procedure Writeln as an output Writeln('This is a standard output statement');  As for an input statement which will be used mostly in order to read from the keyboard, we use the Readln or Read procedures and we have to pass to them parameters which will be mainly variables. i.e. Var Number1,Number2,Number3 : integer; Writeln(‘enter 3 numbers’); Read(Number1,Number2,Number3);  The parameter can be of any type. It depends on what is originally declared. 41

File Input/Output  Consider the following: program Read_A_File; var Turkey : text; Big_String : string[80]; begin (* main program *) Assign(Turkey,'READFILE.PAS'); Reset(Turkey); while not Eof(Turkey) do begin Readln(Turkey,Big_String); Writeln(Big_String); end; (* of while loop *) Close(Turkey); end. (* of program *) The Assign statement assigns the input file to the variable Turkey in this example The Reset(Turkey) statement, sets a pointer to the beginning of the file Once we did that, it is simple to read from that file just like we do from the keyboard. 42

The EOF Function  The Eof function is new and must therefore be defined. When we read data from the file, we move closer and closer to the end of the file, until finally we reach the end and there is no more data to read. This is called "end of file" and is abbreviated Eof. Pascal has this function available as a part of the standard library which returns FALSE until we reach the last line of the file. When there is no more data to read left in the file, the function Eof returns TRUE. To use the function, we merely give it our file identifier as an argument. It should be clear to you that we will loop in this program until we read all of the data available in the input file. see previous example 43

The EOLN Function  The Eoln function is not used in this program but is a very useful function. If the input pointer is anywhere in the text file except at the end of a line, the Eoln returns FALSE, but at the end of a line, it returns a value of TRUE. This function can therefore be used to find the end of a line of text for variable length text input. 44

Finally, we Close the file Turkey. It is not really necessary to close the file because the system will close it for us automatically at program termination, but it is a good habit to get into. See the last shown example 45

Encapsulation  Encapsulation is the cornerstone upon which object oriented programming is built, and without which it would not exist. 46

Inheritance Why do we use Inheritance?  Once you have an object that is completely debugged and working, it is possible that it can be reused on your next project. If you cannot use it exactly as is, but are required to make a change to it, you have two choices. You can modify the existing code and hope you don't introduce any analysis or coding errors, or you can inherit it into a new object and add code around the completely debugged object without actually modifying the original code. Several studies have shown that modifying existing code lead to a high probability of introducing errors into the modified code that are difficult to find because you are not as familiar with the code as you should be. Adding a few methods to an existing object, however, is not nearly as error prone and is the preferred method. 47