Home

GOTO should not be used within loops

Rule description

  • GOTO should not be used within loops

 

Non-compliant Code Example

DECLARE
  num  VARCHAR2(50);
  counter  PLS_INTEGER := 81;
BEGIN
  FOR i in 2..ROUND(SQRT(counter)) LOOP
    IF counter MOD i = 0 THEN
      num := ' is not a prime number';
      GOTO print_now; --Non compliant code (GOTO statement used in the loop)
    END IF;
  END LOOP;

  num := ' is a prime number';
 
  <<print_now>>
  DBMS_OUTPUT.PUT_LINE(TO_CHAR(counter) || num);
END;

Compliant Code Example

DECLARE
  num  VARCHAR2(50);
  counter  PLS_INTEGER := 81;
BEGIN
  FOR i in 2..ROUND(SQRT(counter)) LOOP  --Compliant code (GOTO statement is not used in the loop)
    IF counter MOD i = 0 THEN
        num := ' is not a prime number';
        DBMS_OUTPUT.PUT_LINE(TO_CHAR(counter) || num);
    ELSE
        num := ' is a prime number';
        DBMS_OUTPUT.PUT_LINE(TO_CHAR(counter) || num);
    END IF;
  END LOOP;

END;
Visual Expert 2020
 VEPLSQLRULE85