当前位置:网站首页>Learn Scala if Else statement
Learn Scala if Else statement
2020-11-07 22:18:00 【That's how Linux should learn】
| scala It's a multi paradigm (multi-paradigm) Programming language , The original intention of the design is to integrate various features of object-oriented programming and functional programming .Scala Running on the Java Virtual machine , And compatible with existing Java Program . |

Scala IF...ELSE Statement is the result of execution through one or more statements (True perhaps False) To determine the code block to execute .
You can simply understand the execution process of conditional statements through the following figure :

if Statements are composed of Boolean expressions and subsequent statement blocks .
if The syntax format of the statement is as follows :
if( Boolean expression )
{
// If the Boolean expression is true The statement block is executed
}
If the Boolean expression is true Then execute the statement block in the braces , Otherwise, the statement block in the bracket will be skipped , Execute the statement block after the braces .
object Test {
def main(args: Array[String]) {
var x = 10;
if( x < 20 ){
println("x < 20");
}
}
}
Execute the above code , The output is :
$ scalac Test.scala
$ scala Test
x < 20
if After the statement, you can follow else sentence ,else The statement block within can be in the Boolean expression as false When it comes to execution .
if...else The syntax of is as follows :
if( Boolean expression ){
// If the Boolean expression is true The statement block is executed
}else{
// If the Boolean expression is false The statement block is executed
}
object Test {
def main(args: Array[String]) {
var x = 30;
if( x < 20 ){
println("x Less than 20");
}else{
println("x Greater than 20");
}
}
}
Execute the above code , The output is :
$ scalac Test.scala
$ scala Test
x Greater than 20
if After the statement, you can follow else if...else sentence , It is useful in the case of multiple conditional statements .
if...else if...else The syntax is as follows :
if( Boolean expression 1){
// If the Boolean expression 1 by true The statement block is executed
}else if( Boolean expression 2){
// If the Boolean expression 2 by true The statement block is executed
}else if( Boolean expression 3){
// If the Boolean expression 3 by true The statement block is executed
}else {
// If all of the above conditions are false Execute the statement block
}
object Test {
def main(args: Array[String]) {
var x = 30;
if( x == 10 ){
println("X The value of is 10");
}else if( x == 20 ){
println("X The value of is 20");
}else if( x == 30 ){
println("X The value of is 30");
}else{
println(" Unable to judge X Value ");
}
}
}
Execute the above code , The output is :
$ scalac Test.scala
$ scala Test
X The value of is 30
if...else Nested statements can be implemented in if One or more statements are embedded in if sentence .
if...else The syntax format of nested statements is as follows :
if( Boolean expression 1){
// If the Boolean expression 1 by true The statement block is executed
if( Boolean expression 2){
// If the Boolean expression 2 by true The statement block is executed
}
}
else if...else Nested statements similar if...else Nested statement .
object Test {
def main(args: Array[String]) {
var x = 30;
var y = 10;
if( x == 30 ){
if( y == 10 ){
println("X = 30 , Y = 10");
}
}
}
}
Execute the above code , The output is :
$ scalac Test.scala
$ scala Test
X = 30 , Y = 10
This paper addresses :https://www.linuxprobe.com/learn-scala-if.html
版权声明
本文为[That's how Linux should learn]所创,转载请带上原文链接,感谢
边栏推荐
- Dynamic programming -- state compression DP of set represented by binary
- Insight -- the application of sanet in arbitrary style transfer
- ngnix集群高并发
- See once to understand, graphic single chain table inversion
- Ladongo open source full platform penetration scanner framework
- 一次公交卡被“盗刷”事件带来的思考
- laravel8更新之维护模式改进
- Web安全(三)---CSRF攻击
- android基础-RadioButton(单选按钮)
- Go之发送钉钉和邮箱
猜你喜欢
![A compilation bug brought by vs2015 Update1 update [existing solutions]](/img/3b/00bc81122d330c9d59909994e61027.jpg)
A compilation bug brought by vs2015 Update1 update [existing solutions]

Lay UI left tree Dtree right list table

面部识别:攻击类型和反欺骗技术

Improvement of maintenance mode of laravel8 update

Reflection on a case of bus card being stolen and swiped

Cryptography - Shangsi Valley

数据库基本操作

Jingtao project day09

获取树形菜单列表

Data transmission of asynchronous serial communication controlled by group bus communication
随机推荐
Implementation of Caesar cipher
Cpp(二) 创建Cpp工程
In the age of screen reading, we suffer from attention deficit syndrome
CPP (3) what is cmake
Data structure and sorting algorithm
These core technology of object-oriented, after you master it, you can have a good interview
计组-总线通信控制之异步串行通信的数据传输
Speed up your website with jsdelivr
Summary of the resumption of a 618 promotion project
High concurrency in ngnix cluster
What magic things can a line of Python code do?
See once to understand, graphic single chain table inversion
Adobe Prelude / PL 2020 software installation package (with installation tutorial)
Improvement of maintenance mode of laravel8 update
Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?
爆一个VS2015 Update1更新带来的编译BUG【已有解决方案】
Android Basics - RadioButton (radio button)
easyui dialog“缓存问题”
尾-递
状态压缩:对动态规划进行降维打击