Cotrol- Level Structure

A control structure is a primary concept in most high-level programming languages. In its simplest sense, it is a block of code. More specifically, control structures are blocks of code that dictate the flow of control. In other words, a control structure is a container for a series of function calls, instructions and statements.

A simple example of a control structure is, if a then b else c. These statements will be included or excluded based o­n the condition of a. This simple notion of if then comprises the bulk of even the most sophisticated software applications.

There are many other types of control structures than the if then structure. In broad terms, these include the jump or unconditional branch, the conditional branch, the loop, which is essentially a conditional branch, subroutines, co-routines, continuations, and the conditional halt. There are also lower level flow controls, such as interrupts and signals.

The most common specific forms of those control structures are gotos, subroutines, for loops, do while loops, if then, try catch finally, so o­n and so forth. The names deviate between languages but for the most part these concepts are universal across all imperative programming languages. The concept of the conditional branch, or if then else, exists in C++ as it does in Visual Basic.NET as it does in Ruby.

Programmers reading or maintaining code need to be able to identify these control structures easily. Formatting them in a structured way allows them to be identifiable and distinct from each other. The coding standard structures them by combining a series of style rules. In most standards, these rules center o­n indentation and line breaks.

Let us consider the C-style condition if (a == 5) { b = 1 } else { b = 0 };

This is a perfectly acceptable C-style condition. However, the widely accepted coding standard would have us write it as such:

if ( a == 5 ) {
b = 1 }
else {
b = 0
};

 SOURCE: http://www.valid-computing.com/control-structures



One part of language design became the search for higher-level control structures, to replace as many uses of jumps as possible. Many different structures were proposed, often specific to a particular programming style or problem. Many users were suspicious of this trend, believing that power and efficiency were being lost.
Böhm and Jacopini proved that any program containing jumps can be converted to an equivalent program using only loops and if- then- elses, provided some extra boolean variables are introduced. However, their proof did not show how to make best use of these control constructs.
Dijkstra proposed that only three control constructs should be used: concatenation, selection and repetition:

\begin{picture}(50,16)
\newsavebox {\decisionbox}\savebox {\decisionbox}(0,0){%
...
...ction}}
\put(35,16){\usebox {\repeatbox}\makebox(0,0){repetition}}
\end{picture}
Because each construct has exactly one entry and one exit, they may (recursively) appear as one of the boxes. Experience has shown that structured programming, using these constructs, does give rise to efficient and understandable programs.

Concatenation

Simple sequential code is most easily expressed by concatenation i.e. listing the pieces of code in the correct order. The main related question is how to indicate the boundary between statements. Some languages just use the end of line but then usually require an explicit continuation symbol for statements too long to fit on a single line. Others languages use an explicit character such as ;, with some of these treating it as a separator and the rest treating it as the statement terminator. To turn a code sequence into a single entity, usually known as a compound statement, some languages use an explicit pair of brackets, such as { } or begin end, as a general form useable with any structure. Some languages have specific forms for each structure, such as if fi (or if end_if) and while od (or while end_while). Some languages have both general and specific forms, and others have neither.
Because structures often end up deeply nested, specific keywords can be much easier to pair up than general pairs. 

Selection

The classic selector is the conditional or if statement/expression, and almost all languages have it, usually in two slightly different forms such as if then and if then else.
Some languages place restrictions on the then and else parts, but most modern languages have no such restrictions, allowing nested if statements. One common pattern of use is:
        if ... then ...
        else if ... then ...
        else if ... then ...
        . . .
        else ...
and this has given rise to a different kind of selection construct, the case or switch. This first arose as a modified goto:
        goto label_array[n]
but this was even more dangerous than normal gotos, and was also replaced. A switch construct is essentially a list of (value, action) pairs. Different languages have different forms of this construct:
  • restrictions on types of choice values?
  • can an action have multiple choice values?
  • can an action have a range of choice values?
  • restrictions on expressions for choice values
  • repeated choice values?
  • missing choice value?
    most languages allow a default action, but if this is also missing?
We can generalise the switch into a block of guarded statements, where at most one of the statements is obeyed e.g.:
        if
          i < j then j -= i
          j < i then i -= j
        end
We can even allow for more than one of the guard expressions being valid, either by picking the first such in the list, or by non-deterministically picking a valid guard at random.

Repetition

Repetition comes in two forms: recursion and loops. Some languages have both, some only have one or the other. Loops come in two forms: indeterminate, where the number of repetitions is unknown until the loop terminates, and determinate, where the number of repetitions is known when the loop starts. Again, some languages with iteration have both forms, some only one or the other.
Dijkstra's (indeterminate) repetition construct, which involves a test in the middle of the loop, is not often directly implemented in programming languages. However, special cases of it are common: a version with the first box missing so the test is at the start is found in most languages, and a version with the last box missing so the test is at the end is often found as well.
Almost all languages provide some sort of determinate loop, usually known as a for-loop or do-loop. The basic form is a loop that e.g. counts from 1 to 10, and some languages provide little more than this: e.g.: for i := 1 to 10 do...
and because of this some languages do not bother to test the condition until the end of each repetition. Various enhancements exist: variables or expressions for start and stop values, increments other than 1, decrements, automatic declaration of the counter variable, scalar types other than integer. Some languages guarantee a particular value of the counter outside the loop or permit it to be modified inside the loop, whereas others do not, so as to allow for optimizations (and because modifying the counter does not lead to good quality code).
We can transform the while statement into a loop containing a list of guarded statements, that repeats until none of the guards is valid e.g.:
        while
          i < j do j -= i
          j < i do i -= j
        end
This example terminates when i equals j.




No comments:

Post a Comment