09 Dec What Is Not True about Sequential Control Statements
These statements are logically equivalent, but the first statement obscures the flow of logic, while the second reveals it. The GOTO statement branches unconditionally to a label. The label must be unique in its scope and preceded by an executable statement or PL/SQL block. When executed, the GOTO statement transfers control to the labeled statement or block. In the following example, you switch to an executable statement later in a sequence of statements: Although overlapping conditions are allowed in an IF-THEN-ELSIF statement, you should avoid them as much as possible. In our case, the original specification is somewhat ambiguous about how to treat borderline cases like 20,000. Assuming that the intention is to give the highest bonuses to the lowest paid employees (which seems to us a reasonable approach), we would dispense with the BETWEEN operator and use the following logic of less than/greater than. Note that we also omitted the ELSE clause just to illustrate that it`s optional: overusing GOTO statements can result in complex, unstructured code (sometimes called spaghetti code) that is difficult to understand and maintain. So use GOTO instructions sparingly.
For example, to move from a deeply nested structure to an error-handling routine, remove an exception instead of using a GOTO statement. These operators are often used as test expressions in selection statements or repeat statements (loops). The statement sequence in the ELSE clause is executed only if the condition generates FALSE or NULL. Thus, the ELSE clause guarantees the execution of a sequence of statements. The following example executes the first or second UPDATE statement if the condition is true or false: This readability problem becomes even more important when the ELSE and ELSIF keywords are used and when one IF statement is nested within the other. Take advantage of indents and formatting to easily decode the logic of your IF statements. Future maintenance programmers will thank you. If a continuous statement is found, it ignores the rest of the statements to be executed in the current loop and the control returns to the next iteration. This means that statements found after the Assert statement will not be executed for the current iteration.
The selection structure tests one condition and then executes one sequence of instructions instead of another, depending on whether the condition is true or false. A condition is a variable or expression that returns a Boolean value (TRUE or FALSE). The iteration structure executes a sequence of statements repeatedly as long as a condition is true. The sequence structure simply executes a sequence of instructions in the order in which they appear. Putting everything on one line is perfectly acceptable for simple IF statements like the one shown here. However, if you write IF statements of any complexity, you will find that readability is much better if you format the statement so that each keyword starts a new line. For example, the following code would be very difficult to follow if everything was crammed into one line. In fact, it is difficult to follow because it appears on three lines: This loop is logically equivalent to the previous BASIC loop.
In the instruction sequence, the loop counter has only the values 5, 10, and 15. Another situation in which you want to use nested IF statements is when evaluating a condition in terms of CPU or memory usage is very expensive. In this case, you may want to move this process to an internal IF statement so that it is executed only when absolutely necessary. This is especially true for code that runs frequently or in areas of the application where fast response time is critical. The following IF statement illustrates this concept: The latter and more complex form of the IF statement selects an action from a set of mutually exclusive conditions, and then executes the set of statements associated with that condition. If you write IF statements like this in Oracle9i, you should use CASE statements that are searched instead. BREAK statements are used in loops to stop execution if they are found. Nested IF statements are often necessary to implement complex logical rules, but you should use them with caution. Nested IF statements, such as nested loops, can be very difficult to understand and debug.
If you find that you need to nest more than three levels in your conditional logic, you should look at that logic and see if there is an easier way to encode the same requirement. If not, you must create one or more local modules to hide the innermost IF statements. If the value of the turnover is greater than 50000, the first and second conditions are met. Nevertheless, the bonus is given the correct value of 1500, since the second condition is never tested. If the first condition returns TRUE, the associated statement is executed and the control is passed to the INSERT statement. We also have Boolean operators to combine expressions. These operators also return true or false // This example includes an else clause. Bodies are individual instructions. These instructions are logically equivalent, but the EXIT-WHEN statement is easier to read and understand. Each iteration of the loop, the sequence of instructions is executed, and then the check continues at the beginning of the loop. If further processing is not desirable or impossible, you can use an EXIT statement to close the loop.
You can place one or more EXIT statements anywhere in a loop, but nowhere outside a loop. There are two forms of EXIT statements: EXIT and EXIT-WHEN. Note the following points about the syntax of the IF statement: The difference between these two statements is what happens when the variable x is zero. If x is NULL, the first IF-THEN-ELSE statement displays “x does not contain 2”. The second statement shows “X contains 2”. What for? This is because if x is NULL, the IF expression is not considered TRUE and the control is passed to the ELSE clause.