This chapter shows you how to structure the flow of control through a PL/SQL program. You learn how statements are connected by simple but powerful control structures that have a single entry and exit point. Collectively, these structures can handle any situation. And, their proper use leads naturally to a well-structured program. Show
Major TopicsOverviewAccording to the structure theorem, any computer program can be written using the basic control structures shown in . They can be combined in any way necessary to deal with a given problem. Figure 3-1 Control StructuresThe selection structure tests a condition, then executes one sequence of statements instead of another, depending on whether the condition is true or false. A condition is any variable or expression that returns a Boolean value ( IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;5 or IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;6). The iteration structure executes a sequence of statements repeatedly as long as a condition holds true. The sequence structure simply executes a sequence of statements in the order in which they occur. Conditional Control: IF StatementsOften, it is necessary to take alternative actions depending on circumstances. The IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statement lets you execute a sequence of statements conditionally. That is, whether the sequence is executed or not depends on the value of a condition. There are three forms of IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statements: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;9, IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;0, and IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;1. IF-THENThe simplest form of IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statement associates a condition with a sequence of statements enclosed by the keywords IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;3 and IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;4 IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 (not IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;6), as follows: IF condition THEN sequence_of_statements; END IF; The sequence of statements is executed only if the condition yields IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;5. If the condition yields IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;6 or IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9, the IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statement does nothing. In either case, control passes to the next statement. An example follows: IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF; You might want to place brief IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statements on a single line, as in IF x > y THEN high := x; END IF; IF-THEN-ELSEThe second form of IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statement adds the keyword BEGIN ... IF sales > 50000 THEN bonus := 1500; ELSIF sales > 35000 THEN bonus := 500; ELSE bonus := 100; END IF; INSERT INTO payroll VALUES (emp_id, bonus, ...); END;3 followed by an alternative sequence of statements, as follows: IF condition THEN sequence_of_statements1; ELSE sequence_of_statements2; END IF; The sequence of statements in the BEGIN ... IF sales > 50000 THEN bonus := 1500; ELSIF sales > 35000 THEN bonus := 500; ELSE bonus := 100; END IF; INSERT INTO payroll VALUES (emp_id, bonus, ...); END;3 clause is executed only if the condition yields IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;6 or IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9. Thus, the BEGIN ... IF sales > 50000 THEN bonus := 1500; ELSIF sales > 35000 THEN bonus := 500; ELSE bonus := 100; END IF; INSERT INTO payroll VALUES (emp_id, bonus, ...); END;3 clause ensures that a sequence of statements is executed. In the following example, the first or second BEGIN ... IF sales > 50000 THEN bonus := 1500; ELSIF sales > 35000 THEN bonus := 500; ELSE bonus := 100; END IF; INSERT INTO payroll VALUES (emp_id, bonus, ...); END;8 statement is executed when the condition is true or false, respectively: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE UPDATE accounts SET balance = balance - debit WHERE ... END IF; The IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;3 and BEGIN ... IF sales > 50000 THEN bonus := 1500; ELSIF sales > 35000 THEN bonus := 500; ELSE bonus := 100; END IF; INSERT INTO payroll VALUES (emp_id, bonus, ...); END;3 clauses can include IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statements. That is, IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statements can be nested, as the following example shows: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF; IF-THEN-ELSIFSometimes you want to select an action from several mutually exclusive alternatives. The third form of IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statement uses the keyword DECLARE ... overdrawn BOOLEAN; BEGIN ... IF new_balance < minimum_balance THEN overdrawn := TRUE; ELSE overdrawn := FALSE; END IF; ... IF overdrawn = TRUE THEN RAISE insufficient_funds; END IF; END;4 (not DECLARE ... overdrawn BOOLEAN; BEGIN ... IF new_balance < minimum_balance THEN overdrawn := TRUE; ELSE overdrawn := FALSE; END IF; ... IF overdrawn = TRUE THEN RAISE insufficient_funds; END IF; END;5) to introduce additional conditions, as follows: IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF; If the first condition yields IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;6 or IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9, the DECLARE ... overdrawn BOOLEAN; BEGIN ... IF new_balance < minimum_balance THEN overdrawn := TRUE; ELSE overdrawn := FALSE; END IF; ... IF overdrawn = TRUE THEN RAISE insufficient_funds; END IF; END;4 clause tests another condition. An IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statement can have any number of DECLARE ... overdrawn BOOLEAN; BEGIN ... IF new_balance < minimum_balance THEN overdrawn := TRUE; ELSE overdrawn := FALSE; END IF; ... IF overdrawn = TRUE THEN RAISE insufficient_funds; END IF; END;4 clauses; the final BEGIN ... IF sales > 50000 THEN bonus := 1500; ELSIF sales > 35000 THEN bonus := 500; ELSE bonus := 100; END IF; INSERT INTO payroll VALUES (emp_id, bonus, ...); END;3 clause is optional. Conditions are evaluated one by one from top to bottom. If any condition yields IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;5, its associated sequence of statements is executed and control passes to the next statement. If all conditions yield IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;6 or IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9, the sequence in the BEGIN ... IF sales > 50000 THEN bonus := 1500; ELSIF sales > 35000 THEN bonus := 500; ELSE bonus := 100; END IF; INSERT INTO payroll VALUES (emp_id, bonus, ...); END;3 clause is executed. Consider the following example: BEGIN ... IF sales > 50000 THEN bonus := 1500; ELSIF sales > 35000 THEN bonus := 500; ELSE bonus := 100; END IF; INSERT INTO payroll VALUES (emp_id, bonus, ...); END; If the value of overdrawn := new_balance < minimum_balance;6 is more than 50000, the first and second conditions are true. Nevertheless, overdrawn := new_balance < minimum_balance;7 is assigned the proper value of 1500 because the second condition is never tested. When the first condition yields IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;5, its associated statement is executed and control passes to the overdrawn := new_balance < minimum_balance;9 statement. GuidelinesAvoid clumsy IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statements like those in the following example: DECLARE ... overdrawn BOOLEAN; BEGIN ... IF new_balance < minimum_balance THEN overdrawn := TRUE; ELSE overdrawn := FALSE; END IF; ... IF overdrawn = TRUE THEN RAISE insufficient_funds; END IF; END; This code disregards two useful facts. First, the value of a Boolean expression can be assigned directly to a Boolean variable. So, you can replace the first IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statement with a simple assignment, as follows: overdrawn := new_balance < minimum_balance; Second, a Boolean variable is itself either true or false. So, you can simplify the condition in the second IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statement, as follows: IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;0 When possible, use the DECLARE ... overdrawn BOOLEAN; BEGIN ... IF new_balance < minimum_balance THEN overdrawn := TRUE; ELSE overdrawn := FALSE; END IF; ... IF overdrawn = TRUE THEN RAISE insufficient_funds; END IF; END;4 clause instead of nested IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statements. That way, your code will be easier to read and understand. Compare the following IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statements: IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;1 These statements are logically equivalent, but the first statement obscures the flow of logic, whereas the second statement reveals it. Iterative Control: LOOP and EXIT StatementsIF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06 statements let you execute a sequence of statements multiple times. There are three forms of IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06 statements: IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06, IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;09, and IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;10. LOOPThe simplest form of IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06 statement is the basic (or infinite) loop, which encloses a sequence of statements between the keywords IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06 and IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;4 IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06, as follows: IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;2 With each iteration of the loop, the sequence of statements is executed, then control resumes at the top of the loop. If further processing is undesirable or impossible, you can use an IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;15 statement to complete the loop. You can place one or more IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;15 statements anywhere inside a loop, but nowhere outside a loop. There are two forms of IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;15 statements: IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;15 and IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;19. EXITThe IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;15 statement forces a loop to complete unconditionally. When an IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;15 statement is encountered, the loop completes immediately and control passes to the next statement. An example follows: IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;3 The next example shows that you cannot use the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;15 statement to complete a PL/SQL block: IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;4 Remember, the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;15 statement must be placed inside a loop. To complete a PL/SQL block before its normal end is reached, you can use the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;24 statement. For more information, see . EXIT-WHENThe IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;19 statement allows a loop to complete conditionally. When the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;15 statement is encountered, the condition in the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;27 clause is evaluated. If the condition yields IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;5, the loop completes and control passes to the next statement after the loop. An example follows: IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;5 Until the condition yields IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;5, the loop cannot complete. So, statements within the loop must change the value of the condition. In the last example, if the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;30 statement returns a row, the condition yields IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;6. When the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;30 statement fails to return a row, the condition yields IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;5, the loop completes, and control passes to the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;34 statement. The IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;19 statement replaces a simple IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statement. For example, compare the following statements: IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;6 These statements are logically equivalent, but the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;19 statement is easier to read and understand. Loop LabelsLike PL/SQL blocks, loops can be labeled. The label, an undeclared identifier enclosed by double angle brackets, must appear at the beginning of the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06 statement, as follows: IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;7 Optionally, the label name can also appear at the end of the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06 statement, as the following example shows: IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;8 When you nest labeled loops, you can use ending label names to improve readability. With either form of IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;15 statement, you can complete not only the current loop, but any enclosing loop. Simply label the enclosing loop that you want to complete. Then, use the label in an IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;15 statement, as follows: IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;9 Every enclosing loop up to and including the labeled loop is exited. WHILE-LOOPThe IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;09 statement associates a condition with a sequence of statements enclosed by the keywords IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06 and IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;4 IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06, as follows: IF x > y THEN high := x; END IF;0 Before each iteration of the loop, the condition is evaluated. If the condition yields IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;5, the sequence of statements is executed, then control resumes at the top of the loop. If the condition yields IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;6 or IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9, the loop is bypassed and control passes to the next statement. An example follows: IF x > y THEN high := x; END IF;1 The number of iterations depends on the condition and is unknown until the loop completes. Since the condition is tested at the top of the loop, the sequence might execute zero times. In the last example, if the initial value of IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;49 is greater than 25000, the condition yields IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;6 and the loop is bypassed. Some languages have a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06 IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;52 or IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;53 IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;52 structure, which tests the condition at the bottom of the loop instead of at the top. Therefore, the sequence of statements is executed at least once. PL/SQL has no such structure, but you can easily build one, as follows: IF x > y THEN high := x; END IF;2 To ensure that a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;55 loop executes at least once, use an initialized Boolean variable in the condition, as follows: IF x > y THEN high := x; END IF;3 A statement inside the loop must assign a new value to the Boolean variable. Otherwise, you have an infinite loop. For example, the following IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06 statements are logically equivalent: IF x > y THEN high := x; END IF;4 FOR-LOOPWhereas the number of iterations through a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;55 loop is unknown until the loop completes, the number of iterations through a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;58 loop is known before the loop is entered. IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;58 loops iterate over a specified range of integers. (Cursor IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;58 loops, which iterate over the result set of a cursor, are discussed in .) The range is part of an iteration scheme, which is enclosed by the keywords IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;58 and IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06. A double dot ( IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;63) serves as the range operator. The syntax follows: IF x > y THEN high := x; END IF;5 The range is evaluated when the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;58 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. IF x > y THEN high := x; END IF;6 The following example shows that if the lower bound equals the higher bound, the sequence of statements is executed once: IF x > y THEN high := x; END IF;7 By default, iteration proceeds upward from the lower bound to the higher bound. However, if you use the keyword IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;65, iteration proceeds downward from the higher bound to the lower bound, as the example below shows. After each iteration, the loop counter is decremented. IF x > y THEN high := x; END IF;8 Nevertheless, you write the range bounds in ascending (not descending) order. Inside a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;58 loop, the loop counter can be referenced like a constant. So, the loop counter can appear in expressions but cannot be assigned values, as the following example shows: IF x > y THEN high := x; END IF;9 Iteration SchemesThe bounds of a loop range can be literals, variables, or expressions but must evaluate to integers. For example, the following iteration schemes are legal: IF condition THEN sequence_of_statements1; ELSE sequence_of_statements2; END IF;0 As you can see, the lower bound need not be 1. However, the loop counter increment (or decrement) must be 1. Some languages provide a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;67 clause, which lets you specify a different increment. An example written in BASIC follows: IF condition THEN sequence_of_statements1; ELSE sequence_of_statements2; END IF;1 PL/SQL has no such structure, but you can easily build one. Consider the following example: IF condition THEN sequence_of_statements1; ELSE sequence_of_statements2; END IF;2 This loop is logically equivalent to the previous BASIC loop. Within the sequence of statements, the loop counter has only the values 5, 10, and 15. You might prefer the less elegant but more efficient method shown in the example below. Within the sequence of statements, each reference to the loop counter is multiplied by the increment. IF condition THEN sequence_of_statements1; ELSE sequence_of_statements2; END IF;3 Dynamic RangesPL/SQL lets you determine the loop range dynamically at run time, as the following example shows: IF condition THEN sequence_of_statements1; ELSE sequence_of_statements2; END IF;4 The value of IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;68 is unknown at compile time; the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;69 statement returns the value at run time. 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: IF condition THEN sequence_of_statements1; ELSE sequence_of_statements2; END IF;5 Scope RulesThe 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: IF condition THEN sequence_of_statements1; ELSE sequence_of_statements2; END IF;6 You need not explicitly declare the loop counter because it is implicitly declared as a local variable of type IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;70. The next example shows that the local declaration hides any global declaration: IF condition THEN sequence_of_statements1; ELSE sequence_of_statements2; END IF;7 To reference the global variable in this example, you must use a label and dot notation, as follows: IF condition THEN sequence_of_statements1; ELSE sequence_of_statements2; END IF;8 The same scope rules apply to nested IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;58 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: IF condition THEN sequence_of_statements1; ELSE sequence_of_statements2; END IF;9 Using the EXIT StatementThe IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;15 statement allows a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;58 loop to complete prematurely. For example, the following loop normally executes ten times, but as soon as the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;30 statement fails to return a row, the loop completes no matter how many times it has executed: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE UPDATE accounts SET balance = balance - debit WHERE ... END IF;0 Suppose you must exit from a nested IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;58 loop prematurely. You can complete not only the current loop, but any enclosing loop. Simply label the enclosing loop that you want to complete. Then, use the label in an IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;15 statement to specify which IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;58 loop to exit, as follows: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE UPDATE accounts SET balance = balance - debit WHERE ... END IF;1 Sequential Control: GOTO and NULL StatementsUnlike the IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 and IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06 statements, the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 and IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9 statements are not crucial to PL/SQL programming. The structure of PL/SQL is such that the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statement is seldom needed. Occasionally, it can simplify logic enough to warrant its use. The IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9 statement can make the meaning and action of conditional statements clear and so improve readability. Overuse of IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statements can result in complex, unstructured code (sometimes called spaghetti code) that is hard to understand and maintain. So, use IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statements sparingly. For example, to branch from a deeply nested structure to an error-handling routine, raise an exception rather than use a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statement. GOTO StatementThe IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statement branches to a label unconditionally. The label must be unique within its scope and must precede an executable statement or a PL/SQL block. When executed, the IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 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: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE UPDATE accounts SET balance = balance - debit WHERE ... END IF;2 In the next example, you go to a PL/SQL block farther up in a sequence of statements: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE UPDATE accounts SET balance = balance - debit WHERE ... END IF;3 The label IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;89in the following example is illegal because it does not precede an executable statement: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE UPDATE accounts SET balance = balance - debit WHERE ... END IF;4 To debug the last example, simply add the IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9 statement, as follows: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE UPDATE accounts SET balance = balance - debit WHERE ... END IF;5 As the following example shows, a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statement can branch to an enclosing block from the current block: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE UPDATE accounts SET balance = balance - debit WHERE ... END IF;6 The IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statement branches to the first enclosing block in which the referenced label appears. RestrictionsSome possible destinations of a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statement are illegal. Specifically, a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statement cannot branch into an IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statement, IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;06 statement, or sub-block. For example, the following IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statement is illegal: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE UPDATE accounts SET balance = balance - debit WHERE ... END IF;7 Also, a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statement cannot branch from one IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statement clause to another, as the following example shows: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE UPDATE accounts SET balance = balance - debit WHERE ... END IF;8 The next example shows that a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statement cannot branch from an enclosing block into a sub-block: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE UPDATE accounts SET balance = balance - debit WHERE ... END IF;9 Also, a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statement cannot branch out of a subprogram, as the following example shows: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;0 Finally, a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statement cannot branch from an exception handler into the current block. For example, the following IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statement is illegal: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;1 However, a IF sales > quota THEN compute_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id; END IF;80 statement can branch from an exception handler into an enclosing block. NULL StatementThe IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9 statement explicitly specifies inaction; it does nothing other than pass control to the next statement. It can, however, improve readability. In a construct allowing alternative actions, the IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9 statement serves as a placeholder. It tells readers that the associated alternative has not been overlooked, but that indeed no action is necessary. In the following example, the IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9 statement shows that no action is taken for unnamed exceptions: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;2 Each clause in an IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;7 statement must contain at least one executable statement. The IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9 statement meets this requirement. So, you can use the IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9 statement in clauses that correspond to circumstances in which no action is taken. In the following example, the IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9 statement emphasizes that only top-rated employees receive bonuses: IF trans_type = 'CR' THEN UPDATE accounts SET balance = balance + credit WHERE ... ELSE IF new_balance >= minimum_balance THEN UPDATE accounts SET balance = balance - debit WHERE ... ELSE RAISE insufficient_funds; END IF; END IF;3 Also, the IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9 statement is a handy way to create stubs when designing applications from the top down. A stub is dummy subprogram that allows you to defer the definition of a procedure or function until you test and debug the main program. In the following example, the IF condition1 THEN sequence_of_statements1; ELSIF condition2 THEN sequence_of_statements2; ELSE sequence_of_statements3; END IF;9 statement meets the requirement that at least one statement must appear in the executable part of a subprogram: What is a structure that allows repeated execution of a block of statements?A loop is a structure that allows repeated execution of a block of statements. Within a looping structure, a Boolean expression is evaluated.
Which structure causes a statement or set of statements to execute repeatedly quizlet?More commonly known as a loop, a repetition structure causes a statement or set of statements to execute repeatedly as many times as necessary. What is a condition-controlled loop? A condition-controlled loop causes a statement or set of statements to repeat as long as a condition is true.
What is the term for one execution of a loop?A single execution of the loop body is called an iteration. The loop in the example above makes three iterations.
What is an indefinite loop?An indefinite loop is a loop that waits for some condition to become true. In general, it's not obvious how many iterations it takes. For example, you might be looking for the first number that is divisible by 2, 3, 5, 7, and 11.
|