Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.

Similar presentations


Presentation on theme: "CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241."— Presentation transcript:

1 CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241

2 CS241 PASCAL I - Control Structures2 Week 1 Topics l Reserved Words l Syntax Diagrams l PASCAL Program l Basic Data Types l Basic Output

3 CS241 PASCAL I - Control Structures3 Reserved Words l See page 29-30 l Identifiers that are part of the syntax of the language l Cannot be used for constant, type, variable, function, procedure identifier names

4 CS241 PASCAL I - Control Structures4 Syntax Diagrams l See page 30-31 l Graphic representation of a language’s grammar l Identifier: [A-Za-z][A-Za-z0-9]*

5 CS241 PASCAL I - Control Structures5 PASCAL Program l See page 32 l What is the minimum PASCAL program?

6 CS241 PASCAL I - Control Structures6 Basic Data Types l integer (-maxint <= integer <= maxint) l real (4567.123, 4.567123 x 10 3, 4.567123E3) l char (‘a’, ‘7’, ‘ ‘) l String constant (const name = ‘sally’;)

7 CS241 PASCAL I - Control Structures7 Basic Output l write - output without newline terminator l writeln - output with newline terminator l writeln(expr1[:n[:n]], expr2[:n[:n]], …, exprN[:n[:n]]);

8 CS241 PASCAL I - Control Structures8 Exercises l Exercise #39, page 52

9 CS241 PASCAL I - Control Structures9 Week 2 Topics l Arithmetic Operators l Basic Debugging (tracing and print) l Basic Input l Constants l Standard Functions

10 CS241 PASCAL I - Control Structures10 Arithmetic Operators l ( ), *, MOD, DIV, /, +, - l What is an operator? l Precedence - evaluation sequence of multiple operator expressions l Typically left to right, but can be inside to outside or right to left.

11 CS241 PASCAL I - Control Structures11 Basic Input l Variables - name vs. memory location l Data pointer –location of next data item to be read from input stream l read - input next data item l readln - input next data item, skip to next line

12 CS241 PASCAL I - Control Structures12 Constants l Maintenance - descriptive name, one location updates CONST pi = 3.14; VAR area, radius : real;... area := 3.14 * radius * radius; vs. area := pi * radius * radius;

13 CS241 PASCAL I - Control Structures13 Standard Functions l pp. 92 & 96 l what is a function –perform some operation on argument(s) –returns value(s) as determined by operation l Example area := pi * sqr(radius);

14 CS241 PASCAL I - Control Structures14 Week 3 Topics l Boolean Logic –Relational Operators –Boolean Expressions l IF THEN ELSE Statements l CASE Statements

15 CS241 PASCAL I - Control Structures15 Boolean Expression l Relational Operators (=,, =, <>) used to build Boolean Expressions l Logical Operators (AND, OR, NOT) used to build Compound Boolean Expressions l Exercise: pp 177-178 #16-22, #28-33

16 CS241 PASCAL I - Control Structures16 IF THEN Statements l A decision-making statement l Syntax: IF THEN l Review flow diagram, figure 5.1 pg. 179

17 CS241 PASCAL I - Control Structures17 Compound Statements l Treats a set of statements as one l Good programming practice even for single statement. Why? l Syntax: IF THEN BEGIN ;... ; END;

18 CS241 PASCAL I - Control Structures18 IF THEN ELSE Statements l Action to occur only if boolean statement is false l Review flow diagram, figure 5.2 pg. 187

19 CS241 PASCAL I - Control Structures19 Nested IF Statements l When the part of IF or ELSE is an IF THEN ELSE statement l Nested IF ELSE is considered a single statement and doesn’t require BEGIN END l Review Example 5.13 on page 197 l WARNING: ensure ELSE matches to correct IF

20 CS241 PASCAL I - Control Structures20 CASE Statement l Abbreviated IF THEN, ELSE IF THEN, etc statement l Review flow diagram, figure 5.4 pg. 209 and CASE rules on pg. 210 l protect case with IF THEN ELSE or use of OTHERWISE

21 CS241 PASCAL I - Control Structures21 Exercises l Modify LAB #2 to improve the display l 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

22 CS241 PASCAL I - Control Structures22 Week 4 Topics Nested Selection No additional information available

23 CS241 PASCAL I - Control Structures23 Week 5 Topics l Repetition Problem l Conditional Branch l Pre-testing (Top-testing) Loops l Post-testing (Bottom-testing) Loops l Comparing Loops

