Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.

Similar presentations


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

1 CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

2 CS241 PASCAL I - Control Structures2 Reserved Words Cannot be used for identifiers (names) of constant, type, variable, function, procedure

3 CS241 PASCAL I - Control Structures3 Basic Data Types l integer (-maxint <= integer <= maxint) l real (4567.123, 4.567123 x 10 3, 4.567123E3) l char (‘a’, ‘7’, ‘W‘,...) l string constant (const name = ‘Cyprus’;)

4 CS241 PASCAL I - Control Structures4 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]]);

5 CS241 PASCAL I - Control Structures5 Topics l Arithmetic Operators l Basic Debugging (tracing and print) l Basic Input l Constants l Standard Functions

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

7 CS241 PASCAL I - Control Structures7 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

8 CS241 PASCAL I - Control Structures8 Constants and variables CONST pi = 3.14; VAR area, radius : real;... area := 3.14 * radius * radius;... area := pi * radius * radius;

9 CS241 PASCAL I - Control Structures9 Standard Functions l What is a function –perform some operation on argument(s) –returns value(s) as determined by operation l Example area := pi * sqr(radius);

10 CS241 PASCAL I - Control Structures10 Topics l Boolean Logic –Relational Operators –Boolean Expressions l IF... THEN... ELSE Statements l CASE Statements

11 CS241 PASCAL I - Control Structures11 Boolean Expression Relational Operators (=,, =, <>) used to build Boolean Expressions Logical Operators (AND, OR, NOT) used to build Compound Boolean Expressions

12 CS241 PASCAL I - Control Structures12 IF... THEN Statements l A decision-making statement l Syntax: IF THEN

13 CS241 PASCAL I - Control Structures13 Compound Statements l Treats a set of statements as one l Syntax: IF THEN BEGIN ;... ; END;

14 CS241 PASCAL I - Control Structures14 IF... THEN... ELSE Statements l Action to occur only if a boolean statement is false

15 CS241 PASCAL I - Control Structures15 Nested IF Statements l When the part of IF or ELSE is an IF THEN ELSE statement l Nested IF ELSE is considered as a single statement and doesn’t require BEGIN END

16 CS241 PASCAL I - Control Structures16 CASE Statement l Abbreviated IF THEN, ELSE IF THEN, etc statement l Protect case with IF THEN ELSE or use of OTHERWISE

17 CS241 PASCAL I - Control Structures17 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

18 CS241 PASCAL I - Control Structures18 Topics Nested Selection No additional information available

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

20 CS241 PASCAL I - Control Structures20 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.

21 CS241 PASCAL I - Control Structures21 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

22 CS241 PASCAL I - Control Structures22 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

23 CS241 PASCAL I - Control Structures23 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

24 CS241 PASCAL I - Control Structures24 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

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

26 CS241 PASCAL I - Control Structures26 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.

27 CS241 PASCAL I - Control Structures27 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

28 CS241 PASCAL I - Control Structures28 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)

29 CS241 PASCAL I - Control Structures29 Week 6 Topics l Nested Loops

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

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

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

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

34 CS241 PASCAL I - Control Structures34 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

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

36 CS241 PASCAL I - Control Structures36 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

37 CS241 PASCAL I - Control Structures37 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?

38 CS241 PASCAL I - Control Structures38 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 Control Structures Modified Slides of Philip Fees."

Similar presentations


Ads by Google