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?

do-try-catch ... continue ?

0
Posted at

なにこれ?

なんか見つけたので共有しておきます。

do-try-catch-contitue/break構文

何いってるか分かりませんね。

do文によるループ

コードです。

var i = 1
loop: do {
  print(i)
  defer { i += 1 }
  if i < 10 {
    continue loop
  }
}

do文にはラベルを付けることが出来ます。
そして、continueで繰り返しが可能です。
何をいってるのか分かりません。

途中での終了

コードです。

label: do {
  print("a")
  if true {
    break label
  }
  print("b")
}

do文ではラベルとbreakで途中で終了することが出来ます。
何を言ってるのか分かりません。

tryによる条件分岐とループ

コードです。

func takeCounter() -> () -> Int {
  var i = 0
  return { i += 1; return i }
}

enum E: Error { case e }
func throwIfEven(_ i: Int) throws(E)  {
  if i.isMultiple(of: 2) {
    throw .e
  }
}

let c = takeCounter()
loop: do {
  let i = c()
  try throwIfEven(i)
  print("Odd")
  if i < 100 {
    continue loop
  }
}
catch {
  print("Even")
  continue loop
}

catchからもcontinueを使って繰り返しが出来ます。
ほんとうに何を言っているのか分かりません。

まとめ

いかがでしたでしょう?
ラベルを使ってdo文で繰り返しが行えることが分かりました。

なにこれ?

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?