LoginSignup
0
1

More than 3 years have passed since last update.

for文 swift

Posted at

■for文一つにしても色々ある。

■数字

for i in 1..<5 {
  print(i)
}
// 1,2,3,4

for i in 1...5 {
 print(i)
}
// 1,2,3,4,5

for _ in 1...5 {
 print(a)
}
// a,a,a,a,a  _の場合繰り返したいものを5回分返すと言う意味になる。

■文字

let a = "banana"

for b in a {
  print(b)
}
//  b,a,n,a,n,a 一と文字づつ順番に返される

■ 配列

let fruits = ["Apple","banana","Orange"]

for a in fruits {
  print(a)
}
// Apple,banana,Orange  順番に返す

let contacts = ["Apple": 200, "Banana": 300, "Orange": 400]

for person in contacts {
 print(person.key)
}
// Apple,Banana,Orange

for person in contacts {
 print(person.value)
}
// 200,300,400

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