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

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.

主站蜘蛛池模板: 湖南省| 麻江县| 清河县| 景洪市| 镇平县| 饶阳县| 西藏| 怀来县| 彰化市| 翁牛特旗| 和硕县| 云和县| 清苑县| 伊宁市| 宜兴市| 东莞市| 海林市| 同心县| 龙游县| 湘阴县| 犍为县| 高清| 巴中市| 迁安市| 营口市| 洛浦县| 聂拉木县| 额济纳旗| 开封县| 东乌珠穆沁旗| 太保市| 长沙县| 双桥区| 阳新县| 中山市| 万载县| 聂荣县| 惠州市| 临澧县| 广宁县| 内江市|