-3

I am new to coding and was told that using breaks is a bad form of programming. I was wondering if anyone knew a good way to change the break without changing how the program functions? I have attached the code I am working with.Code in question

Ben
  • 1
  • 1
    Welcome to Stack Overflow! It's a lot easier to help when you post code as text, not as an image. – Henry Apr 08 '22 at 00:03
  • 3
    There is nothing inherently wrong with using `break`, especially in a case like this. Whoever told you that is doing you a disservice. – Tim Roberts Apr 08 '22 at 00:05

1 Answers1

0

Running your code I don't see any problems. What I would change is that you can make a an argument for the function and return True or False, like this:

def isprime(a):
    c=a-1
    for i in range(2, c):
        b=a%i
        if b == 0:
            return False
    else:
        return True

isprime(4)
isprime(7)

In this example I replaced break with return, this will break the loop but also return whether the number is prime or not.

To clarify, there is nothing wrong with using break and I think it is a very usefull possibility when looping.

KingTasaz
  • 161
  • 7
  • It's especially useful in Python because of the `else:` clause of its loops. – Barmar Apr 08 '22 at 00:11
  • I never knew that was a thing, dont even really know how it works. – KingTasaz Apr 08 '22 at 00:12
  • The `else:` block is executed if the loop runs to completion rather than being exited with `break`. It's not really needed in your version, because you return instead of breaking. You can simply put `return true` after the loop. – Barmar Apr 08 '22 at 00:13
  • I wasn't going for neatness, per say, just for useability and understandability as in copying the format of the questioner – KingTasaz Apr 08 '22 at 00:32