0
2

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.

Swiftで条件を満たす時のみfor文を実行する方法

Posted at

whereはめちゃくちゃ便利です。
1から64までのうち3の倍数かつ5の倍数でない数を出力させたい時には下のように書くことができます。

for i in 1...64 where i % 3 != 0 && i % 5 != 0 {
    print(i, terminator:" ")
}

terminator:" "は改行させないために書いています。
出力 1 2 4 7 8 11 13 14 16 17 19 22 23 26 28 29 31 32 34 37 38 41 43 44 46 47 49 52 53 56 58 59 61 62 64

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?