180 likes | 233 Views
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.
E N D
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 • 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.
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.
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.
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.
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.
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.
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
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.
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.
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.
“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.
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.
“Go to” (Example in C) for(...){ for(...){ for(...){ ... if(i==0) goto error: } } } ... ... error: ... ...
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.