SlideShare a Scribd company logo
James Tam
Getting Started With Pascal
Programming
How are computer programs created
What is the basic structure of a Pascal Program
Variables in Pascal
Performing input and output with Pascal
Common programming errors
Useful mathematical functions in Pascal
James Tam
What You Know: A Model For Creating Computer
Software
Specify the problem
Develop a design
Implement the design
Maintain the design
James Tam
Implement The Design
Involves the writing of the program in a programming language
e.g., Pascal, Java, C, C++ etc. etc.
This program must then be translated into a machine
understandable form.
James Tam
Translators
Convert computer programs to machine language
Types
1) Interpreters
• Translate the program as it's executed.
2) Compilers
• Translate the program before it's executed.
James Tam
Compiling Programs: Basic View
anything.p
Pascal
program
gpc
Pascal
compiler
input a.out
Machine
language
program
output
James Tam
Compiling Programs On Different Operating
Systems
Pascal
program
Solaris
compiler
a.out (Solaris)
AmigaDOS
compiler
a.out (AmigaDOS)
Windows
compiler
a.out (Windows)
James Tam
Basic Structure Of Pascal Programs
Program documentation
Program name (input and output operations);
Header
const
var
:
Declaration
s
begin
:
end.
Statements
James Tam
Details Of The Parts Of A Pascal Program
Headers
• Program documentation
- Version number, date of last modification, what does the program do etc.
- Comments for the reader of the program (and not the computer)
(* Marks the beginning of the documentation
*) Marks the end of the documentation
• Program heading
- Name of program, input and/or output operations performed by the program
• Example
(*
* Tax-It v1.0: This program will electronically calculate your tax return.
*)
program taxIt (input, output);
James Tam
Details Of The Parts Of A Pascal Program (2)
Declarations
• List of constants and variables
• More to come later in this section of notes
Statements
• The instructions in the program that actually gets stuff done
• They tell the computer what to do as the program is running
• Each statement is separated by a semicolon ";"
• Much more to come later in the course
James Tam
The Smallest Pascal Program
(*
* Smallest.p:
* The smallest Pascal program that will still compile written by James Tam
* v1.0.
*)
program smallest;
begin
end.
Note: The name "smallest" should match the filename "smallest.p". You can find an online version
of this program in the Unix file system under /home/231/examples/intro/smallest.p (the compiled
version is called "smallest").
James Tam
Creating And Compiling Programs: On The
Computer Science Network
anything.p
(Unix file)
Pascal
program
Flowchart or
pseudo-code
(on paper)
High level
algorithm
XEmacs
Text editor
gpc
Pascal
compiler
Machine language
program
a.out (Unix
file)
Hand-written or hand-drawn
To begin creating the Pascal program, in
Unix type "XEmacs filename.p"
To compile the program, in Unix
type "gpc filename.p"
To run the program, in Unix
type "./a.out"
James Tam
Variables
Set aside a location in memory
Used to store information (temporary)
Types:
• integer – whole numbers
• real – whole numbers and fractions
- Can't start or end with a decimal
• char – alphabetic, numeric and miscellaneous symbols
• boolean – true or false values
Usage:
• Declaration
• Accessing or assigning values to the variables
James Tam
Declaring Variables
Sets aside memory
Memory locations addressed through the name
Naming conventions
• Should be meaningful
• Any combination of letters, numbers or underscore (can't begin with a number and
shouldn't begin with an underscore)
• Can't be a reserved word e.g., program, begin, end (see Appendix B)
• Avoid using words with an existing meaning e.g., integer, real, boolean, write, writeln,
read, readln
• Avoid distinguishing variable names only by case
• For variable names composed of multiple words separate each word by capitalizing the
first letter of each word (save for the first word) or by using an underscore.
• Okay:
- tax_rate
- firstName
• Not Okay
- 1abc
- x
- test.msg
- good-day
James Tam
Declaring Variables (2)
Occurs in the variable declaration ("var") section
i.e.,
var
name of first variable, name of second variable…: type of variables;
e.g.,
var
height, weight: real;
age: integer;
James Tam
Accessing And Assigning Values To Variables
Accessing
•Can be done by referring to the name of the variable
•Syntax:
name
•Example:
num
James Tam
Accessing And Assigning Values To Variables (2)
Assignment
• Performed via the assignment operator :=
• Usage:
- Destination := Source;1
• Example:
- x := 5;
- x:= y;
- interest := principle * rate;
- character := 'a';
• Avoid assigning mixed types
e.g.,
var
num1: integer;
num2: real;
begin
num1 = 12;
num2 = 12.5;
num2 := num1;
1 The source can be any expression (constant, variable or formula)
num1 := num2;
Not allowed!
James Tam
Named Constants
A memory location that is assigned a value that cannot be changed
Occurs in the constant declaration ("const") section
The naming conventions for choosing variable names also applies
to constants but constants should be all UPPER CASE.
Syntax:
const
NAME OF FIRST CONSTANT = value of first constant;
NAME OF SECOND CONSTANT = value of second constant;
etc.
James Tam
Named Constants (2)
Examples:
const
TAXRATE = 0.25;
SAMPLESIZE = 1000;
YES = True;
NO = False;
James Tam
Purpose of Named Constants
1) Makes the program easier to understand
e.g.,
begin
population_change := (0.1758 – 0.1257) * current_population;
Vs.
const
BIRTHRATE = 0.1758;
DEATHRATE = 0.1257;
begin
population_change := (BIRTHRATE - DEATHRATE) * current_population;
Magic Numbers
(avoid!)
James Tam
Purpose of Named Constants
2) Makes the program easier to maintain
If the constant is referred to several times throughout the
program.
const
BIRTHRATE = 0.1758;
DEATHRATE = 0.1257;
begin
BIRTHRATE
BIRTHRATE
DEATHRATE DEATHRATE BIRTHRATE
BIRTHRATE BIRTHRATE
BIRTHRATE
James Tam
Output
Displaying information onscreen
Done via the write and writeln statements
Syntax (either write or writeln):
write ('text message');
or
writeln('text message');
write(name of variable or constant);
or
writeln (name of variable or constant);
write('message', name of variable, 'message'…);
or
writeln('message', name of variable, 'message'…);
James Tam
Output (2)
Examples:
var
num : integer;
begin
num := 10;
writeln('line1');
write('line2A');
writeln('line2B');
writeln(num);
writeln('num=',num);
James Tam
Formatting Output
Automatic formatting of output
•Field width: The computer will insert enough spaces to ensure
that the information can be displayed.
•Decimal places: For real numbers the data will be displayed in
exponential form.
Manually formatting of output:
Syntax:
• write or writeln (data: Field width for data: Number decimal places for
data);
James Tam
Formatting Output (2)
Examples
var
num : real;
begin
num := 12.34;
writeln(num);
writeln(num:5:2);
James Tam
Formatting Output (3)
If the field width doesn’t match the actual size of the field
• Field width too small – extra spaces will be added for numerical variables
but not for other types of data.
• Examples:
num := 123456;
writeln(num:3);
writeln('123456':3);
• Field width too large – the data will be right justified (extra spaces will
be put in front of the data).
• Examples:
num := 123;
writeln(num:6);
Writeln('123':6);
James Tam
Formatting Output (4)
If the number of decimal places doesn’t match the actual number
of decimal places.
• Set number of decimal places less than the actual number of decimal places
– number will be rounded up.
• Example:
num1 := 123.4567
writeln (num1:6:2);
• Set number of decimal places greater than the actual number of decimal
places – number will be padded with zeros.
• Example:
num1 := 123.4567;
writeln(num1:6:6);
James Tam
Formatting Output: A Larger Example
For the complete program and executable look under
/home/231/examples/intro/out1.p (out1 for the compiled version)
program out1;
var
num1 : integer;
num2 : real;
begin
num1 := 123;
num2 := 123.456;
writeln('Auto formatted by Pascal', num1, num2);
writeln('Manual format':13, num1:3, num2:7:3);
writeln('Manual not enough':13, num1:2, num2:6:3);
writeln('Manual too much':16, num1:4, num2:8:4);
end.
James Tam
Input
The computer program getting information from the user
Done via the read and readln statements
Syntax:
(single input)
read (name of variable);
or
readln (name of variable);
(multiple inputs)
read (nv1, nv2…);
or
readln (nv2, nv3…);
James Tam
Input (2)
Examples:
var
num1, num2 : integer
begin
read (num1);
read (num2);
read (num1, num2);
James Tam
Input: Read Vs. Readln
Both:
• Reads each value inputted and matches it to the corresponding variable.
Read
• If the user inputs additional values they will remain
Readln
• Any additional values inputted will be discarded
James Tam
Input: Read Vs. Readln (An example)
For the complete version of this program look in Unix under:
/home/231/examples/intro/read1.p (or read1 and read2 for the compiled
version)
e.g., read1.p
write('Input some integers making sure to separate each one with a space ');
write('or a new line: ');
read (num1, num2);
write('Input some integers making sure to separate each one with a space ');
write('or a newline: ');
read(num3, num4);
James Tam
Input: Read Vs. Readln (An example (2))
For the complete version of this program look in Unix under:
/home/231/examples/intro/read2.p (or read2 for the compiled version)
e.g., read2.p
write('Input some integers making sure to separate each one with a space ');
write('or a newline: ');
readln (num1, num2);
write('Input some integers making sure to separate each one with a space ');
write('or a newline: ');
readln(num3, num4);
James Tam
Extra Uses Of Readln
To filter out extraneous input
As an input prompt
e.g.,
writeln('To continue press return');
readln;
James Tam
Common Programming Errors
Syntax/compile errors
Runtime errors
Logic errors
James Tam
Syntax/Compile Errors
anything.p
(Unix file)
Pascal
program
Flowchart or
pseudo-code
(on paper)
High level
algorithm
XEmacs
Text editor
gpc
Pascal
compiler
Syntax error:
No executable
(a.out) produced.
James Tam
Runtime Errors
anything.p
(Unix file)
Pascal
program
Flowchart or
pseudo-code
(on paper)
High level
algorithm
XEmacs
Text editor
gpc
Pascal
compiler
Machine language
program
a.out (Unix
file)
Executing a.out
Runtime error
(execution stops)
James Tam
Logic Errors
anything.p
(Unix file)
Pascal
program
Flowchart or
pseudo-code
(on paper)
High level
algorithm
XEmacs
Text editor
gpc
Pascal
compiler
Machine language
program
a.out (Unix
file)
Executing a.out
Program finishes
execution but may
result in incorrect
result
James Tam
Some Useful Functions
See also Appendix D in Pascal Programming and Problem
Solving by Leestma S. and Nyhoff L.
Name Description Input type Type of result Example
abs absolute value integer integer abs(-2) = 2
real real abs(-2.2) = 2.2
round rounding real integer round(2.6) = 3
trunc truncation real integer trunc(2.6) = 2
sqr squaring integer integer sqr(2) = 4
real real sqr(1.1) = 1.21
sqrt square root integer real sqrt(4) = 2.00
or
real
James Tam
Summary
What is involved in creating and running computer programs
• Typing in programs with a text editor
• Translating the computer program into machine language
What are the fundamental parts of a Pascal program
What are the basic types of variables employed in Pascal and how are they
used
How to output information with the write and writeln statements
Getting information from the user through the read and readln statements
What are the different types of programming errors
How are some common mathematical functions performed in Pascal

