Chapter 8 Control Structure Control Structures Control structures

  • Slides: 16
Download presentation
Chapter 8 (Control Structure) Control Structures Control structures are used by the programmer to

Chapter 8 (Control Structure) 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(); k++; Control Structure } 1

Chapter 8 (Control Structure) Statement Level Control • Composition: Statements are placed in a

Chapter 8 (Control Structure) 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. 2

Chapter 8 (Control Structure) Two Way Selection Statements The “if” statement: if (expr) {.

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

Chapter 8 (Control Structure) Dangling Else Problem if (expr 1) if (expr 2) {.

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

Chapter 8 (Control Structure) Multiple Selection Statements (1) If-elseif statement if (expr) {. .

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

Chapter 8 (Control Structure) Multiple Selection Statements (2) • The if –elseif statement can

Chapter 8 (Control Structure) 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. 6

Chapter 8 (Control Structure) Iteration Statements • An iterative statement causes a collection of

Chapter 8 (Control Structure) 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. 7

Chapter 8 (Control Structure) Counter-controlled Loop § Has a variable of a numeric type,

Chapter 8 (Control Structure) 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. 8

Chapter 8 (Control Structure) Counter controlled loop (Egs. ) PASCAL: for k: = 1

Chapter 8 (Control Structure) 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 9

Chapter 8 (Control Structure) C for-loop C uses the “for-loop” to emulate a counter

Chapter 8 (Control Structure) 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. 10

Chapter 8 (Control Structure) Logically Controlled Loop Logically controlled loops are much simpler than

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

Chapter 8 (Control Structure) C “for” loop for (expr 1; expr 2; expr 3)

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

Chapter 8 (Control Structure) “for” loop in C++, Java • C++ and Java allow

Chapter 8 (Control Structure) “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. 13

Chapter 8 (Control Structure) Branching (Jump) “Go to” Statements must be avoided. § The

Chapter 8 (Control Structure) 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. 14

Chapter 8 (Control Structure) “Go to” (Example in C) for(. . . ){. .

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

Chapter 8 (Control Structure) Advantages of “goto” § Direct hardware support for efficient execution.

Chapter 8 (Control Structure) 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. 16