'''
while 语句:
格式:
while 表达式:
语句
逻辑:当程序遇到while语句的时候,首先计算【表达式】的值
如果【表达式】的值为假,那么结束整个while语句
如果表达式的值为真,则执行【语句】,循环调用,直到为假停止
'''
#从1打印到小于5的数
num = 1
while num < 5:
print("num = %d"% num)
num += 1
#计算1+2+3+...+100
sum = 0
num = 1
while num <= 100:
sum += num
num += 1
#print("sum = %d" % sum)
print("sum = %d"% sum)
#打印字符串里的每一个字符
str = "My name is jjking."
index = 0
while index <= len(str):
print(str[index])
index += 1
运行结果:
num = 1
num = 2
num = 3
num = 4
sum = 5050
M
y
n
a
m
e
i
s
j
j
k
i
n
g
.