python中的循环

# for...in...循环
classmates = ["Bob","Amy","Jack"];
for friend in classmates:
    print(friend);

# 用range来生成有序数列再求和
sum = 0;
for vab in range(101):
    sum += vab;
print(sum);

# while循环,求100以内奇数的和
sum = 0;
n = 99;
while n > 0:
    sum = sum + n;
    n = n - 2;
print(sum);

# 使用break提早结束循环
# 当n大于10的时候,循环就会结束
n = 1;
while n <= 100:
    if n > 10:
        break;
    print(n);
    n = n + 1;
print("END");

#使用continue结束本次循环
n = 0
while n < 10:
    n = n + 1
    if n % 2 == 0: # 如果n是偶数,执行continue语句
        continue # continue语句会直接继续下一轮循环,后续的print()语句不会执行
    print(n);

你可能感兴趣的:(python中的循环)