SlideServe
Menu
Préférences
© SlideServe · v2026

Control Structures

kiral avatar
25 diapositives · 451 Views · Aug 10, 2014

This guide explores the control structures fundamental to imperative programming languages, tracing their evolution from Algol 60 to modern languages like C, Java, and Python. Key control structures include sequences, conditionals (if and case statements), loops (while and for), and jumps (goto and exceptions). We also address ambiguities in expression evaluation regarding operator precedence and branching scenarios. The discussion covers the distinct characteristics of assembly language control structures, the importance of structured programming, and practical implementations of loops and conditionals across various languages.

Control Structures - slide 1
01 / 25Control Structures
1 / 17

Télécharger la présentation

Control Structures
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Control Structures

  2. Hierarchical Statement Structure • Standard in imperative languages since Algol60. • Exceptions: Early FORTRAN, COBOL, early BASIC, APL. (Fortran kludged definite loops.)

  3. Control structures within function • Sequence: { S1, S2 … Sk } • Conditional: if statement, case statement. • Loop: while loop, for loop. • Jump: goto, exception raising

  4. Expression evaluation Ambiguity of grouping: is 1+2*3 interpreted as (1+2)*3=9 or 1+(2*3)=7? Solutions: • Operator precedence. * binds more tightly than +, so 1+2*3 = 7. • Order precedence. Always compute left to right so 1+2*3=(1+2)*3 = 9 Best to use a lot of parentheses.

  5. Short circuit evaluation If Y <> 0 and X/Y > 5, compute Z. • if (Y <> 0 and X/Y > 5) Z = … --- but operators evaluate their arguments Solutions: • Lazy evaluation of Booleans: if (Y <> 0 && X/Y > 5) Z = … • Explicit conditionals if (Y <> 0) then if (X/Y > 5) Z= …

  6. Prefix and postfix notation Prefix: Operator precedes its arguments Postfix: Operator follows its arguments e.g. (1+3*N)/[(1+N)*(1+2*N)] Prefix: / + 1 * 3 N *+ 1 N + 1 * 2 N Postfix: 1 3 N * + 1 N + 1 2 N * + * / If each operator has a fixed number of arguments, then prefix and postfix are unambiguous with no need for brackets, precedence, or associativity conventions.

  7. Control structure between functions • Function call and return • Coroutine yield • Parallel synchronization • Exception raising

  8. Assembly language In assembly language, (essentially) the only control structures are: • Progression: Move to the next statement (increment the program counter). • Unconditional jump: JMP A Jump to address A • Conditional jump: JMZ R,A If (R==0) then jump to A Possible forms of conditions and addresses vary.

  9. Sequence • Pascal: begin … end • C, C++, Java: { … } • Ada: Brackets for sequence are unnecessary. Keywords for control structures suffice. for J in 1 .. N loop … end loop • ABC, Python: Indicate structure by indentation.

  10. Semicolons • Pascal: Semicolons are separators • C etc.: Semicolons are terminators begin X := 1; { X = 1; Y := 2 Y = 2; end }

  11. Conditionals • if Condition then Statement -- Pascal, Ada • if (Condition) Statement -- C, C++, Java • To avoid ambiguities, use end marker: end if, “}” • To deal with alternatives, use keyword or bracketing: if Conditions if (Conditions) then Statements { Statements } elseif Conditions else if (Conditions) then Statements then { Statements} else Statements else { Statements } end if

  12. if-else ambiguity if Conditions if (Conditions) thenthen { if Conditions if (Conditions) then Statements { Statements } else Statements else { Statements } end if } end if

  13. Compiling conditionals if (Cond) then Statement1 else Statement2 Assembly: …. Compute Cond in register R JMZ R,L … Code for Statement1 JMP END L: … Code for Statement2 END: … Next statement

  14. Multi-way selection “The case statement is the most useful control structure because most programs are interpreters.” (Ed Schonberg) Can achieve same with conditionals, but case statements are clearer. case (NextChar) { I: N=1; V: N=5; X: N=10 L: N=50 }

  15. The well-structured case statement • Discrete type. • No flow-through (hideous C misdesign). • Every value is in exactly one option. • Default option for values not covered. • Labels are computable at compile time.

  16. Implementation • Finite set of possibilities: can build a table of addresses, and • convert expression into table index: • compute value • transform into index • retrieve address of corresponding code fragment • branch to code fragment and execute • branch to end of case statement

  17. Loops • while loop: test at beginning while (Condition) Statement • repeat loop: test at end repeat Statement until Condition do Statement while (Condition) • breaking out: test in middle loop Statement if (condition) then exitloop; Statement end loop

  18. Multiple exits If you want to exit from imbedded loops, you need to specify which one you’re exiting. Ada solution: Label loops Outer: while C1 loop … Inner: while C2 loop … Innermost: while C3 loop … exit Outer when MajorFailure; exit Inner when SmallAnnoyance; end loop Innermost; end loop Inner; end loop Outer;

  19. Definite loops for J in 1 .. 10 loop … end loop for (int I=0; I < N; I++) … Design issues: • Evaluation of bounds (only at start, since Algol60) • Scope of loop variables • Empty bodies • Increments other than 1 • Backward iteration • Non-numeric domains

  20. Definite loops Since the C for loop for (start; test; increment) body; allows arbitrary test conditions and arbitrary increment actions, this is not really a definite loop at all, just an distinctively formatted while loop. However, the compiler is sensitive to the particular construction for (int I=0; I < N; I++) and optimizes the code for this.

  21. C: break and continue break: exit innermost loop (exitloop) continue: go to next iteration of innermost loop.

  22. Loop counter • Local to loop? • If not, what is the value on exit? • Settable in body of loop? • Multiple loop counters?

  23. Non-numeric domains Ada: for M in months loop … end loop Other data types: iterator = Collection.iterator(); element thing = iterator.first; while (iterator.moreElements()) { Body; thing = interator.next(); }

  24. Implementation of loops loop Statement1; if (condition) exit loop; Statement2; end loop L1: … Code for Statement1 … Compute not(condition) and put in R; JMZ R,L2 … Code for Statement2 JMP L1 L2: Next statement

  25. Gotos If you don’t have hierarchical statement structure, you need goto’s. “Go To Statements Considered Harmful” --- Dijkstra, 1968 Many more recent languages (e.g. Java) don’t have them. Certainly must prohibit jumping into the middle of a embedded structure; e.g. { … go to L for (int I=0; I<N; I++) { … L: …} }