Warning: mkdir() [
function.mkdir]: Permission denied in
/home/webs/affiliatelib2/CacheManager.php on line
12
Warning: mkdir() [
function.mkdir]: No such file or directory in
/home/webs/affiliatelib2/CacheManager.php on line
12
Warning: fopen(/home/templatecore2cache//*cluesnet.com/5e/5e2f68f66ebcdff8fe67eb861cc6db49566c1934.tc2cache) [
function.fopen]: failed to open stream: No such file or directory in
/home/webs/affiliatelib2/CacheManager.php on line
130
Warning: fwrite(): supplied argument is not a valid stream resource in
/home/webs/affiliatelib2/CacheManager.php on line
131
Warning: fclose(): supplied argument is not a valid stream resource in
/home/webs/affiliatelib2/CacheManager.php on line
132
In
computer science control flow (or alternatively, flow of control) refers to the order in which the individual
statement (programming), Instruction (computer science) or
function calls of an
imperative programming or functional programming computer program are
execution (computers) or evaluated.Within an imperative programming language, a
control flow statement is an instruction that when executed can cause a change in the subsequent control flow to differ from the natural sequential order in which the instructions are listed. For
strict programming language functional languages, functions and language constructs exist to achieve the same ends, but they are not necessarily called control flow statements.
The kinds of control flow statements available differ by language, but can be roughly categorized by their effect:
- continuation at a different statement (jump),
- executing a set of statements only if some condition is met (choice),
- executing a set of statements zero or more times, until some condition is met (loop),
- executing a set of distant statements, after which the flow of control may possibly return (subroutines, coroutines, and continuations),
- stopping the program, preventing any further execution (halt).
Interrupts and
signal (computing) are low-level mechanisms that can alter the flow of control in a way similar to a subroutine, but are usually in response to some external stimulus or event rather than a control flow statement in the language.
Self-modifying code can also be used to affect control flow through its
Side effect (computer science), but usually does not involve an explicit control flow statement (an exception being the ALTER verb in
COBOL).
At the level of Machine language or
assembly language, control flow instructions usually work by altering the
program counter. For many
Central processing unit the only control flow instructions available are conditional or unconditional Branch (computer science)es (sometimes called jumps). Some processor designs also complicate the control flow by wavering from a strict sequential ordering of instructions, with features such as speculative execution, out-of-order execution, and
branch delay slots.
Compilers for higher-level programming languages must therefore translate all the many control-flow statements of the language into equivalent code using only the more limited instructions, and in a manner which preserves an observable behavior of a natural sequential flow - assuming that only one thread is executing. Discussion of control flow is almost always restricted to a single Thread (computer science), as it depends upon a definite sequence in which instructions are executed one at a time.
Primitives
Labels
A label is an explicit name or number assigned to a fixed position within the
source code, and which may be referenced by control flow statements appearing elsewhere in the source code. Other than marking a position within the source code a label has no effect.
Line numbers are a kind of label used in some languages (e.g.
Fortran and
BASIC programming language), which are whole numbers placed at the beginning of each line of text within the source code. Languages which use line numbers often impose the constraint that the line numbers must increase in value in each subsequent line, but may not require that they be consecutive. For example, in BASIC:
'''10''' X = 3
'''20''' PRINT X
In other languages such as
C (programming language) and Ada programming language a label is an identifier, usually appearing at the beginning of a line and immediately followed by a colon. For example, in C:
'''Success:''' printf ("The operation was successful.\n");
The
Algol 60 language allowed both whole numbers and identifiers as labels(both attached by colons to the following statement), but few if any other variantsof Algol allowed whole numbers.
Goto
The
goto statement (a juxtaposition of the English words
wiktionary:go and
wiktionary:to, and pronounced as two words) is the most basic form of unconditional transfer of control.
Although the
keyword (computer) may either be in upper or lower case depending on the language, it is usually written as: '''goto''' ''label''
The effect of a goto statement is to cause the next statement to be executed to always be the statement appearing immediately after (or at) the indicated label.
Goto statements have been considered harmful by many computer scientists, notably
Dijkstra.
Subroutines
The terminology for
subroutines varies; they may alternatively be known as routines, procedures, functions (especially if they return results) or methods (especially if they belong to class (programming) or
type classes).
In the 1950's, computer memories were very small by current standardsso subroutines were used primarily to reduce program size;a piece of code was written once and then used many timesfrom various other places in the program.
Nowadays, subroutines are more frequently used to help make a program more structured,e.g. by isolating some particular algorithm or hiding some particular data access method.If many programmers are working on a single program,subroutines are one kind of modularity (programming) that can help split up the work.
Minimal structured control flow
(See also Structured program theorem.)In May 1966, Böhm and Jacopini published an article in Communications of the ACMwhich showed that any program with
gotos could be transformed into a goto-free forminvolving only choice (IF THEN ELSE) and loops (WHILE condition DO xxx),possibly with duplicated code and/or the addition of Boolean variables (true/false flags).Later authors have shown that choice can be replaced by loops (and yet more Boolean variables).
The fact that such minimalism is possible does not necessarily mean that it is desirable;after all, computers theoretically only need one machine instruction(subtract one number from another and branch if the result is negative),but practical computers have dozens or even hundreds of machine instructions.
What Böhm and Jacopini's article showed was that all programs could be goto-free.Other research showed that control structures with one entry and one exit weremuch easier to understand than any other form,primarily because they could be used anywhere as a statementwithout disrupting the control flow. In other words, they were
composable. (Later developments, such as
non-strict programming languages - and more recently, composable software transactional memory - have continued this line of thought, making components of programs even more freely composable.)
Control structures in practice
Most programming languages with control structures have an initial keyword which indicates the type of control structure involved. Languages then divide as to whether or not control structures have a final keyword.
- No final keyword: Algol programming language, C (programming language), C++, Haskell (programming language), Java (programming language), Pascal programming language, Perl, PHP, PL/I programming language, Python (programming language). Such languages need some way of grouping statements together:
- Algol 60 and Pascal : begin ... end
- C, C++, Java, Perl, and PHP: curly brackets { ... }
- PL/1: DO ... END
- Python: uses indentation level (see Off-side rule)
- Haskell: either indentation level or curly brackets can be used, and they can be freely mixed
- Final keyword: Ada programming language, Algol 68, Modula-2, Fortran, Visual Basic. The forms of the final keyword vary:
- Ada: final keyword is end + space + initial keyword e.g. if ... end if, loop ... end loop
- Algol 68: initial keyword spelled backwards e.g. if ... fi, case ... esac
- Fortran 77: final keyword is end + initial keyword e.g. IF ... ENDIF, DO ... ENDDO
- Modula-2: same final keyword end for everything
- Visual Basic: every control structure has its own keyword. If ... End If; For ... Next; Do ... Loop
Choice
Loops
A loop is a sequence of statements which is specified once but which may be carried out several times in succession.The code "inside" the loop (the
body of the loop, shown below as
xxx) is obeyed a specified number of times,or once for each of a collection of items, or until some condition is met.
In some languages, such as Scheme (programming language), loops are often expressed using
tail recursion rather than explicit looping constructs.
Count-controlled loops
Most programming languages have constructions for repeating a loop a certain number of times.Note that if N is less than 1 in these examplesthen the language may specify that the body is skipped completely, or that the body is executed just once with N = 1.In most cases counting can go downwards instead of upwardsand step sizes other than 1 can be used.
FOR I = 1 TO N '''for''' I := 1 '''to''' N '''do''' '''begin'''
xxx xxx
NEXT I '''end''';
DO I = 1,N '''for''' ( I=1; I