当前位置:网站首页>Fundamentals of scala (3): operators and process control
Fundamentals of scala (3): operators and process control
2022-06-28 02:11:00 【InfoQ】
One 、 Operator
+ - * / %
,+
and-
Show positive and negative signs in the unary operation table , To express addition and subtraction in a binary operation .
/
To divide or divide , Keep only integer parts and discard decimal parts
- besides ,
+
It also means adding two strings
== != < > <= >=
- stay Java in ,
==
Compare the values of the two variables themselves , The first address of two objects in memory ,equals
Compare whether the contents contained in the string are the same .
- Scala Medium
==
More similar to Java Mediumequals
, andeq()
The comparison is the address
&& || !
, The result of the operation is Boolean value
- Scala Short circuit is also supported
&& ||
= += -= *= /= %=
- stay Scala There is no
++
and--
This grammar , adopt+=
、-=
To achieve the same effect
& | ^ ~
<< >> >>>
, among<< >>
Is a signed shift left and right ,>>>
unsigned right shift
- When an object's method is called , spot . It can be omitted
- If there is only one function parameter , Or no parameters ,() It can be omitted
- Operator priority :
(characters not shown below)
* / %
+ -
:
= !
< >
&
^
|
(all letters, $, _)
object Test {
def main(args: Array[String]): Unit = {
// Standard addition operation
val i: Int = 1.+(1)
// When an object's method is called ,. It can be omitted
val n: Int = 1 + (1)
// If there is only one function parameter , Or no parameters ,() It can be omitted
val m: Int = 1 + 1
println(i + " , " + n + " , " + m)
}
}
Two 、 Process control
if - else
if ( Conditional expression ) {
Code segment
}else if ( Conditional expression ) {
Code segment
}else {
Code segment
}
object Test01_ifelse {
def main(args: Array[String]): Unit = {
println(" Please enter your age :")
val age: Int = StdIn.readInt();
if (age >= 18 && age < 30) {
println(" middle-aged ")
} else if (age < 18) {
println(" juvenile ")
} else {
println(" The elderly ")
}
}
}
- Different from other languages ,Scala Medium
if else
An expression actually has a return value , It can also be used as an expression , Defined as the return value of the last statement executed
- Scala Inconsistent return value types in , Take their common ancestor type .
- The return value can be
Unit
type , At this point, the value of the last expression is ignored , obtain()
- scala There is no ternary conditional operator in , It can be used
if (a) b else c
replacea ? b : c
- Nested branches have the same characteristics .
object Test01_ifelse {
def main(args: Array[String]): Unit = {
println(" Please enter your age :")
// The return value of the branch statement
val result: Unit = if (age >= 18) {
println(" congratulations , Grown up ")
} else if (age < 18) {
println(" sorry , You're underage ")
} else {
println(" You're a man ")
}
println("result:" + result)
val result2: String = if (age >= 18) {
println(" congratulations , Grown up ")
" congratulations , Grown up "
} else if (age < 18) {
println(" sorry , You're underage ")
" sorry , You're underage "
} else {
println(" You're a man ")
" You're a man "
}
println("result:" + result2)
// Return values of different types take the public parent class
val result3: Any = if (age >= 18) {
println(" congratulations , Grown up ")
" congratulations , Grown up "
} else if (age < 18) {
println(" sorry , You're underage ")
age
} else {
println(" You're a man ")
age
}
println("result:" + result3)
// java The ternary operation in a?b:c scala Use in if (a) b else c
val res: String = if(age>=18){
" adult "
}else{
" A minor "
}
val res1 = if (age>= 18) " adult " else " A minor "
}
}
for
for
for
for (i <- 1 to 10) {
println(i + ":hello world")
}
i
Represents a cyclic variable
<-
It is equivalent to assigning traversal value to variable
1 to 10
RichInt
A method call in , Return to oneRange
- Close back and forth
for (i <- 1 until 10) {
println(i + ":hello world")
}
until
It's alsoRichInt
One of the methods in , returnRange
class
- Before closed after opening
for (i <- Array(10, 28, 9, 3, 2)) {
println(i)
}
for (i <- List(10, 28, 9, 3, 2)) {
println(i)
}
for (i <- Set(10, 28, 9, 3, 2)) {
println(i)
}
by + step
for (i <- 1 to 10 by 2) {
println(i)
}
for (i <- 30 to 10 by -2) {
println(i)
}
true
false
continue
for(i <- collection if condition) {
}
if (i <- collection) {
if (condition) {
}
}
for (i <- 1 to 10) {
if (i != 5) {
println(i)
}
}
for (i <- 1 to 10 if i != 5) {
println(i)
}
for
for (i <- 1 to 3) {
for (j <- 1 to 3) {
println("i = " + i + ",j = " + j)
}
}
for (i <- 1 to 3; j <- 1 to 5) {
println("i = " + i + ",j = " + j)
}
object Test03_PracticeMulTable {
def main(args: Array[String]): Unit = {
for (i <- 1 to 9; j <- 1 to i) {
print(s"$j * $i = ${i * j} \t")
if (j == i) println()
}
}
}
for (i <- 1 to 10; j = 10 - i) {
println("i = " + i + ", j = " + j)
}
for {
i <- 1 to 10;
j = 10 - i
} {
println("i = " + i + " , j = " + j)
}
- Scala in for The default return value of the loop is
Unit
, example()
.
val a: Unit = for (i <- 1 to 10) {
println(i)
}
- yield:Java One method of threading in is yield Indicates a thread concession
- scala Is a keyword in Express : At present for The loop generates a collection type as the return value , Then return .
- == Return the results processed during traversal to a new Vector Collection , Use yield keyword .==
val b: immutable.IndexedSeq[Int] = for (i <- 1 to 10) yield i * i
// default implementation is Vector, Vector(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)
while and do......while
Unit
while ( The loop condition ) {
The loop body ( sentence )
Loop variable iteration
}
do{
The loop body ( sentence )
Loop variable iteration
} while( The loop condition )
Cycle break
break
continue
break
continue
breakable
break
continue
try {
for (i <- 0 until 5) {
if (i == 3)
throw new RuntimeException
println(i)
}
} catch {
case e: Exception => // Exit loop
}
import scala.util.control.Breaks
Breaks.breakable(
for (i <- 0 until 5) {
if (i == 3)
Breaks.break()
println(i)
}
)
边栏推荐
- 面试官问:能否模拟实现JS的new操作符
- 【sylixos】NEW_ Example of type 1 character drive
- Deep parsing of kubernetes controller runtime
- 要搞清楚什么是同步,异步,串行,并行,并发,进程,线程,协程
- Capacitor
- Ten MySQL locks, one article will give you full analysis
- Cesium 点击获取经纬度(二维坐标)
- Réseau neuronal pour la solution détaillée Multi - diagrammes de fondation zéro
- 深入解析kubernetes controller-runtime
- JS 随机数(随机数 小数)
猜你喜欢
How fiddle uses agents
Neural network of zero basis multi map detailed map
树莓派实现温控风扇智能降温
Appium自动化测试基础 — ADB常用命令(一)
Cesium Click to draw polygons (dynamically draw polygons)
Jenkins - Pipeline 语法
The research group of Xuyong and duanwenhui of Tsinghua University has developed an efficient and accurate first principles electronic structure deep learning method and program
How to understand query, key and value in transformer
一张图弄懂 MIT,BSD,Apache几种开源协议之间的区别
【牛客討論區】第四章:Redis
随机推荐
Database query optimization: master-slave read-write separation and common problems
Supervised, unsupervised and semi supervised learning
面试官问:能否模拟实现JS的new操作符
Hi, you have a code review strategy to check!
Overview of drug discovery-01 overview of drug discovery
Capacitor
Xctf attack and defense world misc wage earner advanced zone
205. isomorphic string
面试官问:JS的this指向
Jenkins - 邮件通知 Email Notification 插件
Where can I open an account for foreign exchange futures? Which platform is safer for cash in and out?
Description du format geojson (détails du format)
Intranet penetration with FRP
Appium自动化测试基础 — ADB常用命令(一)
[Yocto RM]3 - Yocto Project Releases and the Stable Release Process
Raspberry pie realizes intelligent cooling by temperature control fan
Is it safe to open an online futures account?
Jenkins - Groovy Postbuild 插件丰富 Build History 信息
Coscon'22 lecturer solicitation order
9. Openfeign service interface call