170 likes | 193 Views
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
E N D
CMP 131Introduction 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 • 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.)
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.
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.
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.
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
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
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
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
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’;
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}
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;
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
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
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