3.Scala- control structures and functions

Chapter 3 control structures and functions

Java in the expressions and statements as two different things, there is an expression value, the statement has no value,
Just control structure. In Scala, almost all grammatical structures have values.

3.1 If else expression

scala> val x = 5
x: Int = 5

scala> val s = if( x > 0 ) 1 else -1
s: Int = 1

 

S why there is value?
If the statement returns the type depends on the last statement. The statement following the semicolon is not necessary.
Scala no ternary operator is not required.
 
 
  If expression will return a value type, or else if the return type if not the same, it
Any return type (all types of public supertype). 

 

scala> val s1 = if( x > 0) "hello" else -1
s1: Any = hello

scala> val s3 = if ( x < 0 ) "hello"
s3: Any = ()

 

 

 
  In the absence of a judge, nothing to return, but Scala will consider any expression
There are value for null values, use the Unit class, do write () is called [without a useful placeholder, the equivalent of java
The void] 
scala> val s4 = if(x < 0 ) "hello" else ()
s4: Any = ()

 

NOTE: The end of the line does not need a semicolon, as long as can be determined from the context of the sentence can be terminated. but if
Write multiple statements on a single line, you need to split a semicolon. In Scala, comprising a series of quick expression {}
Style, the result is an expression. Value of the block of the last expression is the value for the block. 

 

 

3.2 while expression

 

  Scala and Java to provide the same while and do loops, and If different statements, While statements
No value in itself, that statement is the result of the whole While Unit type (). 
while (n > 0) { 
    r = r * n
    n -= 1
    println(r)
}
do{
    r = r * n
    n -= 1
    println(r)
}while(n > 0)

 

Note: scala does not provide break and continue statement to exit the loop, if needed break, can
1 done by several methods, using Boolean control variable type 2, using nested function, the function
return 3, using the method break Breaks object. 
// loop termination 
Import scala.util.control.Breaks 
Val Looper = new new Breaks () // parentheses may be omitted 

var COUNT = 0 
looper.breakable {          // . May be replaced by spaces 
  the while (COUNT <= 100 ) { 
    COUNT + . 1 = 
    the println (COUNT) 
    IF (COUNT == 10 ) { 
      Looper. BREAK () 
    } 
  }
//多重while循环的终止
val whileLooper1 = new Breaks
val whileLooper2 = new Breaks

whileLooper1.breakable{
  while (true){
    count += 1
    if(count == 10){
      whileLooper1.break()
    }
    
    whileLooper2.breakable{
      while (true){
        if(count == 20){
          whileLooper2.break()
        }
      }
    }
    
  }
}

 

 

 

 

3.3 for expression 

  Scala also offers a lot of features for the cycle for this common control structure for these
Cycle characteristics is called for derivations (for comprehensions) or for expression (for
expression)。
 
 
  The term originated in the derivation of functional programming. 
Like the variable name <- collection of such an expression is also called generator expression that will
Generating a set of values ​​based on the individual. 
//多重for循环
for(i <- 1 to 3; j <-1 to 3){
  println(i + "----" + j)
  println()
}

 

//输出10 9 8 7 ... 1
for(i <- Range(10, 0, -1)){
  println(i)
}


//输出1~9
for(i <- 1 until 10){
  println(i)
}

 

 

  Protection type, also called the guards, you can add one or more of the guards, the statement does not need to continue

 

// achieve the output having 1 to 10, only even-numbered output 
for (I <- 1 to 10 IF I% 2 == 0 ) { 
  the println (I) 
}

 

 

  For inferential variables may be introduced 

// add the for loop variable expression 
for (I <-. 1 to 10; J = 10 - I; m = J * I) { 
  the println ( "J:" + J) 
  the println ( "m:" + m) 
}
  We need to return a value how to do?
  Use yield keywords to generate a new collection for expression. for-yield expressions
The resulting set of derived type set out in accordance with the type of traversed. 

 

scala> val forResult = for (i <- 1 to 10) yield i
forResult: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

 

 Tip screaming: {} and () for it can be for expression. for derivation have an unwritten convention: when
Use the original contains only a single expression parentheses for derivation, when used braces comprising a plurality of expressions
number. It is noteworthy that, when using the original brackets, the earlier version of Scala requirements must be between expression
Use a semicolon.

 

 

// used as a condition for loop braces package 
for { 
  I <-. 3. 1 to 
  m = I 2 * 
} 
  the println (m) // need to use a specific logic

 

Guess you like

Origin www.cnblogs.com/LXL616/p/11105476.html