Break and continue statement in python

Break and continue statement in python
0

hi everyone ,

I’m stating to learn new python language . I couldn’t understand below mentioned break and continue statement program … anyone can clearly explain me …thanks in advance …

for n in range(2, 10):
… for x in range(2, n):
… if n % x == 0:
… print(n, ‘equals’, x, ‘*’, n//x)
… break
… else:
… # loop fell through without finding a factor
… print(n, ‘is a prime number’)

continue::

for num in range(2, 10):
… if num % 2 == 0:
… print(“Found an even number”, num)
… continue
… print(“Found a number”, num)

The break statement terminates the loop that contains it and continue allows it to resume at a different point (skipping the code between the break and the continue statement). So the example code you are trying to understand only makes sense if the second for loop is located inside the first one. The code is difficult to read because it isn’t indented properly.

I think the code should look like this:

for n in range(2, 10):
    
    for x in range(2, n):
        if n % x == 0:
            print(n, 'equals', x, '*', n//x)
            break
        else:
        # loop fell through without finding a factor
            print(n, 'is a prime number')


for num in range(2, 10):
    if num % 2 == 0:
        print("Found an even number", num)
        continue
        print("Found a number", num)

If that is the case, the code iterates through numbers 2 through 10 and tells if each one is prime. If not prime, it prints that the number equals some number between 2 and the number whose primness is being determined, and some other number.

You can read the code and see.