38
CS241 PASCAL I - Contro l Structures 1 PASCAL Control PASCAL Control Structures Structures Modified Slides of Philip Fees

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

Embed Size (px)

Citation preview

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

CS241 PASCAL I - Control Structures

1

PASCAL Control StructuresPASCAL Control Structures

Modified Slides of Philip Fees

Page 2: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

2

Reserved WordsReserved Words

Cannot be used for identifiers (names) of

constant,

type,

variable,

function,

procedure

Page 3: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

3

Basic Data TypesBasic Data Types

integer (-maxint <= integer <= maxint) real (4567.123, 4.567123 x 103,

4.567123E3) char (‘a’, ‘7’, ‘W‘, . . .) string constant (const name = ‘Cyprus’;)

Page 4: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

4

Basic OutputBasic Output

write - output without newline terminator writeln - output with newline terminator Writeln (expr1[:n[:n]], expr2[:n[:n]], … ,

exprN[:n[:n]]);

Page 5: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

5

TopicsTopics

Arithmetic Operators Basic Debugging (tracing and print) Basic Input Constants Standard Functions

Page 6: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

6

Arithmetic OperatorsArithmetic Operators

( ), *, MOD, DIV, /, +, - What is an operator? Precedence - evaluation of sequence of

multiple operator expressions Typically left to right, but can be inside

to outside or right to left.

Page 7: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

7

Basic InputBasic 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

Page 8: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

8

Constants and variablesConstants and variables

CONSTpi = 3.14;

VARarea, radius : real;

. . .area := 3.14 * radius * radius;

. . .area := pi * radius * radius;

Page 9: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

9

Standard FunctionsStandard Functions

What is a function– perform some operation on argument(s)– returns value(s) as determined by

operation Example

area := pi * sqr(radius);

Page 10: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

10

TopicsTopics

Boolean Logic– Relational Operators– Boolean Expressions

IF . . . THEN . . . ELSE Statements CASE Statements

Page 11: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

11

Boolean ExpressionBoolean Expression

Relational Operators

(=, <, >, <=, >=, <>)

used to build Boolean Expressions

Logical Operators

(AND, OR, NOT)

used to build Compound Boolean Expressions

Page 12: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

12

IF . . . THEN StatementsIF . . . THEN Statements

A decision-making statement

Syntax:IF <boolean expression> THEN

<statement>

Page 13: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

13

Compound StatementsCompound Statements

Treats a set of statements as one Syntax:

IF <boolean expression> THEN BEGIN

<statement1>;<statement2>;...<statementN>;

END;

Page 14: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

14

IF . . . THEN . . . ELSE IF . . . THEN . . . ELSE StatementsStatements

Action to occur only if a boolean statement is false

Page 15: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

15

Nested IF StatementsNested IF Statements

When the <statement> part of IF or ELSE is an IF THEN ELSE statement

Nested IF ELSE is considered as a single statement and doesn’t require BEGIN END

Page 16: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

16

CASE StatementCASE Statement

Abbreviated IF THEN, ELSE IF THEN, etc statement

Protect case with IF THEN ELSE or use of OTHERWISE

Page 17: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

17

ExercisesExercises

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 subtraction2x + -1y becomes 2x - y = 0

– give error if division by 0 would occur

Page 18: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

18

TopicsTopics

Nested Selection

No additional information available

Page 19: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

19

Week 5 TopicsWeek 5 Topics

Repetition Problem Conditional Branch Pre-testing (Top-testing) Loops Post-testing (Bottom-testing) Loops Comparing Loops

Page 20: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

20

Repetition ProblemRepetition 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.

Page 21: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

21

Conditional BranchConditional Branch

Predates looping constructslabel:

statement1;

statement2;

...

statementN;

if ( boolean expression is true)goto label;

Exercise: Write N! using conditional branch logic

Page 22: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

22

Loop TerminologyLoop 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

Page 23: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

23

Pre-testing (Top-testing) LoopsPre-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

Page 24: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

24

For LoopFor 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

Page 25: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

25

While LoopWhile Loop

Syntax:WHILE <Boolean expr> DO

Exercise: pg. 264 #3, 4, 5

Page 26: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

26

Post-testing (Bottom-testing) Post-testing (Bottom-testing) LoopsLoops

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.

Page 27: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

27

Comparing LoopsComparing 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

Page 28: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

28

ExercisesExercises

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)

Page 29: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

29

Week 6 TopicsWeek 6 Topics

Nested Loops

Page 30: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

30

ExamplesExamples

pg. 285 Example 6.26 pg. 285 Example 6.30 pg. 291 Program Problem 6 b pg. 310

Page 31: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

31

Week 7 & 8 TopicsWeek 7 & 8 Topics

Subprogramming Procedures Parameters Side Effects

Page 32: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

32

ProceduresProcedures

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)

Page 33: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

33

Procedures (cont.)Procedures (cont.)

Placed in declaration section of program/procedure/function

use {-------------} to denote procedure boundary

procedure name unique to program (scope)

Page 34: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

34

Procedure ExecutionProcedure 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

Page 35: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

35

ParametersParameters

PROCEDURE <name> ( <parameter list> ); parameter list format: similar to VAR format Discuss parameter notes slide

Page 36: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

36

Parameter TypesParameter 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

Page 37: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

37

Side EffectsSide Effects

Unintended change in a variable Typically associated with call by reference

parameter passing Considered “bad” programming. Why? Sometimes desirable. When/Example?

Page 38: CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures

38

ExerciseExercise

Walk through program on pg. 349 Do prime program in class #2, pg. 372 Lab: #3 pg. 372, #8 pg. 373, prime program

above.