Information AboutControl Flow |
| CATEGORIES ABOUT CONTROL FLOW | |
| control flowcontrol flow | |
| programming constructs | |
|
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 Non-strict 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:
Interrupt s and Signals 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 Effects , but usually does not involve an explicit control flow statement (an exception being the ALTER verb in COBOL ). At the level of Machine or Assembly Language , control flow instructions usually work by altering the Program Counter . For many CPUs the only control flow instructions available are conditional or unconditional Branch 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 Slot s. Compiler s 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 Of Execution , 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 Number s are a kind of label used in some languages (e.g. Fortran and BASIC ), which are Whole Number s 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 and Ada 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. "); 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 variants of Algol allowed whole numbers. Goto See Also: GOTO The goto statement (a juxtaposition of the English words '''', and pronounced as two words) is the most basic form of unconditional transfer of control. Although the Keyword 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 See Also: Subroutine The terminology for Subroutine s varies; they may alternatively be known as routines, procedures, functions (especially if they return results) or methods (especially if they belong to Classes or Type Class es). In the 1950's, computer memories were very small by current standards so subroutines were used primarily to reduce program size; a piece of code was written once and then used many times from 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 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 ACM which showed that any program with gotos could be transformed into a goto-free form involving 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 were much easier to understand than any other form, primarily because they could be used anywhere as a statement without disrupting the control flow. In other words, they were ''composable''. (Later developments, such as Non-strict Programming Language s - and more recently, composable Software Transactions - 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.
CHOICE See Also: Conditional (programming) 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 , 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 examples then the body is skipped completely. In most cases counting can go downwards instead of upwards and 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<=N; ++I ) { xxx xxx END DO } See also For Loop , Loop Counter . In many programming languages, only integers can be reliably used in a count-controlled loop. Floating-point numbers are represented imprecisely due to hardware constraints, so a loop such as for X := 0.1 '''step''' 0.1 '''to''' 1.0 '''do''' might be repeated 9 or 10 times, depending on rounding errors and/or the hardware and/or the compiler version. Furthermore, if the increment of X occurs by repeated addition, accumulated rounding errors may mean that the value of X in each iteration can differ quite significantly from the expected sequence 0.1, 0.2, 0.3, ..., 1.0. Condition-controlled loops Again, most programming languages have constructions for repeating a loop until some condition changes. Note that some variations place the test at the start of the loop, while others have the test at the end of the loop. In the former case the body may be skipped completely, while in the latter case the body is always obeyed at least once. DO WHILE (test) repeat xxx xxx END DO until test; while (test) { '''do''' xxx xxx } while (test); See also While Loop . Collection-controlled loops A few programming languages (e.g. Smalltalk , Perl , Java , C# , Visual Basic ) have special constructs which allow you to implicitly loop through all elements of an array, or all members of a set or collection. | ||
|   | { Class | "wikitable" |
|
|