Q:

python break for loop

#in Python, break statements can be used to break out of a loop
for x in range(5):
    print(x * 2)
    if x > 3:
        break
7
## When the program execution reaches a continue statement, 
## the program execution immediately jumps back to the start 
## of the loop.
while True:
    print('Who are you?')
    name = input()
    if name != 'Joe':
        continue
    print('Hello, Joe. What is the password? (It is a fish.)')
    password = input()
    if password == 'swordfish':
        break
print('Access granted.')
4
while True:
  print('I run!')
  break
print('Not in loop!')
8
number = 0

for number in range(10):
    if number == 5:
        break    # break here

    print('Number is ' + str(number))

print('Out of loop')
4
nums = [6,8,0,5,3]
product = 1

for num in nums:
  if num == 0:
    product = 0
    break # stops the for loop
  product *= num

print(product)
5
number = 0

for number in range(10):
    if number == 5:
        break    # break here

    print('Number is ' + str(number))

print('Out of loop')

4
x = 0
while True:
    x += 1
    if x == 10:
        break
1
#!/usr/bin/python

for num in range(10,20):  #to iterate between 10 to 20
   for i in range(2,num): #to iterate on the factors of the number
      if num%i == 0:      #to determine the first factor
         j=num/i #to calculate the second factor
         print '%d equals %d * %d' % (num,i,j)
         break #to move to the next number, the #first FOR
   else:        # else part of the loop
      print num, 'is a prime number'
-1

New to Communities?

Join the community