Course
Continue Statement
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
Continue Statement
Python Continue Statement
Python
continue
statement is used to skip the execution of the program block and returns the control to the beginning of the current loop to start the next iteration. When encountered, the loop starts next iteration without executing the remaining statements in the current iteration.The continue statement can be used in both
while
and for
loops.Syntax
continue
Flow Diagram
The flow diagram of the
continue
statement looks like this
The
continue
statement is just the opposite to that of break
. It skips the remaining statements in the current loop and starts the next iteration.Example 1
Now let's take an example to understand how the
continue
statement works in Pythonfor letter in 'Python': # First Example if letter == 'h': continue print ('Current Letter :', letter)var = 10 # Second Examplewhile var > 0: var = var -1 if var == 5: continue print ('Current variable value :', var)print ("Good bye!")
When the above code is executed, it produces the following output
Current Letter : PCurrent Letter : yCurrent Letter : tCurrent Letter : oCurrent Letter : nCurrent variable value : 9Current variable value : 8Current variable value : 7Current variable value : 6Current variable value : 4Current variable value : 3Current variable value : 2Current variable value : 1Current variable value : 0Good bye!
Checking Prime Factors
Following code uses continue to find the prime factors of a given number. To find prime factors, we need to successively divide the given number starting with 2, increment the divisior and continue the same process till the input reduces to 1.
The algorithm for finding prime factors is as follows −
- Accept input from user (n)
- Set divisor (d) to 2
- Perform following till n>1
- Check if given number (n) is divisible by divisor (d).
- If n%d==0
- a. Print d as a factor
- Set new value of n as n/d
- Repeat from 4
- If not
- Increment d by 1
- Repeat from 3
Given below is the Python code for the purpose
num = 60print ("Prime factors for: ", num)d=2while num > 1: if num%d==0: print (d) num=num/d continue d=d+1
On executing, this code will produce the following output
Prime factors for: 602235
Assign different value (say 75) to num in the above program and test the result for its prime factors.
Prime factors for: 75355
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.