Session 6 : Statement-Level Control Structures

Control Structure
What is control structure? A control structure is a control statement and the collection of statements whose execution it controls.
there are two kinds of control structure :
- Selection statements
- Iterative 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
- In C89, C99, Python, and C++, the control expression can be arithmetic. But In most other languages, the control expression must be Boolean.

Two-Way Selection Statement
the general form two-way selection statement :
if control_expression
then clause
else clause
for example : nesting selectors
There are different ways to code nesting selectors in various programming language such as java, phyton, ruby, F#, and C.
Java example
if (sum == 0)
       if (count == 0)
           result = 0;
    else result = 1;
Ruby example
if a > b then
 sum = sum + a
 acount = acount + 1
else
 sum = sum + b
 bcount = bcount + 1
end

Multiple-selection statment
The multiple-selection statement allows the selection of one of any number of statements or statement groups. for the example is switch case and if else if else.

Iterative Statements
An iterative statement is one that causes a statement or collection of statements to be executed zero, one, or more times. An iterative statement is often called a loop.
Iterative statements has four types, there are :
- Counter-Controlled Loops
 Counter-controlled loops in imperative languages use a counter variable, but such variables do not exist in pure functional languages. Rather than iteration to  control repetition, functional languages use recursion. Rather than a statement, functional languages use a recursive function. Counting loops can be simulated in  functional languages as follows: The counter can be a parameter for a function that repeatedly executes the loop body, which can be specified in a second function  sent to the loop function as a parameter. So, such a loop function takes the body function and the number of repetitions as parameters.
- Logically-Controlled Loops 
 In many cases, collections of statements must be repeatedly executed, but the repetition control is based on a Boolean expression rather than a counter. For these  situations, a logically controlled loop is convenient. Actually, logically controlled loops are more general than counter-controlled loops. Every counting loop can be  built with a logical loop, but the reverse is not true. Also, recall that only selection and logical loops are essential to express the control structure of any  flowchart.
- User-Located Loop Control Mechanisms
 In some situations, it is convenient for a programmer to choose a location for loop control other than the top or bottom of the loop body. As a result, some languages  provide this capability. A syntactic mechanism for user-located loop control can be relatively simple, so its design is not difficult. Such loops have the structure  of infinite loops but include user-located loop exits. Perhaps the most interesting question is whether a single loop or several nested loops can be exited.
- Iteration Based on Data Structures
 Loop iterations is controlled by the number of elements in a data structure.


Komentar