小编lea*_*tar的帖子

if: else: 和 if: continue 之间的区别

假设我有一个可迭代的:球。我想对该循环中所有非蓝色的球做一些事情。据我所知,我有两个选择:

使用 if: else:

for ball in balls:
    if ball.blue:
        # can do something with blue ball
    else:
        # now execute code for all balls that are not blue
Run Code Online (Sandbox Code Playgroud)

使用 if: 继续

for ball in balls:
    if ball.blue:
        # can do something with blue ball
        continue
    # now execute code for all balls that are not blue
Run Code Online (Sandbox Code Playgroud)

对我来说,这两种结构所能实现的目标没有区别。是否存在有意的差异?是否存在更快、更具可读性等的情况?

python loops if-statement continue

9
推荐指数
1
解决办法
4万
查看次数

标签 统计

continue ×1

if-statement ×1

loops ×1

python ×1