mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-11-23 06:03:07 +03:00
Merge pull request #1950 from pikazlou/scala-flow-control-fix
fix docs for Flow Control
This commit is contained in:
commit
510f4e239e
@ -237,7 +237,7 @@ r foreach println
|
|||||||
|
|
||||||
(5 to 1 by -1) foreach (println)
|
(5 to 1 by -1) foreach (println)
|
||||||
|
|
||||||
// A while loops
|
// A while loop
|
||||||
var i = 0
|
var i = 0
|
||||||
while (i < 10) { println("i " + i); i += 1 }
|
while (i < 10) { println("i " + i); i += 1 }
|
||||||
|
|
||||||
@ -245,17 +245,18 @@ while (i < 10) { println("i " + i); i += 1 } // Yes, again. What happened? Why
|
|||||||
|
|
||||||
i // Show the value of i. Note that while is a loop in the classical sense -
|
i // Show the value of i. Note that while is a loop in the classical sense -
|
||||||
// it executes sequentially while changing the loop variable. while is very
|
// it executes sequentially while changing the loop variable. while is very
|
||||||
// fast, faster that Java loops, but using the combinators and
|
// fast, but using the combinators and comprehensions above is easier
|
||||||
// comprehensions above is easier to understand and parallelize
|
// to understand and parallelize
|
||||||
|
|
||||||
// A do while loop
|
// A do-while loop
|
||||||
i = 0
|
i = 0
|
||||||
do {
|
do {
|
||||||
println("i is still less than 10")
|
println("i is still less than 10")
|
||||||
i += 1
|
i += 1
|
||||||
} while (i < 10)
|
} while (i < 10)
|
||||||
|
|
||||||
// Tail recursion is an idiomatic way of doing recurring things in Scala.
|
// Recursion is the idiomatic way of repeating an action in Scala (as in most
|
||||||
|
// other functional languages).
|
||||||
// Recursive functions need an explicit return type, the compiler can't infer it.
|
// Recursive functions need an explicit return type, the compiler can't infer it.
|
||||||
// Here it's Unit.
|
// Here it's Unit.
|
||||||
def showNumbersInRange(a: Int, b: Int): Unit = {
|
def showNumbersInRange(a: Int, b: Int): Unit = {
|
||||||
@ -273,7 +274,7 @@ val x = 10
|
|||||||
if (x == 1) println("yeah")
|
if (x == 1) println("yeah")
|
||||||
if (x == 10) println("yeah")
|
if (x == 10) println("yeah")
|
||||||
if (x == 11) println("yeah")
|
if (x == 11) println("yeah")
|
||||||
if (x == 11) println ("yeah") else println("nay")
|
if (x == 11) println("yeah") else println("nay")
|
||||||
|
|
||||||
println(if (x == 10) "yeah" else "nope")
|
println(if (x == 10) "yeah" else "nope")
|
||||||
val text = if (x == 10) "yeah" else "nope"
|
val text = if (x == 10) "yeah" else "nope"
|
||||||
|
Loading…
Reference in New Issue
Block a user