Chapter 8 Statement-Level Control Structures PDF
Document Details
Uploaded by AccomplishedUniverse8955
Tags
Summary
This document provides an overview and discussion of statement-level control structures in programming. It details selection statements, like if-then-else, and iterative statements, like for and while loops. Various programming constructs and design issues for each one are also noted.
Full Transcript
CHAPTER 8 STATEMENT-LEVEL CONTROL STRUCTURES All Design issue is not with you Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Introduction A control structure is a control statement and the statements whose execution it controls....
CHAPTER 8 STATEMENT-LEVEL CONTROL STRUCTURES All Design issue is not with you Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Introduction A control structure is a control statement and the statements whose execution it controls. – Selection Statements – Iterative Statements – Unconditional Branching Overall Design Question: – What control statements Selection Statements A selection statement provides the means of choosing between two or more paths of execution. Two general categories: – Two-way selectors – Multiple-way selectors Two-Way Selection Statements – The general form of a two-way selector is as follows: if control_expression then clause else clause Design issues What is the form and type of the control expression? How are the then and else clauses specified? How should the meaning of nested selectors be specified? The control Expression Control expressions are specified in parenthesis if the then reserved word is not used to introduce the then clause, as in the C-based languages. In C89, which did not have a Boolean data type, arithmetic expressions were used as control expressions. In contemporary languages, such as Java and C#, only Boolean expressions can be used for control expressions. Fortran includes a three-way selector named the arithmetic IF that uses an arithmetic expression for control. It causes control to go to one of three different labeled statements, depending on whether the value of its control expression is negative, zero, or greater than zero. This statement is on the obsolescent feature list of Fortran 95. Clause Form In most contemporary languages, the then and else clauses either appear as single statements or compound statements. C-based languages use braces to form compound statements. In Ada the last clause in a selection construct is terminated with end and if. One exception is Perl, in which all then and else clauses must be compound statements, even if they contain single statements. Nesting Selectors In Java and contemporary languages, the static semantics of the language specify that the else clause is always paired with the nearest unpaired then clause. A rule, rather than a syntactic entity, is used to provide the disambiguation. So in the above example, the else clause would be the alternative to the second then clause. To force the alternative semantics in Java, a different syntactic form is required, in which the inner if-then is put in a compound, as in Multiple Selection Constructs The multiple selection construct allows the selection of one of any number of statements or statement groups. Design Issues What is the form and type of the control expression? How are the selectable segments specified? Is execution flow through the structure restricted to include just a single selectable segment? What is done about unrepresented expression values? Examples of Multiple Selectors The C, C++, and Java switch The control expression and the constant expressions are integer type. The switch construct does not provide implicit branches at the end of those code segments. Selectable segments can be statement sequences, blocks, or compound statements. Any number of segments can be executed in one execution of the construct (a trade-off between reliability and flexibility—convenience.) To avoid it, the programmer must supply a break statement for each segment. default clause is for unrepresented values (if there is no default, the whole statement does nothing.) C# switch statement rule disallows the implicit execution of more than one segment. The rule is that every selectable segment must end with an explicit unconditional branch statement, either a break, which transfers control out of the switch construct, or a goto, which can transfer control to on of the selectable segments. C# switch statement example: Multiple Selection Using if Early Multiple Selectors: – FORTRAN arithmetic IF (a three-way selector) IF (arithmetic expression) N1, N2, N3 Bad aspects: – Not encapsulated (selectable segments could be anywhere) – Segments require GOTOs – FORTRAN computed GOTO and assigned GOTO To alleviate the poor readability of deeply nested two-way selectors, Ada has been extended specifically for this use. The elsif version is the more readable of the two. This example is not easily simulated with a switch-case statement, because each selectable statement is chosen on the basis of a Boolean expression. In fact, none of the multiple selectors in contemporary languages are as general as the if-then-elsif construct. Iterative Statement The repeated execution of a statement or compound statement is accomplished by iteration zero, one, or more times. Iteration is the very essence of the power of computer. The repeated execution of a statement is often accomplished in a functional language by recursion rather then by iteration. General design issues for iteration control statements: – 1. How is iteration controlled? – 2. Where is the control mechanism in the loop? Design issues The primary possibilities for iteration control are logical, counting, or a combination of the two. The main choices for the location of the control mechanism are the top of the loop or the bottom of the loop. The body of a loop is the collection of statements whose execution is controlled by the iteration statement. The term pretest means that the loop completion occurs before the loop body is executed. The term posttest means that the loop completion occurs after the loop body is executed. The iteration statement and the associated loop body together form an iteration construct. Counter-Controlled Loops A counting iterative control statement has a v arariable, called the loop var, in which the count value is maintained. It also includes means of specifying the intial and terminal values of the loop var, and the difference between sequential loop var values, called the stepsize. The intial, terminal and stepsize are called the loop parameters. Design Issues: What are the type and scope of the loop variable? What is the value of the loop variable at loop termination? Should it be legal for the loop variable or loop parameters to be changed in the loop body, and if so, does the change affect loop control? Should the loop parameters be evaluated only once, or once for every iteration? The for Statement of the C-Based Languages C’s for is more flexible than the counting loop statements of Fortran and Ada, because each of the expressions can comprise multiple statements, which in turn allow multiple loop vars that can be of any type. Logically Controlled Loops Design Issues: – 1. Pretest or posttest? – 2. Should this be a special case of the counting loop statement (or a separate statement)? C and C++ also have both, but the control expression for the posttest version is treated just like in the pretest case (while - do and do - while) These two statements forms are exemplified by the following C# code: The only real difference between the do and the while is that the do always causes the loop body to be executed at least once. Java does not have a goto, the loop bodies cannot be entered anywhere but at their beginning. User-Located Loop Control Mechanisms It is sometimes convenient for a programmer to choose a location for loop control other than the top or bottom of the loop. Design issues: – Should the conditional be part of the exit? – Should control be transferable out of more than one loop? C and C++ have unconditional unlabeled exits (break). Java, Perl, and C# have unconditional labeled exits (break in Java and C#, last in Perl). The following is an example of nested loops in C#: C and C++ include an unlabeled control statement, continue, that transfers control to the control mechanism of the smallest enclosing loop. This is not an exit but rather a way to skip the rest of the loop statements on the current iteration without terminating the loop structure. Ex: Unconditional Branching An unconditional branch statement transfers execution control to a specified place in the program. with Unconditional Branching: The unconditional branch, or goto, is the most powerful statement for controlling the flow of execution of a program’s statements. However, using the goto carelessly can lead to serious problems. Without restrictions on use, imposed either by language design or programming standards, goto statements can make programs virtually unreadable, and as a result, highly unreliable and difficult to maintain. There problems follow directly from a goto’s capability of forcing any program statement to follow any other in execution sequence, regardless of whether the statement proceeds or follows the first in textual order. Java doesn’t have a goto. However, most currently popular languages include a goto statement. C# uses goto in the switch statement.