control statements l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Control statements PowerPoint Presentation
Download Presentation
Control statements

Loading in 2 Seconds...

play fullscreen
1 / 26

Control statements - PowerPoint PPT Presentation


  • 306 Views
  • Uploaded on

Control statements. Simple statements Basic structured statements Sequence Selection Iteration The jump statement. Simple statements in imperative languages. These are atomic (all or nothing) operations: assignment, the empty statement, a procedure call, exit, next, break, continue

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Control statements' - arleen


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
control statements
Control statements
  • Simple statements
  • Basic structured statements
  • Sequence
  • Selection
  • Iteration
  • The jump statement
simple statements in imperative languages
Simple statementsin imperative languages

These are atomic (all or nothing) operations:

  • assignment,
  • the empty statement,
  • a procedure call,
  • exit, next, break, continue
  • go to (jump).

A block is also an all-or-nothing operation.

structured statements
Structured statements

Three fundamental mechanisms allow us to group simple statements into structured statements.

  • sequence, or the compound statement:

{ S1 S2 }

  • selection, or the conditional statement:

if (C) S1 else S2

  • iteration, or the loop statement:

while (C) S

structured statements 2
Structured statements (2)

All other control structures can be easily derived from the three basic mechanisms.

if (C) S if (C) S else {}

do S while (C) S while (C) S

switch (i) { if (i == C1) S1case C1: S1… else …

and so on.

sequence 1
Sequence (1)

Languages Mechanisms

Algol, Pascal, Ada, ... begin ... end

C, Java, Perl { ... }

Fortran IV nothing

Prolog implicit

a :- b, c, d.This means evaluating b, then c, then d.

Scheme (begin ...)

sequence 2

single entry, single exit

Sequence (2)

A compound statement is treated as a simple statement. This is based on an important abstraction principle.

The inner structure can be “abstracted away”.

selection
Selection

The if-else construct is present in almost all programming languages (Prolog is the only major exception). Modula and Ada were the first to have properly bracketed if-then-else, with four keywords around three elements of the selection:

if C then S1 else S2 end if

Nested selection if-elsif-...-else was also introduced in Ada:

if C1 then S1 elsif C2 then S2 ...... elsif Cn then Sn else Sn+1 end if

special forms of selection in fortran iv
Special forms of selection in Fortran IV

Computed GO TO.

GO TO (label1, ..., labeln), expression

Assigned GO TO.

ASSIGN labeli TO variable

GO TO variable(label1, ..., labeln)

special forms of selection 2
Special forms of selection (2)

The switch statement in C and Java has been probably inspired by computed GO TO.

switch(expression){ case const1: S1; ... case constn: Sn; default: Sn+1;}

After Si has been executed, control "falls through" to the subsequent case: Si+1 is executed next.

Fall-through can be avoided by adding break statements.

special forms of selection 3
Special forms of selection (3)

Case statement in Pascal, Ada and other similar languages: each case is separate, there is no "fall-through". In Ada:

case expression is when constantList1 => S1; ... when constantListn => Sn; when others => Sn+1;end case;

special forms of selection 4
Special forms of selection (4)

Selection in Prolog is driven by success and failure, not by the true-false opposition.

Selection is implicit in backtracking: if you succeed, stop; if not, try another choice .

union( [Elem | S1], S2, S1_S2 ) :- member( Elem, S2 ), union( S1, S2, S1_S2 ).

union( [Elem | S1], S2, [Elem | S1_S2] ) :- \+ member( Elem, S2 ), union( S1, S2, S1_S2 ).

union( [], S2, S2 ).

graphical representation

Y

Y

N

N

Graphical representation

flowgraphs — flow diagrams — flowcharts

if–then–else

if–then

C

C

S1

S2

S

graphical representation 2

Y

N

C

S1

S2

Graphical representation (2)

The abstraction principle:if ( C ) S1 else S2is a simple statement.

Single entry,single exit

graphical representation 3

N

N

N

C1

C2

C3

Cn

Y

Y

Y

Y

S1

S2

S3

Sn

Sn+1

Graphical representation (3)

if–then–elsif-…elsif-then-else

graphical representation 4

N

N

N

e=v1

e=v2

e=v3

e=vn

Y

Y

Y

Y

S1

S2

S3

Sn

Sn+1

Graphical representation (4)

case e of v1: S1; ... else Sn+1 end

iteration
Iteration

Variations: pretest iteration or posttest iteration.

while C do S Pascal

repeat S until C

while (C) SJava

do S while (C)

while C loop S end loop; Ada

(no posttest iteration)

iteration 2
Iteration (2)

In Ada, the prefix while Cis an extension of the basic iterative statement:

loop S end loop;

Another prefix:

for i in range

The bare loop statement must be stopped from inside the loop.

Forced exit closes the nearest iteration:

exit; unconditional

exit when C;

iteration 3
Iteration (3)

The while prefix is an abbreviation. The construction

while C loop S end loop;

is equivalent to

loop exit when not C;S end loop;

example use of exit
SUM := 0;

loop

get(X);

exit when X = 0;

SUM := SUM + X;

end loop;

Simpler, more intuitive

SUM := 0;

get(X);

while X /= 0 loop

SUM := SUM + X;

get(X);

end loop;

Condition reversed, get(X) appears twice

Example: use of exit
graphical representation20

S

Y

Y

N

N

C

S

¬C

S

S

N

Y

C

Graphical representation

while-do

repeat-until

repeat-until

graphical representation 221

S1

Y

C

N

S2

Graphical representation (2)

loop - exit - end loop

for loops
for loops

For-loops ("counter-controlled") are historically earlier and less general than condition-controlled iterative structures.

DO 1000var = lo, hiFortran IV

...

1000 CONTINUE

DO label var = lo, hi, incr

for var := expr do SAlgol 60

for var := low step incr until high do S

for var := expr while C do S

Iterators can be combined:

for i := 0, i+1 while i ≤ n do S(i)

for loops 2
for loops (2)

for var in range Adaloop S end loop;

for var in reverse rangeloop S end loop;

for (e1; e2; e3) S C, Java, Perl

(enough said J)

What is this?

for (;;) S

for loops 32
for loops 32)

Iteration in Prolog and in Scheme is expressed by recursion. The same, of course, is possible in most other typical languages.

jump the goto statement
Jump (the goto statement)

Unconstrained transfer of control is the only mechanism available in low-level languages — but they are very general. One-branch selection and goto allow us to express all other control structures.

The jump mechanism is dangerous, may hurt readability, and should be avoided — advanced control structures work well for all typical and for most less typical uses.

jump the goto statemen t 2
Jump (the goto statemen)t (2)

Some languages restrict goto (do not allow jumping inside an iteration or selection) and make it hard to use.

Ada makes labels visible from far away (so that your boss can see it!).:

SUM := 0; loop get(X); if X = 0 then goto DONE; end if; SUM := SUM + X; end loop;<<DONE>> put(SUM);

goto may leave “unfinished business” — active control structures that must be "folded" at once.