Computer Science

Design Choices

1. There is no explicit loop variable

2. The loop variable (if there is one) has its latest value

3. Everything can be changed in the loop

4. <exp1> is evaluated once, but the other two are evaluated with each iteration

Notes: – The expressions can be statements

or statement sequences

for (i=0,j=10;j==i;i++)…

– If the second expression is absent,

it is an infinite loop

Counter-Controlled Loops

C Languages

The loop statement of the C languages is the most flexible, but there are differences mostly based on the while loop

C99,C++ for:

  • The control expression is coercised Boolean
  • The initial expression can include variable definitions (scope is from the definition to the end of the loop body)
  • Can branch into them

Java,C# for:

  • The control expression must be Boolean
  • Branching into the loop? Java no

C# yes

Counter-Controlled Loops

  • Should the language have a pretest loop or a posttest loop or both?
  • Should this be a special case of the counter-controlled loop or a completely separate statement?

Logically-Controlled Loops

Design Issues

C Languages have both while-do and do-while

Branching into either loop construct?

C,C++,C# yes

Java no

Ada has a pretest version, but no posttest

Fortran 95 has neither

Perl has two pretest logical loops,

while and until,

but no posttest logical loop

Logically-Controlled Loops

Language Examples

  • Should the conditional be part of the exit?
  • Should control be transferable, conditionally or unconditionally, out of more than one nested loop?

User-Located Loop Controls

Design Issues

Ada conditional or unconditional,

for any loop, any number of levels

User-Located Loop Controls

Language Examples

for … loop

exit [when …]

end loop

LOOP1: while … loop

LOOP2: for … loop

exit LOOP1 [when …]

end loop LOOP2;

end loop LOOP1;

C Languages

break

Unconditional, for any loop or switch

C,C++ one level only

Java,Perl,C# multiple levels, by labels

continue

Skips the remainder of this iteration,

but does not exit the loop

Fortran 95

Exit, Cycle

Unconditional

for any loop, any number of levels

User-Located Loop Controls

Language Examples

Concept

Use the order and number of elements of some data structure to control iteration

Control Mechanism

A function call (the iterator) that returns either the next element in a chosen order, if there is one, or else exits the loop

Languages

Specific structures: Perl, PHP, JavaScript, C#

Can be modeled in most languages by functions,

easily so in object-oriented languages

Data Structure Iteration

C,C++ traversing a linked list using pointers

for (p=hdr; p; p=next(p)){…}

C#

String[] strList = {″Bob″,″Carol″, …};

foreach (String name in strList)

System.Console.WriteLine(″Name: {0}″,name);

The foreach statement iterates the elements of any collection that implements IEnumerable

Data Structure Iteration

Examples

Perl has a built-in iterator for arrays and hashes

foreach $name (@names) { print $name }

PHP has predefined iterators to access its arrays

current points at one element of the array

next moves current to the next element

prev moves current to the previous element

reset moves current to the first element

Data Structure Iteration

Examples

C++ iterators are often implemented as friend functions or as separate iterator classes

Java objects of a class implementing the Collection interface can be iterated with an implementation of the Iterator interface

– next() moves the pointer into the collection

– hasNext() is a predicate

– remove() deletes an element

C# can iterate any collection implementing the

IEnumerate interface

– MoveNext() moves the pointer in the collection

– Reset() moves the pointer to the beginning

– Current is the current element

– Can also use foreach to iterate

Data Structure Iteration

User-defined Iteration

General Syntax

goto <Label>

Disadvantage readability

Advantage flexibility

Notes: All control structures can be

built with goto and a selector

Some languages do not have them

Modula-2, Java

Loop exit statements are

camouflaged goto’s

– they are severely restricted

– not harmful to readability

Unconditional Branching

…the quality of programmers is a decreasing function of the density of go to statements in the programs they produce….

…the go to statement should be abolished from all “higher level” programming languages…

The go to statement as it stands is just too primitive; it is too much an invitation to make a mess of one’s program….

Dijkstra, Edsger W., Go To Statement Considered Harmful. Communications of the ACM, Vol. 11, No. 3, March 1968, pp. 147-148.

“Go To Statement

Considered Harmful”

Dijkstra’s remark about the undesirability of the goto statement was not new, and was not final !

1959 Heinz Zemanek expressed doubts

1966 Wirth and Hoare “[multiple selection] mirrors the dynamic structure of a program more clearly than goto statements”

1966 Guiseppe Jacopini proved the (logical) superfluousness of the goto statement

1968 Dijkstra Communications of the ACM

1974 Knuth argued that there were occasions when the efficiency of the goto outweighed its harm to readability

1978 Kernighan and Ritchie called the goto “inifinitely abusable,” but included it in C anyway

“Go To Statement

Considered Harmful”

In computing and related disciplines, considered harmful is a phrase used in the titles of diatribes and other critical essays (there are more than 65 such works).

The March 1987 CACM contained a criticism of Dijkstra’s letter under the title

‘GOTO Considered Harmful’ Considered Harmful.

The May 1987 CACM printed further replies, both for and against, under the title

‘”GOTO Considered Harmful” Considered Harmful’ Considered Harmful?.

Dijkstra’s own response to this controversy was titled

On a somewhat disappointing correspondence.

‘GOTO Considered Harmful’ Considered Harmful

Edsger Dijkstra (1975, 1976)

Purpose

to support a new programming methodology

(verification during program development)

Idea

if the order of evaluation is not important,

the program should not specify one

Used in concurrent programming: CSP, Ada

Used in function definitions: Haskell

Guarded Commands

*

Syntax: if <boolean> -> <statement>

[] <boolean> -> <statement>

[] <boolean> -> <statement>

fi

[] is called a fatbar

Semantics: When this construct is reached:

  • Evaluate all boolean expressions
  • If more than one are true,

choose one nondeterministically

  • If none are true, runtime error

Guarded Commands

Selection

*

Selection Guarded Command
Flow Chart

*

examples (text pp. 367-368)

if i = 0 -> sum := sum + i

[] i > j -> sum := sum + j

[] j > i -> sum := sum + i

fi

if x >= y -> max := x

[] y >= x -> max := y

fi

Guarded Commands

Selection

*

Syntax: do <boolean> -> <statement>

[ ] <boolean> -> <statement>

[ ] <boolean> -> <statement>

od

Semantics: For each iteration:

  • Evaluate all boolean expressions
  • If more than one are true,

choose one nondeterministically;

then start loop again

  • If none are true, exit loop

Guarded Commands

Loop

*

Loop Guarded Command

Flow Chart

*

example (text p. 369)

arrange the values of q1, q2, q3, and q4 so that

q1 <= q2 <= q3 <= q4

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

[] q2 > q3 -> temp := q2; q2 := q3; q3 := temp

[] q3 > q4 -> temp := q3; q3 := q4; q3 := temp

od

Guarded Commands

Loop

*

Guarded Commands
Rationale

Connection between control statements and program verification is intimate

  • Verification is impossible with goto statements
  • Verification is possible with only selection and logical pretest loops
  • Verification is relatively simple with only guarded commands

*

Conclusions

Choice of control statements beyond selection and logical pretest loops is a trade-off between language size and writability

*

*

*

*

*

*

*

Order now and get 10% discount on all orders above $50 now!!The professional are ready and willing handle your assignment.

ORDER NOW »»