Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Big data, a good programmer, shares the learning route of Scala branches and loops.

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Good Programmer Big Data Learning Routes Share Scala Branches and Loops

3.3. conditional expression

Expression: A block of code with an execution result. The result is a specific value or

Thinking about expressions: expression-centric programming thinking

1. The difference between an expression and a statement is that the expression has a return value and the statement is executed. An expression is usually a statement block that returns a value when executed.

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

The if/else expression has a value, which is the value of the expression that follows if or else.

object ConditionDemo {

def main(args: Array[String]){

var x = 1

//assign the value of the if/else expression to variable y

val y = if (x > 0 ) 1 else -1

println(y)

//Support mixed expressions//Return type is Anyval z = if(x>0) "success" else -1println(z)//If else is missing, it is equivalent to if(x>2) 1 else ()//Return type is AnyVal//If type judgment and conversion are performed, you can use://var b = if (m.isInstanceOf[Int]) m.asInstanceOf[Int] else 0val m = if(x>2) 1println(m)//In scala, every expression has a value, scala has a unit class, written (), voidval n = if(x>2) 1 else ()println(n)//if nested val k= if(x=1) 1 else -1println(k)

}

Implementation results:

Scala's conditional expressions are simpler, for example:

Note: 1, each expression has a type

2, conditional expression has value

3, mixed expression, the result is Any or AnyVal

Scala has no switch statement.

3.4. chunk expression

object BlockExpressionDemo {

def main(args: Array[String]){

var x = 0//In scala,{} can contain a series of expressions, the value of the last expression in the block is the value of the block 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)//block statement, last sentence is assignment statement, value is var res2 = { val dx = x1 - x0 val dy = y1 - y0 val res = Math.sqrt(dx*dx+dy*dy) }println(res2)

}

}

Implementation results:

Note:

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

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

3.5. cycle

There are for loops and while loops in scala, and there are more for loops.

for loop syntax structure: for (i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report