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?

let、var

Posted at

背景

再代入されないのに、varを使用。
コンパイルエラーにはならないけど、再代入が必要ないのにvarを使っている点が不適切なので、警告が出た。なので、letvarを整理

for _ in 0..<3 {
    var value = Int.random(in: 1...100) // `value`は再代入されない。冗長
    print(value)
}

let

ループごとに新しい定数をつくる

for _ in 0..<3 {
    let value = Int.random(in: 1...100)
    print(value)
}

var

初期化した変数に対して、ループごとに再代入している

var value = 0

for _ in 0..<3 {
    value = Int.random(in: 1...100)
    print(value)
}
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?