Help us understand the problem. What is going on with this article?

Swiftでの繰り返し処理

swiftの繰り返し処理

アウトプットとして、初めてQittaで投稿します。もし間違えてるとこがありましたら、指摘して下さい!

処理を繰り返す for - in文

qiita.swift
for 変数 in 配列 {
   ステートメント
}
example.swift
//配列の定義
var numList = [2, 4, 6, 8, 10]
var sum = 0

for num in numList {
   sum += num
}
print("合計\(sum)") => 30

ちなみに変数numは繰り返し処理のなかで定義したので、外では使えません。

for - in で指定回数繰り返す

レンジから取り出した値を使わない場合、値を受け取る変数の代わりに _ を指定する

example.swift
var stars = ""
for _ in 1...5 {
   stars += "★"
   print(stars)
}
=>
  ★★
  ★★★
  ★★★★
  ★★★★★

レンジとは

レンジは整数の範囲を表します。二つの違いは終了値を含むか含まないか

レンジ.swift
開始値...終了値
1...5 //1 ~ 5

開始値..<終了値
1..< 5 //1 ~ 4
}

最後に

初めての投稿でわかりづらいかもしれませんが、これからも投稿を続けようと思いおいます。Swift勢のみなさん頑張りましょう!ありがとうございました

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした