Many programmers refer to this construct as a loop. Two forms of the repetition control structure are the do-while and do-until. A Control Statement is a statement that control the execution of other statements.
There are different types of control statements:. Loop is a statement that tells program how many times a statement will be executed.
Three types of loops are:. Switch statement is a statement that decides which statement is going to execute from several statements. Difference Between Macros and Functions. Difference Between Search Engine and Directory. You must be logged in to post a comment. Control Structures vs Control Statements Summary: Difference Between Control Structures and Control Statements is that when programmers are required to design the logic of a program, they typically use control structures to describe the tasks a program is to perform.
The longer and more complex the program, the more these constructs will be used repeatedly. This guide uses pseudo-code examples to illustrate the constructs. It is important to realise that while all programming languages include these constructs, there may be slightly different formats. The three basic programming constructs Programs are designed using common building blocks. For example, the following LOOP statements are logically equivalent:. Whereas the number of iterations through a WHILE loop is unknown until the loop completes, the number of iterations through a FOR loop is known before the loop is entered.
FOR loops iterate over a specified range of integers. Cursor FOR loops, which iterate over the result set of a cursor, are discussed in Chapter 5. A double dot.. The syntax follows:. The range is evaluated when the FOR loop is first entered and is never re-evaluated. As the next example shows, the sequence of statements is executed once for each integer in the range. After each iteration, the loop counter is incremented. The following example shows that if the lower bound equals the higher bound, the sequence of statements is executed once:.
By default, iteration proceeds upward from the lower bound to the higher bound. However, as the example below shows, if you use the keyword REVERSE , iteration proceeds downward from the higher bound to the lower bound. After each iteration, the loop counter is decremented. Nevertheless, you write the range bounds in ascending not descending order.
Inside a FOR loop, the loop counter can be referenced like a constant but cannot be assigned values, as the following example shows:.
The bounds of a loop range can be literals, variables, or expressions but must evaluate to numbers. The lower bound need not be 1, as the examples below show. However, the loop counter increment or decrement must be 1. Some languages provide a STEP clause, which lets you specify a different increment 5 instead of 1 for example. Inside the FOR loop, simply multiply each reference to the loop counter by the new increment. In the following example, you assign today's date to elements 5, 10, and 15 of an index-by table:.
What happens if the lower bound of a loop range evaluates to a larger integer than the upper bound? As the next example shows, the sequence of statements within the loop is not executed and control passes to the next statement:.
The loop counter is defined only within the loop. You cannot reference it outside the loop. After the loop is exited, the loop counter is undefined, as the following example shows:. The next example shows that the local declaration hides any global declaration:. To reference the global variable in this example, you must use a label and dot notation, as follows:.
The same scope rules apply to nested FOR loops. Consider the example below. Both loop counters have the same name. So, to reference the outer loop counter from the inner loop, you must use a label and dot notation, as follows:. For example, the following loop normally executes ten times, but as soon as the FETCH statement fails to return a row, the loop completes no matter how many times it has executed:. Suppose you must exit from a nested FOR loop prematurely. You can complete not only the current loop, but any enclosing loop.
Occasionally, it can simplify logic enough to warrant its use. The NULL statement can improve readability by making the meaning and action of conditional statements clear. Overuse of GOTO statements can result in complex, unstructured code sometimes called spaghetti code that is hard to understand and maintain. So, use GOTO statements sparingly.
For example, to branch from a deeply nested structure to an error-handling routine, raise an exception rather than use a GOTO statement. The GOTO statement branches to a label unconditionally. When executed, the GOTO statement transfers control to the labeled statement or block. In the following example, you go to an executable statement farther down in a sequence of statements:.
As the following example shows, a GOTO statement can branch to an enclosing block from the current block:. The GOTO statement branches to the first enclosing block in which the referenced label appears.
0コメント