LoginSignup
7
7

More than 5 years have passed since last update.

Scalaの繰り返し文

Posted at

Scalaでの繰り返し構文

Scalaで使えるループはfor, while, do-whileの3種類があるみたいなのでそれぞれを使って"Hello World"が10回表示されるものを書いてみました。

HelloWorld.scala
object HelloWolrd {
  def main(args: Array[String]): Unit = {
    forHello
    whileHello
    doWhileHello
  }

  def forHello = for(i <- 0 to 9) println("Hello Wolrd " + i)

  def whileHello = {
    var i = 0
    while(i < 10) { 
      println("Hello Wolrd " + i)
      i += 1
    }
  }

  def doWhileHello = {
    var i = 0
    do {
      println("Hello Wolrd " + i)
      i += 1
    } while(i < 10)
  }
}

whileとdo-whileに対してforはかなりすっきり書けることが分かりました、whileとdo-whileはあんまり使われなさそうな予感。

7
7
1

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