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

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.

主站蜘蛛池模板: 昌乐县| 永德县| 班戈县| 南部县| 新昌县| 台安县| 达尔| 自治县| 平度市| 德清县| 哈巴河县| 新源县| 马山县| 永吉县| 宁津县| 和田市| 临海市| 南充市| 花莲市| 那曲县| 乌拉特前旗| 贵南县| 新巴尔虎左旗| 化隆| 玉田县| 阿巴嘎旗| 顺平县| 六安市| 兴国县| 廉江市| 阳江市| 潮州市| 昆山市| 岱山县| 深圳市| 霸州市| 隆尧县| 南宫市| 西和县| 田东县| 新郑市|