Posts

Showing posts with the label continue

15. break, continue and pass statements

Image
break statement The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop. If the break statement is inside a nested loop (loop inside another loop), the break statement will terminate the innermost loop. break can be used in while loop and for loop . The statement break is mostly required when because of some external condition, we need to exit from a loop. Example for letter in 'Python':         if letter == 'h':                          break print(letter) output: P y t In this program, we iterate through the string "Python" . We check if the letter is 'h', upon which we break from the loop. Hence, we see in our output that all the letters up till 'h' gets printed. After that, the loop terminates. Continue Statement This statement is used to tell Python to skip the rest of the statements of the current loop block and to move to next iteration, of the loop. Con