break和continue的方法(break 直接跳出循环)与 (continue本次忽略,但之后的继续)

break 跳出循环 (做到这个结果就结束)

continue 结束本次循环 (只是这个结果不要,继续做循环)

跳出循环
while condition(条件) expression(表达)
条件满足的情况 跳出循环

break

编写文件

x = 0 
while x <= 10:
    print(x, end=" ")
    x += 1
print("小于等于10")


y = 0
print("加多一个条件,当y等于6的时候跳出循环        break;   这个是; 号 不是:")
while y <= 10:
    if y == 6:
        break;
    print(y, end=" ")
    y += 1

输出结果

C:\Users\Administrator>"C:\Users\Administrator\Desktop\new 1002.py"
0 1 2 3 4 5 6 7 8 9 10 小于等于10
加多一个条件,当y等于6的时候跳出循环      break;   这个是; 号 不是:
0 1 2 3 4 5
C:\Users\Administrator>

编写文件

print(1)
dalao = ["aa","AA","bb","BB"]
for xiaodi in dalao:
    print(xiaodi)

# .startwith() 以什么开头的方法 
# 列表 dalao 
print(2)
# 如果不是a 开头的 就 跳出循环
dalao = ["aa","AA","bb","BB"]
for xiaodi2 in dalao:
    if not xiaodi2.startswith("a"):
        break;
    print(xiaodi2)
print(3)
#如果是  a 或者 A  开头就打印 
dalao = ["aa","AA","bb","BB"]
for xiaodi2 in dalao:
    if xiaodi2.startswith("a") or xiaodi2.startswith("A"):
        print(xiaodi2)

输出结果

C:\Users\Administrator>"C:\Users\Administrator\Desktop\new 1002.py"
1
aa
AA
bb
BB
2
aa
3
aa
AA

C:\Users\Administrator>

continue;
continue 结束本次循环 (只是这个结果不要,继续做)
编写文件

print(1)
dalao = ["aa","AA","bb","BB"]
for xiaodi in dalao:
    if xiaodi.startswith("a"):
        continue;
    print(xiaodi)

输出结果

C:\Users\Administrator>"C:\Users\Administrator\Desktop\new 1002.py"
1
AA
bb
BB

C:\Users\Administrator>

编写

dalao = ["aa","AA","bb","BB"]
dajie = [1,2,3,4]
i = 0
while i < len(dajie):
    print(i)
    i += 1

输出

0
1
2
3

编写

dalao = ["aa","AA","bb","BB"]
dajie = [1,2,3,4]
a = 1
while a < len(dajie):
    for d in dalao:
        print(d,end=" ")
    print(a)
    a += 1

输出

aa AA bb BB 1
aa AA bb BB 2
aa AA bb BB 3

编写

break 满足当前条件 就跳出循环

dalao = ["aa","AA","bb","BB"]
dajie = [1,2,3,4]
a = 1
while a < len(dajie):
    for d in dalao:
        if d.startswith("b"):
            break;
        print(d,end=" ")
    print(a)
    a += 1

输出

aa AA 1
aa AA 2
aa AA 3

编写

continue 满足当前条件的 本次忽略 然后继续

dalao = ["aa","AA","bb","BB"]
dajie = [1,2,3,4]
a = 1
while a < len(dajie):
    for d in dalao:
        if d.startswith("b"):
            continue;
        print(d,end=" ")
    print(a)
    a += 1

输出

aa AA BB 1
aa AA BB 2
aa AA BB 3

你可能感兴趣的:(Python,python)