1 / 16

Control Structures

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++; }. Statement Level Control.

nmorrison
Download Presentation

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 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. 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. 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. 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. Multiple Selection Statements (1)

  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. 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. 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. 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. 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. Logically Controlled Loop Logically controlled loops are much simpler than counter controlled loops, and are based on the value of a logical expression. The posttest loop body will always be executed at least once.

  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. “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. 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. “Go to” (Example in C) for(...){ for(...){ for(...){ ... if(i==0) goto error: } } } ... ... error: ... ...

  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.

More Related