1 / 19

Statement-level Control Structures

Statement-level Control Structures. The Flow of Control Among Statements. Introduction. Selection Statements Iterative Statements Unconditional Branching Guarded Commands. Introduction. Control statements provide two very important means of making programs flexible and powerful.

katen
Download Presentation

Statement-level 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. Statement-level Control Structures The Flow of Control Among Statements

  2. Introduction • Selection Statements • Iterative Statements • Unconditional Branching • Guarded Commands

  3. Introduction • Control statements provide two very important means of making programs flexible and powerful. • Some way of allowing different control flow paths. • A way of allowing for the repeated execution of statements or groups of statements. • A control structure is defined as a control statement and the collection of statements whose execution it controls.

  4. INTRODUCTION (Continued) • Bohm and Jacopini proved that all algorithms that can be diagrammed in a flowchart can be reproduced in a programming language with only the two previously mentioned control statements. • So why have more than these?

  5. Control Statement Design Issues • The one design issue which is common to all control and iteration structures is whether to allow multiple points of entry to the statement. • These multiple entry points are only possible in languages that allow for GOTO or statement labels. • Multiple exits from a control statement exist in most programming languages and they are not seen as a detriment either to readability or understanding.

  6. Selection Statements

  7. Selection Statements • Selection statements are grouped into two different categories. • Two-way control statements (if then) • n-way control statements

  8. Issues in Selection Statements • What type of expression controls the if statement • How are then and else clauses specified. • By curly braces. • With the keyword Then. • How should the meaning of nested selectors be specified. • Indention • Placed in a compound (Braces) • Using a reserved word to end the statement.

  9. Iterative Statements

  10. Iterative Statements • Counter-Controlled Loops • Logically Controlled Loops • User-Located Loop Control Mechanisms • Iteration Based on Data Structures

  11. Unconditional Branching

  12. Unconditional Branching • An unconditional branch statement transfers execution control to a specified location in the program. • Can be one of the most powerful statements in any language. • Leads to problems in readability since programs are not restricted to a traditional top to bottom flow of execution. • Can lead to higher maintenance costs and unreliability.

  13. Examples of Unconditional Branching Statements • GOTO • Switch • User-Controlled Loop Control Statements

  14. Guarded Commands

  15. Guarded Commands • A new and different form of selection and loop statement recommended by Dijkstra • Supports correctness in development. • Constructs can be considered independently • Increased clarity in reasoning • Allows for nondeterminism • Enables program verification

  16. Guarded Commands • Guarded commands consist of a boolean expression , the guard, and the statement or statements to be executed when it is found true. • Although it bears some resemblance to a multi-select statement, it differs in that any statement whose guard evaluates to true can be executed.

  17. Conclusions • Although Bohm and Jacopini proved that only selection and pretest logical loops were absolutely necessary to follow an flow chart sequence, no modern language has taken the step to remove other control structures. • Additional control structures which have been suggested in the past may have not been included because doing so would increase the size and complexity of languages.

  18. Conclusions (continued) • Control structures should be simple. • They should have one entrance although having more than one exit from a control structure is standard and is not seen as a hindrance to readability. • GOTO is regarded as evil , however many modern languages still allow it, including C#.

  19. SUMMARY • There are several different categories of control structures. • Selection • Multiple selection • Iterative • Unconditional branching • Looping structures vary among languages • Do • For • Foreach

More Related