1. Trang chủ
  2. » Giáo án - Bài giảng

Statement level control structures

25 113 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 25
Dung lượng 182,02 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Counter-Controlled Loops • A counting iterative control statement has a variable, called the loop variable, in which the count value is maintained • Loop parameter includes the initial,

Trang 2

• Levels of Control Flow:

1 Within expressions

2 Among program statements

3 Among program units (highest level)

• Evolution:

– FORTRAN I control statements were based

directly on IBM 704 hardware

– It was proven that all flowcharts can be coded with only two-way selection and pretest logical

Copyright © 2006 Addison-Wesley All rights reserved 1-3

loops

Introduction (cont.)

• A control structure is a control statement and the collection of statements whose execution it controls

• Overall Design Question:

What control statements should a language have – What control statements should a language have, beyond selection and pretest logical loops?

Trang 3

Design issues

• Whether the control structure can have multipleWhether the control structure can have multiple entries

Whether the control structure can have multiple

• Whether the control structure can have multiple exits

Copyright © 2006 Addison-Wesley All rights reserved 1-5

Compound statements

• Compound statements allow a collection of

statements to be abstracted to a single

Trang 4

Selection Statements

• AA selection statementselection statement provides the means ofprovides the means of

choosing between two or more paths of

Copyright © 2006 Addison-Wesley All rights reserved 1-7

Two-Way Selection Statements

Trang 5

Example – FORTRAN IV

• FORTRAN IF

IF (boolean_expr) statement

• Problem: can select only a single statement; toProblem: can select only a single statement; to

select more, a GOTO must be used, as in the

Trang 6

Nested Selectors - if … if … else

• Java's static semantics rule: else goes with the nearest if

• ALGOL 60 chose to use syntax, rather than aALGOL 60 chose to use syntax, rather than a

rule, to connect else clauses to then clauses

if sum = 0 then begin

if count = 0 then

result := 0

if sum = 0 then begin

if count = 0 then result := 0 result := 0

else

result := 1

d

result := 0 end

Nested Selectors (cont.)

• FORTRAN 90 and Ada solution – closing special words

d if else

end if

d if

end if else

d if

Trang 7

Multiple-Way Selection Statements

• The multiple selection construct allows the

selection of one of any number of statements

• Design issues:

Wh t i th f d t f th t l

– What is the form and type of the control

expression?

