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.
推薦閱讀
- Word 2000、Excel 2000、PowerPoint 2000上機指導與練習
- 過程控制工程及仿真
- CorelDRAW X4中文版平面設計50例
- SharePoint 2010開發最佳實踐
- PostgreSQL Administration Essentials
- Associations and Correlations
- 新手學電腦快速入門
- Blender Compositing and Post Processing
- Learning C for Arduino
- 貫通Java Web開發三劍客
- Salesforce Advanced Administrator Certification Guide
- Excel 2007終極技巧金典
- FANUC工業機器人配置與編程技術
- JSP通用范例開發金典
- Cisco UCS Cookbook