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

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.

主站蜘蛛池模板: 密云县| 建瓯市| 文安县| 伊通| 聂拉木县| 扎赉特旗| 宁南县| 道孚县| 乐都县| 新河县| 旌德县| 会昌县| 庆安县| 建平县| 泰安市| 炉霍县| 屏边| 兴海县| 德格县| 云南省| 沂源县| 镶黄旗| 盱眙县| 且末县| 榕江县| 衡山县| 永胜县| 绿春县| 沽源县| 得荣县| 广河县| 新民市| 沙田区| 托克逊县| 剑阁县| 乡城县| 双鸭山市| 项城市| 建瓯市| 报价| 娄底市|