How are the selectable segments specified (single

– How are the selectable segments specified (single, compound or statement sequences)?

– Is execution flow through the structure restricted Is execution flow through the structure restricted

to include just a single selectable segment?

– What is done about unrepresented expression

Copyright © 2006 Addison-Wesley All rights reserved 1-13

Trang 8

Modern Multiple Selectors

• The general form of Hoare's multiple selector is

Copyright © 2006 Addison-Wesley All rights reserved 1-15

– The executed statement is the one chosen by the

value of the integer_expression

Design choices

• Expression is any ordinal type

(int, boolean, char, enum)

• Segments can be single, compound statement orSegments can be single, compound statement or block

• One/any number of segment(s) can be executed per execution of the construct

• Many dialects now have otherwise or else

clause for unrepresented values

Trang 9

if index = 3 goto one_three

if index = 2 goto two_four

if index = 4 goto two four s_odd : s_odd + idx;

s_odd := s_odd + index; goto out

else Writeln(‘Error’);

end;

goto out two_four:

even := even + 1;

s even := s even + index;

Copyright © 2006 Addison-Wesley All rights reserved 1-17

s_even := s_even + index; out: …

Multiple-Way Selection Statements

• Multiple selectors can appear as direct

extensions to two-way selectors, using else-if

clauses (ALGOL 68, FORTRAN 90, Ada, C)

Trang 10

1 How is iteration controlled?

2 Where should the control mechanism appear in

Copyright © 2006 Addison-Wesley All rights reserved 1-19

the loop?

Counter-Controlled Loops

• A counting iterative control statement has a

variable, called the loop variable, in which the count value is maintained

• Loop parameter includes the initial, terminal

values of the loop variable and stepsize - the difference between sequential loop variable

values

• Counter-controlled loops are often supported

by machine instructions so their execution is faster than logically-controlled loops

Trang 11

• Should it be legal for the loop variable or loop

• Should it be legal for the loop variable or loop parameters to be changed in the loop body, and

if so does the change affect loop control?

• Should the loop parameters be evaluated only Copyright © 2006 Addison-Wesley All rights reserved 1-21once, or once for every iteration?

Iterative Statements – FORTRAN 90

– Stepsize can be any value but zero

– Parameters can be expressions

– Loop variable must be INTEGER

– Loop variable always has its last value

– The loop parameters can be changed in the loop, but

iteration count is evaluated only once, so the changes

iteration count is evaluated only once, so the changes

do not affect loop control

Trang 12

An operational semantics description

<expr> step <expr> until <expr> |

<expr> while <Boolean_expr>

• Design choices:

Control expression can be i t or l

– Control expression can be int or real

– Control variable has its last assigned value after loop termination

– The loop variable cannot be changed in the loop

– The loop parameters are evaluated with every iteration, making it very complex and difficult to read and the making it very complex and difficult to read and the changes may affect loop control

Trang 13

3 * index while index < 1000

3 * index while index < 1000,

34, 2, –24 do sum := sum + index;

Copyright © 2006 Addison-Wesley All rights reserved 1-25

 1 + 4 + 13 + 41 + 43 + 45 + 47 + 147 + 441 +

34 + 2 + –24

An operational semantics description

• With the step-until form

for_var = init_expr

loop:

until = until expr

step = step_expr

temp = (for var – until) * SIGN(step) p ( _ ) ( p)

if temp > 0 goto out

[loop body]

for_var = for_var + step

goto loop

out: …

Trang 14

sum := sum + count

temp = count – until

if temp > 0 goto loop2 sum = sum + count

count = count + step goto loop1

loop2: p count = 3 * count

if sum > 1000 goto out sum = sum + count

Copyright © 2006 Addison-Wesley All rights reserved 1-27

sum sum + count goto loop2

– Loop variable must be an ordinal type

– After normal termination, loop variable is undefined – The loop variable cannot be changed in the loop; the p g p; loop parameters can be changed, but they are

evaluated just once, so it does not affect loop control – Stepsize is 1

Trang 15

Iterative Statements - Ada

– The loop variable does not exist outside the loop

– The loop variable cannot be changed in the loop

Copyright © 2006 Addison-Wesley All rights reserved 1-29

– The discrete_range is evaluated just once

An operational semantics description

[define for_var (its type is that of the

discrete_range)] [evaluate discrete range]

loop:

if [there are no elements left in the discrete

range] goto out

for_var = [next element of discrete range]

Trang 16

Iterative Statements – C

• Syntax:

for ([expr_1] ; [expr_2] ; [expr_3]) statement

• Design choices:

– There is no explicit loop variable

– Everything can be changed in the loop

– The first expression is evaluated once, but the other two are evaluated with each iteration

Copyright © 2006 Addison-Wesley All rights reserved 1-31

An operational semantics description

Trang 17

Iterative Statements – C++, Java

• C++: differs from C in two ways

– The control expression can also be Boolean

– The initial expression can include variable The initial expression can include variable

definitions (scope is from the definition to the end of the loop body)

• Java: differs from C++ in that the control

expression must be Boolean

Copyright © 2006 Addison-Wesley All rights reserved 1-33

Logically-Controlled Loops

• In many cases, collections of statements must

be repeatedly executed, but the repetition

control is based on a Boolean expression rather h

than a counter

• Logically-controlled loops are more general than counter-controlled loops Every counting loop can be built with a logical loop, but the reverse

is not true

• Only selection and logical loops are essential to express the control structure of any flowchart

Trang 18

Design Issues

• Pretest or posttestPretest or posttest

• Should this be a special case of the counting

loop statement (or a separate statement)

Copyright © 2006 Addison-Wesley All rights reserved 1-35

Language Examples

• C/C++ also have both, but the control expression for the posttest version is treated just like in the

for the posttest version is treated just like in the

pretest case (while-do and do-while)

Trang 19

Language Examples (cont.)

• Pascal has separate pretest and posttest logical

loop statements (while-do and repeat-until)

• Java is like C, except the control expression J , p p

must be Boolean (and the body can only be

entered at the beginning - Java has no goto)

• Ada has a pretest version, but no posttest

• FORTRAN 77 and 90 have neither

• Perl has two pretest logical loops, while and

Copyright © 2006 Addison-Wesley All rights reserved 1-37

until, but no posttest logical loop

User-Located Loop Control Mechanisms

end loop;

Name:

loop exit Name when end if;

end loop;

found;

end loop Name;

Name:

loop

… exit Name when exit;

end if;

end loop;

… end loop;

exit Name when

S > 5;

… end loop Name;

Trang 20

User-Located Loop Control Mechanisms

• C/C++, Java

– break : Unconditional, for any loop or switch;

one level only

– continue: Unconditional, for any loops; it skips the remainder of this iteration, but does not exit

Copyright © 2006 Addison-Wesley All rights reserved 1-39

– CYCLE : the same semantics as C's continue

Iteration Based on Data Structures

• These loops are controlled by the number of elements in a data structure

elements in a data structure

• Control mechanism is a call to a function that returns the next element in some chosen order

returns the next element in some chosen order,

if there is one; else exit loop

• In Perl: foreach statement iterates on the In Perl: foreach statement iterates on the

elements of lists or arrays

@name = (“Bob”, “Carol”, “Ted”);

Trang 21

Iteration Based on … (cont.)

• In COMMON LISP: the dolist function iterates on

simple lists

dolist (x '(a b c d)) (prin1 x) (princ " "))

• In Java: for statement is designed for Collections In Java: for statement is designed for Collections class and array

class EnhancedForDemo {

public static void main(String[] args){

int[] numbers = {1, 2, 3};

for (int item : numbers) { ( ) {

System.out.println("Count is: " + item); }

• Some languages do not have them: e.g., Java

• Loop exit statements are restricted andLoop exit statements are restricted and

somewhat camouflaged goto’s

Trang 22

Guarded Commands

• Purpose: to support a new programming

methodology (verification during program

development)p

• This methodology is described in Dijkstra (1976)

Copyright © 2006 Addison-Wesley All rights reserved 1-43

Selection

• Syntax:

if <Boolean expression>  <statement>

[] <Boolean expression>  <statement>

[] <Boolean expression>  <statement>

fi

• Semantics: when this construct is reached

– Evaluate all boolean expressions p

– If more than one are true, choose one

non-deterministically

– If none are true, it is a runtime error

Trang 24

• Syntax:

do <Boolean expression>  <statement>

[] <Boolean expression>  <statement>

[] <Boolean expression>  <statement>

od

• Semantics: For each iteration

– Evaluate all boolean expressions Evaluate all boolean expressions

– If more than one are true, choose one

non-deterministically; then start loop again

Copyright © 2006 Addison-Wesley All rights reserved 1-47

– If none are true, exit loop

Loops

Trang 25

do q1 > q2 –> temp := q1; q1 := q2; q2 := temp;

[] q2 > q3 –> temp := q2; q2 := q3; q3 := temp; [] q3 > q4 –> temp := q3; q3 := q4; q4 := temp;

• Connection between control statements and

program verification is intimate

• Verification is impossible with gotosp g

• Verification is possible with only selection and logical pretest loops

• Verification is relatively simple with only

guarded commands

Ngày đăng: 26/01/2015, 10:08

TỪ KHÓA LIÊN QUAN