while语句

while循环语句基本格式


# basic of while

i = 1

while i <= 5:
  print("Hello Python")
  i=i+1
print("the last i=%d"%i)

求0-100之间所有数字的和


# the summation of 0-100
sum = 0
i = 0
while i < 101:
  sum = sum + i
  i+=1
print("sum=%d"%sum)

求0-100之间所有偶数之和


# the summation of even number (0-100)

i = 0

sum = 0

while i <= 100:

  if i % 2 == 0:
    print(i)
    sum += i
  i += 1
print("the summation of even number =%d"%sum)

求0-100之间所有奇数之和


# the summation of odd number(0-100)

i = 0

sum = 0

while i <= 100:
  if i % 2 != 0:
    print(i)
    sum += i
  i += 1
print("the summation of odd number=%d"%sum)