All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.py.rules.python.S1716.html Maven / Gradle / Ivy

The newest version!

This rule raises an issue when a break or continue statement is used outside of a loop.

Why is this an issue?

break and continue are control flow statements used inside of loops. break is used to break out of its innermost enclosing loop and continue will continue with the next iteration.

The example below illustrates the use of break in a while loop:

n = 1
while n < 10:
    if n % 3 == 0:
      print("Found a number divisible by 3", n)
      break
    n = n + 1

This next example uses continue inside a for loop:

words = ["alice", "bob", "charlie"]
for word in words:
    if word == word[::-1]:
        print("Found a palindrome", word)
        continue
    print("This is not a palindrome", word)

Python will raise a SyntaxError when break or continue are used outside of for or while loops.

If the goal is to interrupt the main program flow, quit(), exit(), os._exit() and sys.exit() are the preferred way.

Code examples

Noncompliant code example

narg=len(sys.argv)
if narg == 1:
    print('@Usage: input_filename nelements nintervals')
    break

Compliant solution

narg=len(sys.argv)
if narg == 1:
    print('@Usage: input_filename nelements nintervals')
    sys.exit()

Resources

Documentation





© 2015 - 2025 Weber Informatics LLC | Privacy Policy