LoginSignup
0
1

More than 3 years have passed since last update.

【Udemy Python3入門+応用】 40.while else文

Posted at

※この記事はUdemyの
現役シリコンバレーエンジニアが教えるPython3入門+応用+アメリカのシリコンバレー流コードスタイル
の講座を受講した上での、自分用の授業ノートです。
講師の酒井潤さんから許可をいただいた上で公開しています。

■else文

elseの使い方
else
count = 0

while count < 5:
    print(count)
    count += 1
else:
    print('done')
result
0
1
2
3
4
done

whileで指定した条件に当てはまらなかった場合、
else内に記述したものが実行される。

breakelse
break_else
count = 0

while count < 5:
    if count == 1:
        break
    print(count)
    count += 1
else:
    print('done')
result
0

breakでループを抜けた場合は、else内で記述されたものは実行されない。
逆に言えば、elseは、whilebreakによるものではなく抜けた場合、実行される。

0
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
0
1