Bird
0
0

Find the error in this FOR loop code:

medium📝 Debug Q7 of 15
PostgreSQL - PL/pgSQL Fundamentals
Find the error in this FOR loop code:
DO $$
DECLARE
  sum INT := 0;
BEGIN
  FOR i IN 1..5 LOOP
    sum := sum + j;
  END LOOP;
  RAISE NOTICE 'Sum: %', sum;
END $$;
AVariable j is undefined inside the loop
BFOR loop range is invalid
CRAISE NOTICE syntax is incorrect
Dsum variable should be declared as TEXT
Step-by-Step Solution
Solution:
  1. Step 1: Check variables used inside the loop

    The loop variable is i, but the code uses j inside the loop.
  2. Step 2: Identify undefined variable usage

    Variable j is not declared or assigned, causing an error.
  3. Final Answer:

    Variable j is undefined inside the loop -> Option A
  4. Quick Check:

    Use correct loop variable inside FOR loops [OK]
Quick Trick: Use the loop variable name consistently inside the loop [OK]
Common Mistakes:
  • Using wrong variable inside loop
  • Assuming FOR range is invalid
  • Misunderstanding RAISE NOTICE syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes