当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 获取树形菜单列表
- Analysis of kubernetes service types: from concept to practice
- See once to understand, graphic single chain table inversion
- Face recognition: attack types and anti spoofing techniques
- What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?
- Data structure and sorting algorithm
- Summary of the resumption of a 618 promotion project
- Stack bracket matching
- IDEA-项目未自动生成 .iml 文件
- High concurrency in ngnix cluster
猜你喜欢
关于晋升全栈工程师,从入门到放弃的神功秘籍,不点进来看一看?
工作1-3年的程序员,应该具备怎么样的技术能力?该如何提升?
Got timeout reading communication packets解决方法
Do not understand the underlying principle of database index? That's because you don't have a B tree in your heart
More than 50 object detection datasets from different industries
团灭 LeetCode 股票买卖问题
WPF personal summary on drawing
Improvement of maintenance mode of laravel8 update
What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?
京淘项目day09
随机推荐
Adobe Prelude /Pl 2020软件安装包(附安装教程)
Go sending pin and email
leetcode之判断路径是否相交
Get started, GIT
About the promotion of the whole stack of engineers, from the introduction to give up the secret arts, do not click in to have a look?
supervisor进程管理安装使用
Web Security (4) -- XSS attack
云计算之路-出海记:整一台 aws 免费云服务器
Data transmission of asynchronous serial communication controlled by group bus communication
A detailed explanation of microservice architecture
Fortify漏洞之 Privacy Violation(隐私泄露)和 Null Dereference(空指针异常)
Principles of websocket + probuf
【原创】ARM平台内存和cache对xenomai实时性的影响
Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?
关于update操作并发问题
On hiz buffer
Improvement of maintenance mode of laravel8 update
Adobe Lightroom / LR 2021 software installation package (with installation tutorial)
14000 word distributed transaction principle analysis, master all of them, are you afraid of being asked in the interview?
Android 9.0/P WebView 多进程使用的问题