1 / 17

CMP 131 Introduction to Computer Programming

CMP 131 Introduction to Computer Programming. Violetta Cavalli-Sforza Week 4, Lecture 1. Today. Brief review of important programming concepts Some programming language details Syntax charts. Important Structured Programming Concepts. Selection Repetition/Looping/Iteration

pperkinson
Download Presentation

CMP 131 Introduction to Computer 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. CMP 131Introduction to Computer Programming Violetta Cavalli-Sforza Week 4, Lecture 1

  2. Today • Brief review of important programming concepts • Some programming language details • Syntax charts

  3. Important Structured Programming Concepts • Selection • Repetition/Looping/Iteration • Sequential execution (stacking) • Nesting • Blocks & Compound Statements (Most high-level PLs provide constructs for these important structured programming concepts. Pascal was one of the first PLs to provide them.)

  4. PROGRAM Rectangle (output); CONST Height = 5; Width = 10; VAR I, J : integer; BEGIN writeln ('Drawing a rectangle.'); writeln; FOR I := 1 TO Height DO FOR J := 1 TO Width DO write ('*'); writeln; END.

  5. PROGRAM Rectangle (output); CONST Height = 5; Width = 10; VAR I, J : integer; BEGIN writeln ('Drawing a rectangle.'); writeln; FOR I := 1 TO Height DO BEGIN FOR J := 1 TO Width DO write ('*'); writeln; END END.

  6. BEGIN-END BRACKETING • Is used to structure programs • Associates a several different executable statements with a single name • Allows nesting and control of several executable statements as if they were a single compound statement.

  7. Syntax & PL Details

  8. Letter Letter Digit Syntax Graphs • Syntax graph • Graphical representation for language syntax • Circles (ovals) used for language terminals • Rectangles used for non-terminals • Arrows show the flow of elements • Terminal: • An element that appears as part of the source program of a specific language • Non-terminal: • An element that is not part of the language alphabet and will be eventually replaced by one or more terminals • Example: • Syntax graph for identifiers

  9. Identifiers • Identifier: • Name of a constant, variable, program, function, or procedure • All identifiers used in a program should either be Standard or user-defined identifiers • No restrictions on the length of an identifier • Only 63 characters will be recognized • Reasonable lengths: between 3 & 10 chars • Pascal identifiers • Reserved words: • Words that have special meaning to the compiler • (See Appendix B, for Pascal’s reserved words) • Standard identifiers: • Words that also have special meaning, but can be redefined by the programmer • User-defined identifiers: • Names given by the programmer • Should not use reserved word or standard identifiers

  10. Reserved Words AND ELSE NIL SET ARRAY END NOT THEN BEGIN FILE OF TO CASE FOR OR UNTIL CONST FORWARD PROCEDURE TYPE DIV FUNCTION PROGRAM VAR DO IF RECORD WHILE DOWNTO MOD REPEAT WITH

  11. Standard Identifiers • Data types: • boolean, char, integer, real, text • Constants: • false, maxint, true • Functions: • abs, arctan, chr, cos, eof, eoln, exp, ln, odd, ord, pred, round, sin, sqr, sqrt, succ, trunc • Procedures: • dispose, new, read, readln, reset, rewrite, write, writeln • Files: • input, output

  12. const = value Identifier ; Constants • Constants: • Memory cells whose value can’t change during runtime of a program • Should never be used in left-hand-side of an assignment-statement or in read-statement • Come after the keyword “const” in declaration part • More than one constant declaration may follow the word const • A semicolon appears at the end of each constant declaration • Syntax graph: • Example: const PI = 3.14; {equal sign !!!} MAX = 100; NAME = ‘John Clark’;

  13. Literals • Values assigned to constants or used directly inside the program • Don’t have names • Could be of any type • Types is automatically recognized by the compiler • Examples: • ‘John Clark’ {String literal} • 100 {Integer literal} • 3.14 {Real literal} • ‘D’ {Character literal} • true {Boolean literal}

  14. var : type Identifier ; , Variables • Variables: • Memory cell whose value can change during runtime • When declared, commas separate variable names in the variable list • More than one list of variables may be declared after the word var • Can be initialized by: • Assignment statement • Read/ReadLn statement • Syntax graph: • Example: var Num1, Num2, Num3 : Integer; {colon !!!} Average : Real; Sum : Integer;

  15. Used Conventions • We will use the following conventions : • Reserved words in uppercase • Built-in functions in lowercase • Constants and Variables: First letter capitalized. If two or more words, first letter of each word is capitalized • Names of identifiers should be meaningful Sum, Number { OK!} X, Y, Z {Not OK!} • Similar names for different identifiers should be avoided (unless they are meaningful) X1, X2, X3 {Not Ok!} • Don’t use too short or too long names TheValueOfTheSumOfAllNumbersInTheList {Too long!} A {Too short!} • Pascal compiler doesn't differentiate between lower and upper case letters

  16. Self-Check • Which of the following are Pascal reserved words, standard identifiers, valid identifiers, invalid identifiers? end ReadLn Bill program Sues’s Rate Start begin const Y=Z Prog#2 &Up First Name ‘MaxScores’ A*B CostaMesa,CA Barnes&Noble CONST XYZ123 ThisIsALongOne 123XYZ

  17. Self-Check • Which of the following literal values are legal and what are their types? Which are illegal and why? 15 ‘XYZ’ ‘*’ $25.123 15; -999 .123 ‘x’ “X” ‘9’ ‘-5’ True

More Related