Home

EXIT should not be used in loops

Description

    The EXIT should not be used in loops rule states that the EXIT statement should not be used to exit from a loop. Instead, the loop should be exited by using the appropriate looping construct, such as the EXIT WHEN or EXIT FOR statements. This rule ensures that code is properly structured and that loops are exited in a predictable and consistent manner.

Key Benefits

  • No Endless Loops: EXIT should not be used in loops to avoid creating an endless loop.
  • Cleaner Code: EXIT should not be used in loops to make the code more readable and easier to maintain.
  • More Efficient Code: EXIT should not be used in loops to make the code more efficient and faster to execute.

 

Non-compliant Code Example

function int TestFunctionCall (int cnt)

DO WHILE cnt <= 15
    
    IF cnt < 0 THEN
        EXIT; //Non compliant code (EXIT statement is used within loop)
    END IF;
      
      cnt = cnt - 1
LOOP

return cnt;

end function

Compliant Code Example

function cnt TestFunctionCall (int cnt)

DO UNTIL  (cnt <= 15 AND cnt >= 0)
      cnt = cnt - 1
LOOP 

return cnt;

end function
Visual Expert 2024
 VEPBRULE55