Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.

Similar presentations


Presentation on theme: "Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution."— Presentation transcript:

1 Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution of the program. while (i<100) Control Statement { fun(); Control Structure k++; }

2 Chapter 8 (Control Structure) Slide 2 Statement Level Control Composition: Statements are placed in a textual sequence, and they are executed in order. Alternation/Selection: Two or more sequence of statements form alternatives, so that one of the sequences is executed. Iteration/Loop: A sequence of statements may be executed repeatedly Jump/Branch: Control is transferred from one statement to another, which need not necessarily be placed in a textual sequence.

3 Chapter 8 (Control Structure) Slide 3 Two Way Selection Statements The “if” statement: if (expr) {... then clause } else {... else clause (Optional) } In Java, “expr” has to be either a relational expression or a Boolean expression.

4 Chapter 8 (Control Structure) Slide 4 Dangling Else Problem if (expr1) if (expr2) {... } else {... } The solution to the dangling else problem is to pair an else to the most recent unpaired if in the current block.

5 Chapter 8 (Control Structure) Slide 5 Multiple Selection Statements (1) If-elseif statementSwitch statement if (expr) {... then clause } elseif (expr) {... elseif clause } else {... else clause } switch (expr) { case v1:... case clause case v2:... case vn: default:... default clause }

6 Chapter 8 (Control Structure) Slide 6 Multiple Selection Statements (2) The if –elseif statement can be represented by a nested if-else statement. A switch statement can be represented by an if-elseif statement. The expression in a ‘switch’ must evaluate to an integer, and the value in a case clause must be a constant.

7 Chapter 8 (Control Structure) Slide 7 Iteration Statements An iterative statement causes a collection of statements to be executed zero, one, or more times. Pretest: if the condition for loop termination is tested at the top of the loop (before the statements). Posttest loop: the condition for exiting the loop is tested after the statements in the loop.

8 Chapter 8 (Control Structure) Slide 8 Counter-controlled Loop  Has a variable of a numeric type, called the loop variable in which the count value is maintained.  Has loop parameters specifying the initial and terminal values and optionally a step size, of the loop variable. These values determine the number of iterations performed.  The loop variable is not allowed to be modified inside the loop.

9 Chapter 8 (Control Structure) Slide 9 Counter controlled loop (Egs.) PASCAL: for k:= 1 to 10 do begin i:=i-1; p:=i*5; end FORTRAN: DO I = INIT, TERM, STEP K=K+1 P=K*2 END DO

10 Chapter 8 (Control Structure) Slide 10 C for-loop C uses the “for-loop” to emulate a counter controlled loop. It is not strictly a counted iteration - it does not have any of the characteristics of a counter controlled loop. for (k=1; k<=10; k++) {... } “k” is not a loop variable, since we can use any expression in its place, and the variable “k” is allowed to be modified inside the loop body.

11 Chapter 8 (Control Structure) Slide 11 Logically Controlled Loop Logically controlled loops are much simpler than counter controlled loops, and are based on the value of a logical expression. PretestPosttest while(expr) {... } do {... } while(expr); The posttest loop body will always be executed at least once.

12 Chapter 8 (Control Structure) Slide 12 C “for” loop for (expr1; expr2; expr3) {... } is equivalent to expr1; while(expr2) {... expr3; } All expressions above are optional. An expression can contain multiple statements separated by commas.

13 Chapter 8 (Control Structure) Slide 13 “for” loop in C++, Java C++ and Java allow variable declaration in the first expression. In C++, the scope of a variable declared in the first expression is from its definition to the end of the function in which it is defined. In Java, the scope of such a variable is that of the loop body. Java also has the restriction that the second expression must be either a relational or a Boolean expression.

14 Chapter 8 (Control Structure) Slide 14 Branching (Jump) “Go to” Statements must be avoided.  The statement level control provided by ‘Go to’ is unstructured.  ‘Go to’ statements severely affect readability of the code.  ‘Go to’ statements make the code more prone to errors, and hampers code development.

15 Chapter 8 (Control Structure) Slide 15 “Go to” (Example in C) for(...){... if(i==0) goto error: }... error:......

16 Chapter 8 (Control Structure) Slide 16 Advantages of “goto”  Direct hardware support for efficient execution.  Simple and completely general-purpose - can be used to implement any other control form.  Useful in abortive exits in iterations (eg. required to conditionally abandon processing in some deeply nested structure).  Sometimes useful in control branching in large program codes.  The break statement is a restricted form of unlabeled branch statement, which is used to leave the innermost block.


Download ppt "Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution."

Similar presentations


Ads by Google