More Related Content

PPT
Programming For As Comp
PPT
Programming For As Comp
PPTX
Intro To C++ - Class 3 - Sample Program
PPTX
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part II
PPTX
Matlab-3.pptx
PPTX
INTRODUCCIÓN A LA PROGRAMACIÓN
DOCX
CS150 Assignment 7 Cryptography Date assigned Monday.docx
PPTX
cmp104 lec 8
Programming For As Comp
Programming For As Comp
Intro To C++ - Class 3 - Sample Program
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part II
Matlab-3.pptx
INTRODUCCIÓN A LA PROGRAMACIÓN
CS150 Assignment 7 Cryptography Date assigned Monday.docx
cmp104 lec 8

Similar to starting_pascal_programming_for_beginner (20)

PPT
Fundamental of C Programming Language and Basic Input/Output Function
PPTX
Python Training in Bangalore | Python Introduction Session | Learnbay
PPT
keyword
PPT
keyword
PDF
C notes.pdf
PPT
03-fortran.ppt
PPTX
Microprocessor chapter 9 - assembly language programming
PDF
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
PPTX
Assembly Language Programming
DOCX
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
PPTX
C++ lecture 01
PPT
Basic concept of MATLAB.ppt
PPT
Unit 1 c - all topics
DOCX
Final Project SkeletonCipherClient.javaFinal Project SkeletonC.docx
PPTX
Lecture 3 Perl & FreeBSD administration
PPT
Unix Lec2
PPTX
Input-output
PPTX
C programming language tutorial
PPTX
Lab 01hhkhkhkhkhkhkhkhkhjjjkhkkhhhhh.pptx
Fundamental of C Programming Language and Basic Input/Output Function
Python Training in Bangalore | Python Introduction Session | Learnbay
keyword
keyword
C notes.pdf
03-fortran.ppt
Microprocessor chapter 9 - assembly language programming
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
Assembly Language Programming
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
C++ lecture 01
Basic concept of MATLAB.ppt
Unit 1 c - all topics
Final Project SkeletonCipherClient.javaFinal Project SkeletonC.docx
Lecture 3 Perl & FreeBSD administration
Unix Lec2
Input-output
C programming language tutorial
Lab 01hhkhkhkhkhkhkhkhkhjjjkhkkhhhhh.pptx
Ad

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Complications of Minimal Access Surgery at WLH
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
master seminar digital applications in india
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Institutional Correction lecture only . . .
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Anesthesia in Laparoscopic Surgery in India
VCE English Exam - Section C Student Revision Booklet
A systematic review of self-coping strategies used by university students to ...
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Final Presentation General Medicine 03-08-2024.pptx
Complications of Minimal Access Surgery at WLH
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pharma ospi slides which help in ospi learning
Supply Chain Operations Speaking Notes -ICLT Program
master seminar digital applications in india
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Institutional Correction lecture only . . .
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
202450812 BayCHI UCSC-SV 20250812 v17.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Ad

