Bird
0
0

Examine the following PostgreSQL code snippet:

medium📝 Debug Q6 of 15
PostgreSQL - PL/pgSQL Fundamentals
Examine the following PostgreSQL code snippet:
DO $$
DECLARE
  counter INT := 1;
BEGIN
  WHILE counter <= 3 LOOP
    RAISE NOTICE 'Counter: %', counter;
  END LOOP;
END $$;

What is the main issue with this code?
AThe loop variable 'counter' is never incremented, causing an infinite loop.
BThe RAISE NOTICE statement syntax is incorrect.
CThe WHILE loop condition should use '<' instead of '<='.
DThe DECLARE block is missing a semicolon after variable declaration.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the loop condition

    The loop runs while 'counter' is less than or equal to 3.
  2. Step 2: Check loop body

    The code raises a notice but does not increment 'counter'.
  3. Step 3: Consequence

    Since 'counter' never changes, the condition remains true indefinitely, causing an infinite loop.
  4. Final Answer:

    The loop variable 'counter' is never incremented, causing an infinite loop. -> Option A
  5. Quick Check:

    Loop variable must be updated inside WHILE loop [OK]
Quick Trick: Always update loop counters inside WHILE loops [OK]
Common Mistakes:
  • Forgetting to increment the loop variable inside the loop
  • Misunderstanding loop termination conditions
  • Incorrect syntax in RAISE NOTICE statements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes