官术网_书友最值得收藏!

Breaking out of loops

Conditional statements can be used to break out of a loop execution (for loop and while loop). When a specific condition is met, an if statement can be used to break out of a loop:

i = 0 
while True:
print("The value of i is ", i)
i += 1
if i > 100:
break

In the preceding example, the while loop is executed in an infinite loop. The value of i is incremented and printed on the screen. The program breaks out of the while loop when the value of i is greater than 100 and the value of i is printed from 1 to 100.

主站蜘蛛池模板: 恩施市| 青海省| 黄浦区| 临桂县| 永定县| 互助| 晋宁县| 江陵县| 拜城县| 珠海市| 阿城市| 夏河县| 阳泉市| 扬州市| 三台县| 榆社县| 西充县| 普洱| 茶陵县| 镇远县| 孙吴县| 镇远县| 渭南市| 博乐市| 辽源市| 东阳市| 临湘市| 景德镇市| 剑川县| 鸡东县| 洞头县| 平顶山市| 达州市| 荥阳市| 无棣县| 乐亭县| 称多县| 咸阳市| 色达县| 剑阁县| 平定县|