Python break, continue statement
The break statement is used to exit a for or a while loop. The purpose of this statement is to end the execution of the loop (for or while) immediately and the program control goes to the statement after the last statement of the loop. If there is an optional else statement in while or for loop it skips the optional clause also. Here is the syntax.
while (expression1) : statement_1 statement_2 ...... if expression2 : break
for variable_name in sequence : statement_1 statement_2 if expression3 : break
Example: break in for loop
In the following example for loop breaks when the count value is 5. The print statement after the for loop displays the sum of first 5 elements of the tuple numbers.
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Declaring the tuple num_sum = 0 count = 0 for x in numbers: num_sum = num_sum + x count = count + 1 if count == 5: break print("Sum of first ",count,"integers is: ", num_sum)
Sum of first 5 integers is: 15
Example: break in while loop
In the following example while loop breaks when the count value is 5. The print statement after the while loop displays the value of num_sum (i.e. 0+1+2+3+4).
num_sum = 0 count = 0 while(count<10): num_sum = num_sum + count count = count + 1 if count== 5: break print("Sum of first ",count,"integers is: ", num_sum)
Sum of first 5 integers is : 10
The continue statement is used in a while or for loop to take the control to the top of the loop without executing the rest statements inside the loop. Here is a simple example.
for x in range(7): if (x == 3 or x==6): continue print(x)
0 1 2 4 5
In the above example, the for loop prints all the numbers from 0 to 6 except 3 and 6 as the continue statement returns the control of the loop to the top
Test your Python skills with w3resource's quiz
Python: Tips of the Day
Python: Time library
Time library provides lots of time related functions and methods and is good to know whether you're developing a website or apps and games or working with data science or trading financial markets. Time is essential in most development pursuits and Python's standard time library comes very handy for that.
Let's check out a few simple examples:
import time time_now=time.strftime("%H:%M:%S",time.localtime()) print(time_now) date_now=time.strftime("%Y-%b-%d",time.localtime()) print(date_now)
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework