Nested Loops
Nested Loops
When the following code is executed, how many lines of output will be di splayed? BEGIN FOR i IN 1..5 LOOP FOR j IN 1..8 LOOP DBMS_OUTPUT.PUT_LINE(i || ',' || j); END LOOP; DBMS_OUTPUT.PUT_LINE(i); END LOOP; END; Mark for Review (1) Points 80 45 (*) 14 41
Correct 2. Look at the following code: DECLARE v_blue NUMBER(3) := 0; v_red NUMBER(3) := 0; BEGIN << blue >> LOOP v_blue := v_blue + 1; EXIT WHEN v_blue > 10; << red >> LOOP v_red := v_red + 1; EXIT WHEN v_red > 10; -- Line A END LOOP red; END LOOP blue; END; What should you code at Line A to exit from the outer loop? Mark for Review (1) Points EXIT; EXIT red;
Correct 3. What statement allows you to exit the outer loop at Point A in the follo wing block? DECLARE v_outer_done CHAR(3) := 'NO'; v_inner_done CHAR(3) := 'NO'; BEGIN LOOP -- outer loop ... LOOP -- inner loop ... ... -- Point A EXIT WHEN v_inner_done = 'YES'; ... END LOOP; ... EXIT WHEN v_outer_done = 'YES'; ... END LOOP; END; Mark for Review (1) Points EXIT AT v_outer_done = 'YES'; EXIT WHEN v_outer_done = 'YES'; (*) WHEN v_outer_done = YES EXIT; EXIT <<outer loop>>;
Incorrect. Refer to Section 4 Lesson 5. 4. What type of loop statement would you write for Point A? BEGIN FOR v_outerloop IN 1..3 LOOP -- Point A DBMS_OUTPUT.PUT_LINE('Outer loop is:'||v_outerloop|| ' and inner loop is: '||v_innerloop); END LOOP; END LOOP; END; Mark for Review (1) Points
WHILE v_innerloop <=5 LOOP FOR v_innerloop 1..5 LOOP (*) LOOP WHILE v_outerloop<v_innerloop LOOP
Incorrect. Refer to Section 4 Lesson 5. 5. Which one of these statements about using nested loops is true? Mark for Review (1) Points All the loops must be labelled The outer loop must be labelled, but the inner loop need not be labelled The outer loop must be labelled if you want to exit the outer loop from within the inner loop (*) Both loops can have the same label
Correct 6. What will be displayed when the following block is executed? DECLARE x NUMBER(6) := 0 ; BEGIN FOR i IN 1..10 LOOP FOR j IN 1..5 LOOP x := x+1 ; END LOOP; END LOOP; DBMS_OUTPUT.PUT_LINE(x); END; Mark for Review (1) Points 5 10
15 50 (*)
Correct