GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||
GeezerVison is showing codescracker.com — Difference between break and continue in Python
Difference between break and continue in PythonThis tutorial is created to clear your doubts about one of the famous comparisons between two conditional statements in the Python programming language, namely the break and continue statements. break vs. continue in PythonThe table given below differentiates break and continue keywords:
break vs. continue example in PythonI'm sure that after understanding the example program given below, you will have a complete understanding of the break vs. continue keywords in Python. In Python programming, this is how these two keywords or statements are used: nums = [1, 2, 3, 4, 5] print("----Using \"break\"-------") for n in nums: if n == 3: break print(n) print("----Using \"continue\"-------") for n in nums: if n == 3: continue print(n) This program produces the output as shown in the snapshot given below: As you can see from the above program, the first block of code is: for n in nums: if n == 3: break print(n) prints "1, 2." Because when the value of n becomes 3, the condition "n == 3" or "3 == 3" evaluates to be true, and using break, the execution of the loop gets terminated. Whereas the second part is the following block of code: for n in nums: if n == 3: continue print(n) prints all the numbers except 3. Because when the condition "n == 3" evaluates to be true, therefore using "continue," the program flow jumps to the next iteration of the loop, and the remaining statement(s) after the keyword "continue," that is, print(n), get skipped for that iteration. « Previous Tutorial Next Tutorial » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. | ||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |