460 likes | 1.02k Views
PASCAL I - Control Structures. Philip Fees CS241. Week 1 Topics. Reserved Words Syntax Diagrams PASCAL Program Basic Data Types Basic Output. Reserved Words. See page 29-30 Identifiers that are part of the syntax of the language
E N D
PASCAL I - Control Structures Philip Fees CS241 CS241 PASCAL I - Control Structures
Week 1 Topics • Reserved Words • Syntax Diagrams • PASCAL Program • Basic Data Types • Basic Output CS241 PASCAL I - Control Structures
Reserved Words • See page 29-30 • Identifiers that are part of the syntax of the language • Cannot be used for constant, type, variable, function, procedure identifier names CS241 PASCAL I - Control Structures
Syntax Diagrams • See page 30-31 • Graphic representation of a language’s grammar • Identifier: [A-Za-z][A-Za-z0-9]* CS241 PASCAL I - Control Structures
PASCAL Program • See page 32 • What is the minimum PASCAL program? CS241 PASCAL I - Control Structures
Basic Data Types • integer (-maxint <= integer <= maxint) • real (4567.123, 4.567123 x 103, 4.567123E3) • char (‘a’, ‘7’, ‘ ‘) • String constant (const name = ‘sally’;) CS241 PASCAL I - Control Structures
Basic Output • write - output without newline terminator • writeln - output with newline terminator • writeln(expr1[:n[:n]], expr2[:n[:n]], … , exprN[:n[:n]]); CS241 PASCAL I - Control Structures
Exercises • Exercise #39, page 52 CS241 PASCAL I - Control Structures
Week 2 Topics • Arithmetic Operators • Basic Debugging (tracing and print) • Basic Input • Constants • Standard Functions CS241 PASCAL I - Control Structures
Arithmetic Operators • ( ), *, MOD, DIV, /, +, - • What is an operator? • Precedence - evaluation sequence of multiple operator expressions • Typically left to right, but can be inside to outside or right to left. CS241 PASCAL I - Control Structures
Basic Input • Variables - name vs. memory location • Data pointer • location of next data item to be read from input stream • read - input next data item • readln - input next data item, skip to next line CS241 PASCAL I - Control Structures
Constants • Maintenance - descriptive name, one location updates CONST pi = 3.14; VAR area, radius : real; ... area := 3.14 * radius * radius; vs. area := pi * radius * radius; CS241 PASCAL I - Control Structures
Standard Functions • pp. 92 & 96 • what is a function • perform some operation on argument(s) • returns value(s) as determined by operation • Example area := pi * sqr(radius); CS241 PASCAL I - Control Structures
Week 3 Topics • Boolean Logic • Relational Operators • Boolean Expressions • IF THEN ELSE Statements • CASE Statements CS241 PASCAL I - Control Structures
Boolean Expression • Relational Operators (=, <, >, <=, >=, <>) used to build Boolean Expressions • Logical Operators (AND, OR, NOT) used to build Compound Boolean Expressions • Exercise: pp 177-178 #16-22, #28-33 CS241 PASCAL I - Control Structures
IF THEN Statements • A decision-making statement • Syntax: IF <boolean expression> THEN <statement> • Review flow diagram, figure 5.1 pg. 179 CS241 PASCAL I - Control Structures
Compound Statements • Treats a set of statements as one • Good programming practice even for single statement. Why? • Syntax: IF <boolean expression> THEN BEGIN <statement1>; <statement2>; ... <statementN>; END; CS241 PASCAL I - Control Structures
IF THEN ELSE Statements • Action to occur only if boolean statement is false • Review flow diagram, figure 5.2 pg. 187 CS241 PASCAL I - Control Structures
Nested IF Statements • When the <statement> part of IF or ELSE is an IF THEN ELSE statement • Nested IF ELSE is considered a single statement and doesn’t require BEGIN END • Review Example 5.13 on page 197 • WARNING: ensure ELSE matches to correct IF CS241 PASCAL I - Control Structures
CASE Statement • Abbreviated IF THEN, ELSE IF THEN, etc statement • Review flow diagram, figure 5.4 pg. 209 and CASE rules on pg. 210 • protect case with IF THEN ELSE or use of OTHERWISE CS241 PASCAL I - Control Structures
Exercises • Modify LAB #2 to improve the display • New features • If the coefficient is 1 don’t display the 1: 1x + 2y = 5 becomes x + 2y = 5 • if the coefficient is 0 don’t display: 0x + 5y = 5 becomes 5y = 5 • display subtraction 2x + -1y becomes 2x - y = 0 • give error if division by 0 would occur CS241 PASCAL I - Control Structures
Week 4 Topics Nested Selection No additional information available CS241 PASCAL I - Control Structures
Week 5 Topics • Repetition Problem • Conditional Branch • Pre-testing (Top-testing) Loops • Post-testing (Bottom-testing) Loops • Comparing Loops CS241 PASCAL I - Control Structures
Repetition Problem • A class of problems that can be solved by repeatedly performing some task until some condition is no longer valid. • Example 1: Monopoly, “Go to Jail” until you roll doubles, 3 rolls, or pay $50. • Example 2: N ! = 1 * 2 * 3 * ... (N-1) * N • Iterations could be written as sequence of steps - # of iterations may vary. CS241 PASCAL I - Control Structures
Conditional Branch • Predates looping constructs label: statement1; statement2; ... statementN; if ( boolean expression is true) goto label; • Exercise: Write N! using conditional branch logic CS241 PASCAL I - Control Structures
Loop Terminology • initialization - assign values before evaluation • evaluate - determine if loop should continue • increment - modify or increase loop controls • iteration - one pass over loop (evaluate, body, increment) • loop body - syntax that is executed with each iteration of loop CS241 PASCAL I - Control Structures
Pre-testing (Top-testing) Loops • Evaluates looping condition before executing body of loop • Body of loop executes a minimum of 0 times • Pascal has FOR and WHILE loops • FOR loop (pg. 239) • based on FORTRAN FOR loop • used for fixed increment looping • WHILE loop (pg. 252) • General purpose top-testing loop CS241 PASCAL I - Control Structures
For Loop • Syntax: FOR <var> := <init> [TO | DOWNTO] <value> DO • Good practice to have maximum iteration value defined as a CONST or VAR (i.e., don’t hard code). • Loop control variable may or may not be defined as VAR • Exercise: pg. 251 7-10 CS241 PASCAL I - Control Structures
While Loop • Syntax:WHILE <Boolean expr> DO • Exercise: pg. 264 #3, 4, 5 CS241 PASCAL I - Control Structures
Post-testing (Bottom-testing) Loops • Evaluates looping condition after executing body of loop • Body of loop executes a minimum of 1 times • Syntax: REPEAT UNTIL <Boolean expr> • Exercise: pg. 272 #3, 4, 5. CS241 PASCAL I - Control Structures
Comparing Loops • Each loop can be rewritten in another loop’s syntax • Easier to use similar testing loops (i.e. for and while) • Review Example 6.22, and 6.23 on pg 275 CS241 PASCAL I - Control Structures
Exercises • Write one of the following: • pg. 312 # 12 (easier) NOTE: read term from keyboard instead of file • pg. 310 # 6a (moderate) • pg. 311 #8 (harder) CS241 PASCAL I - Control Structures
Week 6 Topics • Nested Loops CS241 PASCAL I - Control Structures
Examples • pg. 285 • Example 6.26 pg. 285 • Example 6.30 pg. 291 • Program Problem 6 b pg. 310 CS241 PASCAL I - Control Structures
Week 7 & 8 Topics • Subprogramming • Procedures • Parameters • Side Effects CS241 PASCAL I - Control Structures
Subprogramming • Modular - readability, exchange (swap), and fix parts • Subtasks - repetitive execution of sub functionality • Structured - receive values, perform task, return result • Black Box design - Lego building blocks CS241 PASCAL I - Control Structures
Procedures • Design to perform small discreet task • Thought of as a small program - test as such • Program is a collection/series of procedure (function) calls • Discuss procedure format slide (pg. 321) CS241 PASCAL I - Control Structures
Procedures (cont.) • Placed in declaration section of program/procedure/function • use {-------------} to denote procedure boundary • procedure name unique to program (scope) CS241 PASCAL I - Control Structures
Procedure Execution • a procedure is called or invoked by name • flow of control jumps to first line in procedure • on completing procedure, flow of control returns to first line after procedure call • walk through exercise 11 pg. 335 CS241 PASCAL I - Control Structures
Parameters • PROCEDURE <name> ( <parameter list> ); • parameter list format: similar to VAR format • Discuss parameter notes slide CS241 PASCAL I - Control Structures
Parameter Types • formal - variables in procedure heading (parameters) • actual - values in procedure call (arguments) • call by value (value parameters) arguments are copies to parameters • call by reference (variable parameters) parameters refer to arguments CS241 PASCAL I - Control Structures
Side Effects • Unintended change in a variable • Typically associated with call by reference parameter passing • Considered “bad” programming. Why? • Sometimes desirable. When/Example? CS241 PASCAL I - Control Structures
Exercise • Walk through program on pg. 349 • Do prime program in class #2, pg. 372 • Lab: #3 pg. 372, #8 pg. 373, prime program above. CS241 PASCAL I - Control Structures