Docsity

Statement Level Control Structures - Programming Languages - Slides | CS 403, Study notes of Programming Languages

Toggle sidebar
bg1
1
1
CS 403 - Programming
Languages
Class 11
September 26, 2000
CS 403, Class 10 Slide # 2
Today’s Agenda
Finish Chapter 7
Assignment:
Read chapter 8 for Tuesday
Reminder: CS Dept website has
student contact information, student
websites. Please update your contact
information (email address, personal
webpage URL)
CS 403, Class 10 Slide # 3
Homework Assignment
Chapter 5, page 255, 256
#13 (Row major & column major), 1, 2, 12
(free!?), 14
Chapter 6, page 284ff
#5 (include Scott Meyer’s arguments on
this), 10, 14, 19
Turn in on paper or by email (9:30AM)
on Tuesday, October 3rd
CS 403, Class 10 Slide # 4
Of course, 403 is important!
The cartoon that was on this pa ge was taken from
http://www.borg.com/~rjgtoons/toon.html
CS 403, Class 10 Slide # 5
Chapter 7
•Statement-level Control
Structures
CS 403, Class 10 Slide # 6
Levels of Control Flow
1. Within expressions
2. Among program units
3. Among program statements
pf3
pf4
pf5

Partial preview of the text

Download Statement Level Control Structures - Programming Languages - Slides | CS 403 and more Study notes Programming Languages in PDF only on Docsity!

1

CS 403 - Programming

Languages

Class 11

September 26, 2000

CS 403, Class 10 Slide # 2

Today’s Agenda

Finish Chapter 7

Assignment:

 Read chapter 8 for Tuesday

Reminder: CS Dept website has

student contact information, student

websites. Please update your contact

information (email address, personal

webpage URL)

CS 403, Class 10 Slide # 3

Homework Assignment

Chapter 5, page 255, 256

 #13 (Row major & column major), 1, 2, 12

(free!?), 14

Chapter 6, page 284ff

 #5 (include Scott Meyer’s arguments on

this), 10, 14, 19

Turn in on paper or by email (9:30AM)

on Tuesday, October 3 rd

CS 403, Class 10 Slide # 4

Of course, 403 is important!

The cartoon that was on this page was taken from http://www.borg.com/~rjgtoons/toon.html

CS 403, Class 10 Slide # 5

Chapter 7

• Statement-level Control

Structures

CS 403, Class 10 Slide # 6

Levels of Control Flow

1. Within expressions

2. Among program units

3. Among program statements

CS 403, Class 10 Slide # 7

Levels of Control Flow

Evolution

  • FORTRAN I control statements were

based directly on IBM 704 hardware

  • Much research and argument in the1960s

about the issue

  • One important result: It was proven that all flowcharts can be coded with only two-way selection and pretest logical loops.

CS 403, Class 10 Slide # 8

Control Structures

Def: A control structure is a control

statement and

the statements whose execution it

controls.

  • Compound statements - introduced by

ALGOL 60 in the form of begin...end

  • A block is a compound statement that can

define a new scope (with local variables)

CS 403, Class 10 Slide # 9

Selection Statements

Design Issues:

1. What is the form and type of the control

expression?

2. What is the selectable segment form

(single statement, statement sequence,

compound statement)?

3. How should the meaning of nested

selectors be specified?

CS 403, Class 10 Slide # 10

Selection Statements

Single-Way Examples

FORTRAN IF : IF (boolean_expr) statement

Problem: can select only a single statement; to select more, a goto must be used, as in the following example FORTRAN example:

IF (.NOT. condition ) GOTO 20 ... ... 20 CONTINUE

CS 403, Class 10 Slide # 11

Two-way Selector Examples

ALGOL 60 if :

if ( boolean_expr )

then statement (the then clause)

else statement (the else clause)

  • The statements could be single or

compound.

CS 403, Class 10 Slide # 12

e.g. (Pascal) if ... then if ... then ... else ...

Which then gets the else? Pascal's rule: else goes with the nearest then

  • ALGOL 60's solution - disallow direct nesting if ... then if ... then begin begin if ... if ... then ... then ... end else ... else ... End

Nested Selectors

CS 403, Class 10 Slide # 19

Counter-Controlled Loops

FORTRAN 90’s Other DO

Syntax: [name:] DO variable = initial, terminal [, stepsize] … END DO [name]

