Good programmers to share large data line learning Scala branching and looping

Good programmers to share large data for learning the route branch and loop Scala
3.3 conditional expression.
Expression: a code block having the execution result. The result is a specific value or ()

Expressions of thinking: an expression centered programming ideas

1. The difference between statements and expressions: the expression returns a value, the statement is executed. Expression is a general statement block, after execution, returns a value

2. Do not use the return statement, that is the return value of the last expression

if / else expression has value, that value is the value with the expression after the if or else

{ConditionDemo Object
DEF main (args: the Array [String]) {
var. 1 = X
// the if / else expression value to the variable Y
Val Y = IF (X> 0). 1 -1 the else
the println (Y)

//支持混合型表达式
//返回类型是Any
val z = if(x>0) "success"  else  -1
println(z)

//如果缺失else,相当于if(x>2) 1 else ()
//返回类型是AnyVal
//如果进行类型判断和转换,可以使用:
//var b = if(m.isInstanceOf[Int]) m.asInstanceOf[Int] else 0
val m = if(x>2) 1
println(m)

//在scala中,每个表达式都有值,scala中有个unit类,写作(),相当于Java中的 void
val n = if(x>2) 1 else ()
println(n)

//if 嵌套
val  k= if(x<0) 0 else if (x>=1) 1 else -1
println(k)

}

Results of the:

Scala's condition is relatively simple expressions, such as:

Note: 1, every expression has a type

2, has a value of conditional expression

3, hybrid expression, resulting Any or AnyVal

4, scala no switch statement

. 3.4 Expression
Object BlockExpressionDemo {
DEF main (args: the Array [String]) {

var x = 0

//在scala中,{}中可以包含一系列表达式,块中最后一个表达式的值就是块的值
val res = {
  if (x < 0) {
    -1
  } else if (x >= 1) {
    1
  } else {
    "error"
  }
}
  println(res)

  val x0 = 1
  val y0 = 1
  val x1 = 2
  val y1 = 2
  val distance = {
    val dx = x1 - x0
    val dy = y1 - y0
    Math.sqrt(dx*dx+dy*dy)
  }
  println(distance)

//块语句,最后一句是赋值语句,值是unit类型的

 var res2 = {
   val dx = x1 - x0
   val dy = y1 - y0
  val res =  Math.sqrt(dx*dx+dy*dy)
 }
println(res2)

}

}

Results of the:

note:

1, the expression value of the block is the last value of an expression

2, the value of the assignment is the type of unit,

3.5 Cyclic
there for and while loops in the scala, more for loop

syntax structure for loop: for (i <- expression / array / set)

while (conditional statement) expression {}

Expression do {} while ()

ForDemo Object
DEF main (args: the Array [String]) {
// each loop will assign a value interval I
for (I <-. 1 to 10)
the println (I)

//for i <-数组
val arr = Array("a", "b", "c")
for( i <- arr)
  println(i)

val s = "hello"
for(i <- 0 until s.length){
  println(s(i))
}
//  或者
//   for(c <- s)println(c)
//  或者
//  for(i <- 0 until s.length){
//  println(s.charAt(i))
//使用了隐式转换,把字符串变成一个ArrayCharSequence 
// }

//高级for循环
for(i <- 1 to 3 ; j<- 1 to 3 if i != j)
  print((10*i + j) + "")
  println()

//for推导式,如果for循环的循环体以yeild开始,则该循环会构建出一个集合或者数组,每次迭代生成其中的一个值。
val v= for ( i <- 1 to 10 )yield i*10
        println (v)

//也可以借助函数由初始数组生成一个新的数组
  val arr1 = Array(1,2,3,4,5,6,7,8,9)
  val arr2 = arr.map(_*10)
  val arr3 = arr.filter(_%2==0)

}
}

Guess you like

Origin blog.51cto.com/14479068/2434814