Control explanations .

Uploaded on:
Category: Sports / Games
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
Slide 1

Control articulations Simple statements Basic organized explanations Sequence Selection Iteration The hop proclamation

Slide 2

Simple proclamations in basic dialects These are nuclear (win big or bust) operations: task, the void proclamation, a methodology call, exit, next, break, proceed go to (hop). A piece is likewise a win or bust operation.

Slide 3

Structured proclamations Three key components permit us to gathering straightforward explanations into organized articulations. succession , or the compound proclamation: { S1 S2 } determination , or the restrictive articulation: if (C) S1 else S2 cycle , or the circle explanation: while (C) S

Slide 4

Structured articulations (2) All other control structures can be effectively gotten from the three fundamental systems. on the off chance that ( C ) S  if ( C ) S else {} do S while ( C )  S while ( C ) S switch (i) { if (i == C1 ) S1 case C1 : S1 …  else … et cetera.

Slide 5

Sequence (1) Languages Mechanisms Algol, Pascal, Ada, ... begin ... end C, Java, Perl { ... } Fortran IV nothing Prolog implicit a :- b, c, d. This implies assessing b, then c, then d. Scheme (begin ...)

Slide 6

single section, single leave Sequence (2) A compound proclamation is dealt with as a straightforward explanation. This depends on a vital deliberation standard . The inward structure can be "disconnected away".

Slide 7

Selection The if-else build is available in all programming dialects (Prolog is the main real special case). Modula and Ada were the first to have legitimately sectioned if-then-else, with four watchwords around three components of the choice: if C then S1 else S2 end if Nested choice if-elsif-...- else was additionally presented in Ada: if C1 then S1 elsif C2 then S2 ...... elsif Cn then Sn else Sn+1 end if

Slide 8

Special types of choice in Fortran IV Computed GO TO. GO TO (mark 1 , ..., name n ), expression Assigned GO TO. Dole out name i TO variable GO TO variable(label 1 , ..., name n )

Slide 9

Special types of determination (2) The switch proclamation in C and Java has been presumably roused by figured GO TO. switch(expression){ case const 1 : S 1 ; ... case const n : S n ; default: S n+1 ;} After S i has been executed, control "falls through" to the resulting case: S i+1 is executed next. Fall-through can be maintained a strategic distance from by including break articulations.

Slide 10

Special types of determination (3) Case proclamation in Pascal, Ada and other comparative dialects: every case is particular, there is no "fall-through". In Ada: case expression is when constantList 1 => S 1 ; ... at the point when constantList n => S n ; when others => S n+1 ; end case;

Slide 11

Special types of choice (4) Selection in Prolog is driven by achievement and disappointment, not by the genuine false restriction. Choice is understood in backtracking: on the off chance that you succeed, stop; if not, attempt another decision . union( [Elem | S1], S2, S1_S2 ) :- part( Elem, S2 ), union( S1, S2, S1_S2 ). union( [Elem | S1], S2, [Elem | S1_S2] ) :- \+ part( Elem, S2 ), union( S1, S2, S1_S2 ). union( [], S2, S2 ).

Slide 12

Y N Graphical representation flowgraphs — stream charts — flowcharts if–then–else if–then C S 1 S 2 S

Slide 13

Y N C S 1 S 2 Graphical representation (2) The deliberation guideline: if ( C ) S1 else S2 is a basic articulation. Single section, single exit

Slide 14

N C 1 C 2 C 3 C n … Y S 1 S 2 S 3 S n S n+1 Graphical representation (3) if–then–elsif-… elsif-then-else

Slide 15

N e=v 1 e=v 2 e=v 3 e=v n … Y S 1 S 2 S 3 S n S n+1 Graphical representation (4) case e of v1: S1; ... else Sn+1 end

Slide 16

Iteration Variations: pre test cycle or pos t test emphasis. while C do S Pascal rehash S until C while ( C ) S Java do S while ( C ) while C circle S end circle; Ada (no posttest cycle)

Slide 17

Iteration (2) In Ada, the prefix while C is an augmentation of the essential iterative proclamation: circle S end circle; Another prefix: for i in range The uncovered circle articulation must be halted from inside the circle. Constrained leave shuts the closest cycle: exit; unconditional leave when C ;

Slide 18

Iteration (3) The while prefix is a condensing. The development while C circle S end circle; is equal to circle exit when not C ; S end circle;

Slide 19

SUM := 0; circle get(X); exit when X = 0; SUM := SUM + X; end circle; Simpler, more instinctive SUM := 0; get(X); while X/= 0 circle SUM := SUM + X; get(X); end circle; Condition turned around, get(X) shows up twice Example: utilization of exit

Slide 20

S Y N C S ¬C S N Y C Graphical representation while-do rehash until rehash until

Slide 21

S 1 Y C N S 2 Graphical representation (2) circle - exit - end circle

Slide 22

for circles For-circles ("counter-controlled") are truly prior and less broad than condition-controlled iterative structures. DO 1000 var = lo , hello there Fortran IV ... 1000 CONTINUE DO name var = lo , hello , incr for var := expr do S Algol 60 for var := low stride incr until high do S for var := expr while C do S Iterators can be joined: for i := 0, i+1 while i ≤ n do S(i)

Slide 23

for circles (2) for var in range Ada circle S end circle; for var in invert extend circle S end circle; for ( e1 ; e2 ; e3 ) S C, Java, Perl (enough said J ) What is this? for (;;) S

Slide 24

for circles 32) Iteration in Prolog and in Scheme is communicated by recursion. The same, obviously, is conceivable in most other run of the mill dialects.

Slide 25

Jump (the goto articulation) Unconstrained exchange of control is the main system accessible in low-level dialects — however they are exceptionally broad. One-branch choice and goto permit us to express all other control structures. The hop system is risky, may hurt coherence, and ought to be maintained a strategic distance from — propelled control structures function admirably for all run of the mill and for most less average employments.

Slide 26

Jump (the goto statemen)t (2) Some dialects limit goto (don\'t permit hopping inside a cycle or determination) and make it difficult to utilize. Ada makes names obvious from far away (so that your manager can see it!).: SUM := 0; loop get(X); if X = 0 then goto DONE ; end if; SUM := SUM + X; end circle; <<DONE>> put(SUM); goto may leave "unfinished business" — dynamic control structures that must be "folded" immediately.

View more...