1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

for文の continue break else

Last updated at Posted at 2020-01-03
players = ['勇者', '戦士', '魔法使い', '僧侶']
for player in players:
    print(player)
実行結果
勇者
戦士
魔法使い
僧侶
無理やりwhile文で書くと
players = ['勇者', '戦士', '魔法使い', '僧侶']
i = 0
while i < len(players):
    print(players[i])
    i += 1
else
players = ['勇者', '戦士', '魔法使い', '僧侶']
for player in players:
    print(player)
else:
    print('終了')
elseの実行結果
勇者
戦士
魔法使い
僧侶
終了
break
players = ['勇者', '戦士', '魔法使い', '僧侶']
for player in players:
    if player == '魔法使い':
        break
    print(player)
else:
    print('終了')
breakの実行結果
勇者
戦士

while文の場合と同様に、
breakでループを抜けるとforループ全てから抜けてしまうので、
else以下は実行されない。

continue
players = ['勇者', '戦士', '魔法使い', '僧侶']
for player in players:
    if player == '魔法使い':
        continue
    print(player)
else:
    print('終了')
contiuneの実行結果
勇者
戦士
僧侶
終了
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?