1 / 39

Getting Started With Pascal Programming

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.

Jimmy
Télécharger la présentation

Getting Started With Pascal Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. What You Know: A Model For Creating Computer Software • Specify the problem • Develop a design • Implement the design • Maintain the design

  3. 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. Translators • Convert computer programs to machine language • Types • Interpreters • Translate the program as it's executed. • Compilers • Translate the program before it's executed.

  5. Machine language program Pascal program Pascal compiler anything.p a.out input output gpc Compiling Programs: Basic View

  6. Solaris compiler a.out (Solaris) Windows compiler a.out (Windows) AmigaDOS compiler a.out (AmigaDOS) Compiling Programs On Different Operating Systems Pascal program

  7. Header Program documentation Program name (input and output operations); Declarations const var : Statements begin : end. Basic Structure Of Pascal Programs

  8. 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. 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. 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. High level algorithm Hand-written or hand-drawn Flowchart or pseudo-code (on paper) To begin creating the Pascal program, in Unix type "XEmacs filename.p" Text editor XEmacs Pascal program anything.p (Unix file) Pascal compiler gpc Machine language program To compile the program, in Unix type "gpc filename.p" a.out (Unix file) To run the program, in Unix type "./a.out" Creating And Compiling Programs: On The Computer Science Network

  12. 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. 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. 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. Accessing And Assigning Values To Variables • Accessing • Can be done by referring to the name of the variable • Syntax: name • Example: num

  16. Not allowed! 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;

  17. 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. Named Constants (2) • Examples: • const • TAXRATE = 0.25; • SAMPLESIZE = 1000; • YES = True; • NO = False;

  19. Magic Numbers (avoid!) 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;

  20. 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. 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. Output (2) • Examples: • var • num : integer; • begin • num := 10; • writeln('line1'); • write('line2A'); • writeln('line2B'); • writeln(num); • writeln('num=',num);

  23. 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. Formatting Output (2) • Examples • var • num : real; • begin • num := 12.34; • writeln(num); • writeln(num:5:2);

  25. 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. 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. 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. 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. Input (2) • Examples: • var • num1, num2 : integer • begin • read (num1); • read (num2); • read (num1, num2);

  30. 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. 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. 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. Extra Uses Of Readln • To filter out extraneous input • As an input prompt • e.g., • writeln('To continue press return'); • readln;

  34. Common Programming Errors • Syntax/compile errors • Runtime errors • Logic errors

  35. High level algorithm Flowchart or pseudo-code (on paper) Text editor XEmacs Pascal program anything.p (Unix file) Pascal compiler gpc Syntax error: No executable (a.out) produced. Syntax/Compile Errors

  36. High level algorithm Pascal program Flowchart or pseudo-code (on paper) anything.p (Unix file) Machine language program a.out (Unix file) Executing a.out Runtime error (execution stops) Runtime Errors Text editor Pascal compiler XEmacs gpc

  37. High level algorithm Pascal program Flowchart or pseudo-code (on paper) anything.p (Unix file) Machine language program a.out (Unix file) Executing a.out Program finishes execution but may result in incorrect result Logic Errors Text editor Pascal compiler XEmacs gpc

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