CMP 131 Introduction to Computer Programming Violetta CavalliSforza

  • Slides: 17
Download presentation
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 4, Lecture 1

CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 4, Lecture 1

Today • Brief review of important programming concepts • Some programming language details •

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

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 :

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 :

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.

BEGIN-END BRACKETING • Is used to structure programs • Associates a several different executable

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.

Syntax & PL Details

Syntax & PL Details

 • Syntax graph – – Syntax Graphs Graphical representation for language syntax Circles

• Syntax graph – – Syntax Graphs 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 Letter Digit

 • Identifier: Identifiers – Name of a constant, variable, program, function, or procedure

• Identifier: Identifiers – 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 ARRAY END NOT BEGIN FILE OF CASE FOR OR

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

Standard Identifiers • Data types: – boolean, char, integer, real, text • Constants: –

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, sqrt, succ, trunc • Procedures: – dispose, new, readln, reset, rewrite, writeln • Files: – input, output

Constants • Constants: – Memory cells whose value can’t change during runtime of a

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: const Identifier = value • 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

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’ 100 3. 14 ‘D’ true {String literal} {Integer literal} {Real literal} {Character literal} {Boolean literal}

Variables • Variables: – – Memory cell whose value can change during runtime When

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/Read. Ln statement • Syntax graph: var Identifier : type ; , • Example: var Num 1, Num 2, Num 3 Average Sum : Integer; {colon !!!} : Real; : Integer;

Used Conventions • We will use the following conventions : – Reserved words in

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 X, Y, Z { OK!} {Not OK!} • Similar names for different identifiers should be avoided (unless they are meaningful) X 1, X 2, X 3 {Not Ok!} • Don’t use too short or too long names The. Value. Of. The. Sum. Of. All. Numbers. In. The. List A {Too long!} {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,

Self-Check • Which of the following are Pascal reserved words, standard identifiers, valid identifiers, invalid identifiers? end Read. Ln Bill program Sues’s Rate Start begin const Y=Z Prog#2 &Up First Name ‘Max. Scores’ A*B Costa. Mesa, CA Barnes&Noble CONST XYZ 123 This. Is. ALong. One 123 XYZ

Self-Check • Which of the following literal values are legal and what are their

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