当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- LadonGo开源全平台渗透扫描器框架
- What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?
- Count the frequency of letters in text (case insensitive)
- Cpp(四) Boost安装及基本使用 for Mac
- The instanceof operator in ecmascript7 specification
- [solution] distributed timing task solution
- Web安全(三)---CSRF攻击
- 14000 word distributed transaction principle analysis, master all of them, are you afraid of being asked in the interview?
- 洞察——风格注意力网络(SANet)在任意风格迁移中的应用
- use Xunit.DependencyInjection Transformation test project
猜你喜欢
爆一个VS2015 Update1更新带来的编译BUG【已有解决方案】
Cryptography - Shangsi Valley
Idea - the. IML file was not automatically generated by the project
微信小程序request报400错误 @RequestBody接收不到
计组-总线通信控制之异步串行通信的数据传输
京淘项目day09
Go之发送钉钉和邮箱
【C++学习笔记】C++ 标准库 std::thread 的简单使用,一文搞定还不简单?
你可能不知道的Animation动画技巧与细节
Reflection on a case of bus card being stolen and swiped
随机推荐
Animation techniques and details you may not know
What do you think of the most controversial programming ideas?
use Xunit.DependencyInjection Transformation test project
构造请求日志分析系统
CPP (1) installation of cmake
Supervisor process management installation and use
sed之查找替换
Stack bracket matching
CPP (4) boost installation and basic use for Mac
use Xunit.DependencyInjection Transformation test project
到底选openstack还是vmware?
How Facebook open source framework simplifies pytorch experiment
What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?
ROS learning: remote start ROS node
awk实现类sql的join操作
Face recognition: attack types and anti spoofing techniques
Lay UI left tree Dtree right list table
尾-递
Wechat applet request reported 400 error @ requestbody failed to receive
【原创】ARM平台内存和cache对xenomai实时性的影响