BREAK/CONTINUE STATEMENT IN PYTHON

GOCOURSE


Break Statement

The break statement transfers control to the statement immediately following the loop, which it is used to terminate. 
    

Example For Break Statement In "for loop":

Program:

 
  for x in "GOCOURSEwebsite":
   if x=="w":
     break
   print(x)
  print("for loop Example for break statement")



Output:

 
  G
  O
  C
  O
  U
  R
  S
  E
  Example for break statement in for loop



Example For Break Statement In "while loop":

Program:

 
  i = 1
  while i < 10:
    print(i)
    if (i == 5):
      break
    i += 1


Output:

 
  1
  2
  3
  4
  5


Continue Statement

To get the program control back to the beginning of the loop, use the Continue function. It simply moves on to the next iteration and ignores the remaining code in the loop.

Example For Continue Statement In "for loop":

Program:

  
  for x in "GOCOURSEwebsite":
   if x=="w":
     continue
   print(x)
   print("Example for continue statement in for loop")


Output:

  
  G
  O
  C
  O
  U
  R
  S 
  E
  e
  b
  s
  i
  t
  e
  Example for continue statement in for loop


Example For Continue Statement In "while loop":

Program:


  i = 0
  while i < 10:
    i += 1
    if i == 5:
      continue
    print(i)


Output:


  1
  2
  3
  4
  6
  7
  8
  9
  10





More topic in Python

Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send