0
0

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.

初めてのPython学習-基礎③-

Posted at

#繰り返し処理
for文以外にもwhile文
while「ある条件に当てはまる間、処理を繰り返す」

while 条件式:


x = 1

while x <= 100:
    print(x)
    x += 1
①条件式で使う変数xを1と定義
②xが100以下のとき以下の処理を繰り返す
③xを出力する
④xに1を足す(繰り返しのたびに1ずつ増えていく)

※while文では処理の最後に必ず変数の値を更新する
※変数の値を更新する記載は必ずwhile文の中(インデント)
 →無限ループ

繰り返し処理の終了
break

numbers = [1,2,3,4,5,6]
for number in numbers:
    print(number)
    if number == 3:
        break

その周の処理だけをスキップ
continue

numbers = [1,2,3,4,5,6]
for number in numbers:
    if number % 2 == 0:
        continue
    print(number)
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?