24 CS241 PASCAL I - Control Structures24 Repetition Problem l A class of problems that can be solved by repeatedly performing some task until some condition is no longer valid. l Example 1: Monopoly, “Go to Jail” until you roll doubles, 3 rolls, or pay $50. l Example 2: N ! = 1 * 2 * 3 *... (N-1) * N l Iterations could be written as sequence of steps - # of iterations may vary.

25 CS241 PASCAL I - Control Structures25 Conditional Branch l Predates looping constructs label: statement1; statement2;... statementN; if ( boolean expression is true) goto label; l Exercise: Write N! using conditional branch logic

26 CS241 PASCAL I - Control Structures26 Loop Terminology l initialization - assign values before evaluation l evaluate - determine if loop should continue l increment - modify or increase loop controls l iteration - one pass over loop (evaluate, body, increment) l loop body - syntax that is executed with each iteration of loop

27 CS241 PASCAL I - Control Structures27 Pre-testing (Top-testing) Loops l Evaluates looping condition before executing body of loop l Body of loop executes a minimum of 0 times l Pascal has FOR and WHILE loops l FOR loop (pg. 239) –based on FORTRAN FOR loop –used for fixed increment looping l WHILE loop (pg. 252) –General purpose top-testing loop

28 CS241 PASCAL I - Control Structures28 For Loop l Syntax: FOR := [TO | DOWNTO] DO l Good practice to have maximum iteration value defined as a CONST or VAR (i.e., don’t hard code). l Loop control variable may or may not be defined as VAR l Exercise: pg. 251 7-10

29 CS241 PASCAL I - Control Structures29 While Loop l Syntax: WHILE DO l Exercise: pg. 264 #3, 4, 5

30 CS241 PASCAL I - Control Structures30 Post-testing (Bottom-testing) Loops l Evaluates looping condition after executing body of loop l Body of loop executes a minimum of 1 times l Syntax: REPEAT UNTIL l Exercise: pg. 272 #3, 4, 5.

31 CS241 PASCAL I - Control Structures31 Comparing Loops l Each loop can be rewritten in another loop’s syntax l Easier to use similar testing loops (i.e. for and while) l Review Example 6.22, and 6.23 on pg 275

32 CS241 PASCAL I - Control Structures32 Exercises l 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)

33 CS241 PASCAL I - Control Structures33 Week 6 Topics l Nested Loops

34 CS241 PASCAL I - Control Structures34 Examples l pg. 285 l Example 6.26 pg. 285 l Example 6.30 pg. 291 l Program Problem 6 b pg. 310

35 CS241 PASCAL I - Control Structures35 Week 7 & 8 Topics l Subprogramming l Procedures l Parameters l Side Effects

36 CS241 PASCAL I - Control Structures36 Subprogramming l Modular - readability, exchange (swap), and fix parts l Subtasks - repetitive execution of sub functionality l Structured - receive values, perform task, return result l Black Box design - Lego building blocks

37 CS241 PASCAL I - Control Structures37 Procedures l Design to perform small discreet task l Thought of as a small program - test as such l Program is a collection/series of procedure (function) calls l Discuss procedure format slide (pg. 321)

38 CS241 PASCAL I - Control Structures38 Procedures (cont.) l Placed in declaration section of program/procedure/function l use {-------------} to denote procedure boundary l procedure name unique to program (scope)

39 CS241 PASCAL I - Control Structures39 Procedure Execution l a procedure is called or invoked by name l flow of control jumps to first line in procedure l on completing procedure, flow of control returns to first line after procedure call l walk through exercise 11 pg. 335

40 CS241 PASCAL I - Control Structures40 Parameters l PROCEDURE ( ); l parameter list format: similar to VAR format l Discuss parameter notes slide

41 CS241 PASCAL I - Control Structures41 Parameter Types l formal - variables in procedure heading (parameters) l actual - values in procedure call (arguments) l call by value (value parameters) arguments are copies to parameters l call by reference (variable parameters) parameters refer to arguments

42 CS241 PASCAL I - Control Structures42 Side Effects l Unintended change in a variable l Typically associated with call by reference parameter passing l Considered “bad” programming. Why? l Sometimes desirable. When/Example?

43 CS241 PASCAL I - Control Structures43 Exercise l Walk through program on pg. 349 l Do prime program in class #2, pg. 372 l Lab: #3 pg. 372, #8 pg. 373, prime program above.


Download ppt "CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241."

Similar presentations


Ads by Google