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

while loop

while is a looping statement that will iterate over a block of code until the entered test expression is true. We use this loop when we don't know how many times the iterations will go on. Refer to the following syntax:

while test_expression:
while body statements

In the while loop, first we will check the test expression. The while block will get executed only if the test expression is true. After one iteration, the expression will be checked again and this process continues until test_expression evaluates to false. This is illustrated in the following diagram: 

The following is an example of the while loop:

a = 10
sum = 0
i = 1
while i <= a:
sum = sum + i
i = i + 1
print("The sum is", sum)


Output:
The sum is 1
The sum is 3
The sum is 6
The sum is 10
The sum is 15
The sum is 21
The sum is 28
The sum is 36
The sum is 45
The sum is 55
主站蜘蛛池模板: 沅江市| 河南省| 湖口县| 忻城县| 手游| 星座| 波密县| 锦屏县| 茌平县| 宁化县| 尉犁县| 襄樊市| 沙田区| 蒙阴县| 科技| 错那县| 光山县| 津南区| 沾益县| 凤台县| 米林县| 瑞安市| 隆昌县| 平陆县| 舞钢市| 柘荣县| 璧山县| 正蓝旗| 定陶县| 澎湖县| 任丘市| 文成县| 镇赉县| 通河县| 土默特左旗| 固阳县| 鸡泽县| 裕民县| 合水县| 本溪| 淅川县|