1 / 14

Control Structures

Control Structures. Types of Structured Sequential Control. Types of Unstructured Control.  GOTO.  coroutines. Asynchronous Control (stay tuned). Concurrent Control (stay tuned). Simple Sequence - execute instructions in the written order.

Télécharger la présentation

Control Structures

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Structures Types of Structured Sequential Control Types of Unstructured Control  GOTO  coroutines Asynchronous Control (stay tuned) Concurrent Control (stay tuned)

  2. Simple Sequence - execute instructions in the written order  What instructions are inherently sequential?  Why {...} notation (or begin ... end)? Selection - choose between alternative code clauses  What are the two forms in Java/C++ ?  Distinguishing characteristics between selection control structures:  Examples:  if -- Java  CASE -- Modula-2  COND -- Scheme  arithmetic-IF -- FORTRAN IF (expression) 100, 200, 300

  3. Which IF? Java/C/C++ if (condition) thenClause; else elseClause; Pascal/Algol 60 ifconditionthen thenClause else elseClause Modula-2/Ada ifcondition thenClause; else elseClause; end if

  4. Language Generations & IF 1st Generation  IF ≈ optional branch (2-way selection) • examples: conditional branches -- assembler logical IF -- FORTRAN IF -- BASIC • concern: branching into the midst of a clause 2nd Generation  if-then-else replaces if-then  examples: Pascal & C  concern: dangling else

  5. The Dangling Else Problem When does the else clause execute? if (condition1) if (condition2) thenClause; else elseClause; How to write the code as indented?

  6. Language Generations & IF 1st Generation  IF either branches or not (2-way selection) • examples: conditional branches -- assembler logical IF -- FORTRAN IF -- BASIC • concern: branching into the midst of a clause 2nd Generation  if-then-else replaces if-then  examples: Pascal & C  concern: dangling else 3rd Generation  elseif option is included  examples: Algol 68, Ada & Modula-2

  7. Multi-way Selection Computed-GOTO -- FORTRAN GOTO (10, 20, 30, 40), integerExpression 10 Instruction1 ... GOTO 100 20 Instruction2 ... GOTO 100 30 Instruction3 ... GOTO 100 40 Instruction4 ... GOTO 100 100 CONTINUE

  8. OR SELECT; WHEN(Condition1) Instruction1; WHEN(Condition2) Instruction2; ... OTHERWISE InstructionN; END; Select -- PL/1 SELECT (Expression); WHEN(0, 1) Instruction1; WHEN(7) Instruction2; ... OTHERWISE InstructionN; END;

  9. switch -- C switch (Expression) { case constant1: Instruction1; break; case constant2: case constant3: Instruction1; break; ... default: InstructionN; break; }

  10. Non-deterministic Selection Non-deterministic code has alternative valid behavior under the same circumstances. Guarded-IF (by Edsgar Dijkstra) ifcondition1  clause1  condition2  clause2  condition3  clause3 ...  conditionN  clauseN fi Ada Select select when condition1 => clause1 or whencondition2 => clause2 or whencondition3 => clause3 ... or whenconditionN => clauseN end select

  11. Repetition Control Structures - execute a body repeatedly • top of loop (while) • bottom of loop (repeat/do)   EXIT -- Modula-2 & Ada  break -- C et. al.  LEAVE -- PL/1 (What occurs when a continue instruction (C) executed?)  control structures run amuck - PL/1 DO J=1 TO 10 BY 2, 17, 23 WHILE (X>0); ... IF (Y=1) THEN LEAVE; ... END;

  12. Under what circumstances do these Modula-2 loops behave differently? FOR J:=1 TO A[N] DO loopBody; END; J:=1; WHILE J<=A[N] DO loopBody; J:=J+1; END; The Counting Loop -- What hath FORTRAN wrought? Early versions of FORTRAN had one loop construct. DO 1000 j=1,20,1 ... 1000 CONTINUE Modern versions of this loop are called for loops. Modula-2 restrictions. Ada version: for ControlVar in7..20 loop ... end loop

  13. Zahn’s Event Indicator Skeleton loop untilE1or ... orEN loopBody repeat then E1 : Instruction1; ... EN : InstructionN; fi BNF <eventLoop>loop until <eventList> <instructions> repeat then <signalList> fi <eventList><eventName> | <eventName> or <eventList> <signalList><signalCase> | <signalCase> <signalList> <signalCase><eventName> : <instructions> ;

  14. Example: sequential array search j := 1; loop until Found or NotThere begin if j>arraySize then NotThere; if A[j]=lookupValue then Found; j := j + 1; end repeat then Found: index := j; NotThere: begin /* append value to array end */ arraySize := arraySize + 1; A[arraySize] := lookupValue; index := arraySize; end fi /*assert: A[index] = lookupValue */

More Related