starting_pascal_programming_for_beginner

  • 1. James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with Pascal Common programming errors Useful mathematical functions in Pascal
  • 2. James Tam What You Know: A Model For Creating Computer Software Specify the problem Develop a design Implement the design Maintain the design
  • 3. James Tam Implement The Design Involves the writing of the program in a programming language e.g., Pascal, Java, C, C++ etc. etc. This program must then be translated into a machine understandable form.
  • 4. James Tam Translators Convert computer programs to machine language Types 1) Interpreters • Translate the program as it's executed. 2) Compilers • Translate the program before it's executed.
  • 5. James Tam Compiling Programs: Basic View anything.p Pascal program gpc Pascal compiler input a.out Machine language program output
  • 6. James Tam Compiling Programs On Different Operating Systems Pascal program Solaris compiler a.out (Solaris) AmigaDOS compiler a.out (AmigaDOS) Windows compiler a.out (Windows)
  • 7. James Tam Basic Structure Of Pascal Programs Program documentation Program name (input and output operations); Header const var : Declaration s begin : end. Statements
  • 8. James Tam Details Of The Parts Of A Pascal Program Headers • Program documentation - Version number, date of last modification, what does the program do etc. - Comments for the reader of the program (and not the computer) (* Marks the beginning of the documentation *) Marks the end of the documentation • Program heading - Name of program, input and/or output operations performed by the program • Example (* * Tax-It v1.0: This program will electronically calculate your tax return. *) program taxIt (input, output);
  • 9. James Tam Details Of The Parts Of A Pascal Program (2) Declarations • List of constants and variables • More to come later in this section of notes Statements • The instructions in the program that actually gets stuff done • They tell the computer what to do as the program is running • Each statement is separated by a semicolon ";" • Much more to come later in the course
  • 10. James Tam The Smallest Pascal Program (* * Smallest.p: * The smallest Pascal program that will still compile written by James Tam * v1.0. *) program smallest; begin end. Note: The name "smallest" should match the filename "smallest.p". You can find an online version of this program in the Unix file system under /home/231/examples/intro/smallest.p (the compiled version is called "smallest").
  • 11. James Tam Creating And Compiling Programs: On The Computer Science Network anything.p (Unix file) Pascal program Flowchart or pseudo-code (on paper) High level algorithm XEmacs Text editor gpc Pascal compiler Machine language program a.out (Unix file) Hand-written or hand-drawn To begin creating the Pascal program, in Unix type "XEmacs filename.p" To compile the program, in Unix type "gpc filename.p" To run the program, in Unix type "./a.out"
  • 12. James Tam Variables Set aside a location in memory Used to store information (temporary) Types: • integer – whole numbers • real – whole numbers and fractions - Can't start or end with a decimal • char – alphabetic, numeric and miscellaneous symbols • boolean – true or false values Usage: • Declaration • Accessing or assigning values to the variables
  • 13. James Tam Declaring Variables Sets aside memory Memory locations addressed through the name Naming conventions • Should be meaningful • Any combination of letters, numbers or underscore (can't begin with a number and shouldn't begin with an underscore) • Can't be a reserved word e.g., program, begin, end (see Appendix B) • Avoid using words with an existing meaning e.g., integer, real, boolean, write, writeln, read, readln • Avoid distinguishing variable names only by case • For variable names composed of multiple words separate each word by capitalizing the first letter of each word (save for the first word) or by using an underscore. • Okay: - tax_rate - firstName • Not Okay - 1abc - x - test.msg - good-day
  • 14. James Tam Declaring Variables (2) Occurs in the variable declaration ("var") section i.e., var name of first variable, name of second variable…: type of variables; e.g., var height, weight: real; age: integer;
  • 15. James Tam Accessing And Assigning Values To Variables Accessing •Can be done by referring to the name of the variable •Syntax: name •Example: num
  • 16. James Tam Accessing And Assigning Values To Variables (2) Assignment • Performed via the assignment operator := • Usage: - Destination := Source;1 • Example: - x := 5; - x:= y; - interest := principle * rate; - character := 'a'; • Avoid assigning mixed types e.g., var num1: integer; num2: real; begin num1 = 12; num2 = 12.5; num2 := num1; 1 The source can be any expression (constant, variable or formula) num1 := num2; Not allowed!
  • 17. James Tam Named Constants A memory location that is assigned a value that cannot be changed Occurs in the constant declaration ("const") section The naming conventions for choosing variable names also applies to constants but constants should be all UPPER CASE. Syntax: const NAME OF FIRST CONSTANT = value of first constant; NAME OF SECOND CONSTANT = value of second constant; etc.
  • 18. James Tam Named Constants (2) Examples: const TAXRATE = 0.25; SAMPLESIZE = 1000; YES = True; NO = False;
  • 19. James Tam Purpose of Named Constants 1) Makes the program easier to understand e.g., begin population_change := (0.1758 – 0.1257) * current_population; Vs. const BIRTHRATE = 0.1758; DEATHRATE = 0.1257; begin population_change := (BIRTHRATE - DEATHRATE) * current_population; Magic Numbers (avoid!)
  • 20. James Tam Purpose of Named Constants 2) Makes the program easier to maintain If the constant is referred to several times throughout the program. const BIRTHRATE = 0.1758; DEATHRATE = 0.1257; begin BIRTHRATE BIRTHRATE DEATHRATE DEATHRATE BIRTHRATE BIRTHRATE BIRTHRATE BIRTHRATE
  • 21. James Tam Output Displaying information onscreen Done via the write and writeln statements Syntax (either write or writeln): write ('text message'); or writeln('text message'); write(name of variable or constant); or writeln (name of variable or constant); write('message', name of variable, 'message'…); or writeln('message', name of variable, 'message'…);
  • 22. James Tam Output (2) Examples: var num : integer; begin num := 10; writeln('line1'); write('line2A'); writeln('line2B'); writeln(num); writeln('num=',num);
  • 23. James Tam Formatting Output Automatic formatting of output •Field width: The computer will insert enough spaces to ensure that the information can be displayed. •Decimal places: For real numbers the data will be displayed in exponential form. Manually formatting of output: Syntax: • write or writeln (data: Field width for data: Number decimal places for data);
  • 24. James Tam Formatting Output (2) Examples var num : real; begin num := 12.34; writeln(num); writeln(num:5:2);
  • 25. James Tam Formatting Output (3) If the field width doesn’t match the actual size of the field • Field width too small – extra spaces will be added for numerical variables but not for other types of data. • Examples: num := 123456; writeln(num:3); writeln('123456':3); • Field width too large – the data will be right justified (extra spaces will be put in front of the data). • Examples: num := 123; writeln(num:6); Writeln('123':6);
  • 26. James Tam Formatting Output (4) If the number of decimal places doesn’t match the actual number of decimal places. • Set number of decimal places less than the actual number of decimal places – number will be rounded up. • Example: num1 := 123.4567 writeln (num1:6:2); • Set number of decimal places greater than the actual number of decimal places – number will be padded with zeros. • Example: num1 := 123.4567; writeln(num1:6:6);
  • 27. James Tam Formatting Output: A Larger Example For the complete program and executable look under /home/231/examples/intro/out1.p (out1 for the compiled version) program out1; var num1 : integer; num2 : real; begin num1 := 123; num2 := 123.456; writeln('Auto formatted by Pascal', num1, num2); writeln('Manual format':13, num1:3, num2:7:3); writeln('Manual not enough':13, num1:2, num2:6:3); writeln('Manual too much':16, num1:4, num2:8:4); end.
  • 28. James Tam Input The computer program getting information from the user Done via the read and readln statements Syntax: (single input) read (name of variable); or readln (name of variable); (multiple inputs) read (nv1, nv2…); or readln (nv2, nv3…);
  • 29. James Tam Input (2) Examples: var num1, num2 : integer begin read (num1); read (num2); read (num1, num2);
  • 30. James Tam Input: Read Vs. Readln Both: • Reads each value inputted and matches it to the corresponding variable. Read • If the user inputs additional values they will remain Readln • Any additional values inputted will be discarded
  • 31. James Tam Input: Read Vs. Readln (An example) For the complete version of this program look in Unix under: /home/231/examples/intro/read1.p (or read1 and read2 for the compiled version) e.g., read1.p write('Input some integers making sure to separate each one with a space '); write('or a new line: '); read (num1, num2); write('Input some integers making sure to separate each one with a space '); write('or a newline: '); read(num3, num4);
  • 32. James Tam Input: Read Vs. Readln (An example (2)) For the complete version of this program look in Unix under: /home/231/examples/intro/read2.p (or read2 for the compiled version) e.g., read2.p write('Input some integers making sure to separate each one with a space '); write('or a newline: '); readln (num1, num2); write('Input some integers making sure to separate each one with a space '); write('or a newline: '); readln(num3, num4);
  • 33. James Tam Extra Uses Of Readln To filter out extraneous input As an input prompt e.g., writeln('To continue press return'); readln;
  • 34. James Tam Common Programming Errors Syntax/compile errors Runtime errors Logic errors
  • 35. James Tam Syntax/Compile Errors anything.p (Unix file) Pascal program Flowchart or pseudo-code (on paper) High level algorithm XEmacs Text editor gpc Pascal compiler Syntax error: No executable (a.out) produced.
  • 36. James Tam Runtime Errors anything.p (Unix file) Pascal program Flowchart or pseudo-code (on paper) High level algorithm XEmacs Text editor gpc Pascal compiler Machine language program a.out (Unix file) Executing a.out Runtime error (execution stops)
  • 37. James Tam Logic Errors anything.p (Unix file) Pascal program Flowchart or pseudo-code (on paper) High level algorithm XEmacs Text editor gpc Pascal compiler Machine language program a.out (Unix file) Executing a.out Program finishes execution but may result in incorrect result
  • 38. James Tam Some Useful Functions See also Appendix D in Pascal Programming and Problem Solving by Leestma S. and Nyhoff L. Name Description Input type Type of result Example abs absolute value integer integer abs(-2) = 2 real real abs(-2.2) = 2.2 round rounding real integer round(2.6) = 3 trunc truncation real integer trunc(2.6) = 2 sqr squaring integer integer sqr(2) = 4 real real sqr(1.1) = 1.21 sqrt square root integer real sqrt(4) = 2.00 or real
  • 39. James Tam Summary What is involved in creating and running computer programs • Typing in programs with a text editor • Translating the computer program into machine language What are the fundamental parts of a Pascal program What are the basic types of variables employed in Pascal and how are they used How to output information with the write and writeln statements Getting information from the user through the read and readln statements What are the different types of programming errors How are some common mathematical functions performed in Pascal

Editor's Notes

  • #12: Real – complete – must include a fractional and a whole number portion e.g., 82.1 not 82.
  • #22: line1 line2Aline2B 10 num=10
  • #24: 1.234000000000000e+01 12.34
  • #25: Eg1., 123456 Eg2., 123 Eg3.,---123 Eg4.,---123
  • #26: e.g1., 123.46 e.g2., 123.456700
  • #27: Auto formatted by Pascal123 1.234560000000000e+02 Manual format123123.456 Manual not en123123.456 Manual too much 123123.4560
  • #31: 1 2 3 4
  • #32: 1 2 3 4 5 6