Loop var must be an INTEGER

CS 403, Class 10 Slide # 20

Counter-Controlled Loops

  1. ALGOL 60

Syntax: for var := <list_of_stuff> do statement where <list_of_stuff> can have:  list of expressions  expression step expression until expression  expression while boolean_expression for index := 1 step 2 until 50, 60, 70, 80, index + 1 until 100 do

( index = 1, 3, 5, 7, ..., 49, 60, 70, 80,81, 82, ..., 100)

CS 403, Class 10 Slide # 21

Counter-Controlled Loops

ALGOL 60 Design choices

Control expression can be int or real ; its scope is whatever it is declared to be Control var has its last assigned value after loop termination The loop var cannot be changed in the loop, but the parameters can, and when they are, it affects loop control Parameters are evaluated with every iteration, making it very complex and difficult to read

CS 403, Class 10 Slide # 22

Counter-Controlled Loops

  1. Pascal Syntax: for variable := initial ( to | downto ) final do statement Design Choices:
  2. Loop var must be an ordinal type of usual scope
  3. After normal termination, loop var is undefined
  4. The loop var cannot be changed in the loop; the loop parameters can be changed, but they are evaluated just once, so it does not affect loop control
  5. Loop variable evaluated just once

CS 403, Class 10 Slide # 23

Counter-Controlled Loops

4. Ada

Syntax:

for var in [reverse] discrete_range loop

end loop

CS 403, Class 10 Slide # 24

Counter-Controlled Loops

Ada Design choices

1. Type of the loop var is that of the discrete

range; its scope is the loop body (it is

implicitly declared)

2. The loop var does not exist outside the

loop.

3. The loop var cannot be changed in the loop,

but the discrete range can; it does not

affect loop control.

4. The discrete range is evaluated just once

CS 403, Class 10 Slide # 25

Counter-Controlled Loops

5. C

Syntax: for ([expr_1] ; [expr_2] ; [expr_3]) statement The expressions can be whole statements, or even statement sequences, with the statements separated by commas The value of a multiple-statement expression is the value of the last statement in the expression e.g., for (i = 0, j = 10; j == i; i++) ... If the second expression is absent, it is an infinite loop. CS 403, Class 10 Slide # 26

Counter-Controlled Loops

C Design Choices

 There is no explicit loop var

 Irrelevant

 Everything can be changed in the loop

 Pretest

 The first expression is evaluated once, but the

other two are evaluated with each iteration.

This loop statement is the most flexible.

CS 403, Class 10 Slide # 27

Counter-Controlled Loops

6. C++

Differs from C in two ways:

1. The control expression can also be Boolean

2. The initial expression can include variable

definitions (scope is from the definition to the end of the function in which it is defined)

CS 403, Class 10 Slide # 28

Counter-Controlled Loops

7. Java

Differs from C++ in two ways:

1. Control expression must be Boolean

2. Scope of variables defined in the initial

expression is only the loop body

CS 403, Class 10 Slide # 29

Exercise: Page 325 #

Rewrite in Ada & C/C++ (& Java?)

k = (j + 13) / 27 loop: if k > 10 then goto out k := k + 1 i := 3 * k – 1 goto loop out:

Compare at your table…

CS 403, Class 10 Slide # 30

Logically-Controlled Loops

Design Issues:

  1. Pretest or postest?
  2. Should this be a special case of the counting loop statement (or a separate statement)?

CS 403, Class 10 Slide # 37

Unconditional Branching

Problem: readability Some languages do not have them:e.g., Modula- and Java Label forms:

  1. Unsigned int constants: Pascal (with colon) FORTRAN (no colon)
  2. Identifiers with colons: ALGOL 60, C
  3. Identifiers in << ... >> : Ada
  4. Variables as labels: PL/I  Can be assigned values and passed as parameters  Highly flexible, but make programs impossible to read and difficult to implement CS 403, Class 10 Slide # 38

Restrictions on Pascal's gotos:

A statement group is either a compound statement or the body of a repeat-until The target of a goto cannot be a statement in a statement group that is not active  Means the target can never be in a statement group that is at the same level or is nested more deeply than the one with the goto  An important remaining problem: the target can be in any enclosing subprogram scope, as long as the statement is not in a statement group  This means that a goto can terminate any number of subprograms