当前位置:网站首页>学习Scala IF…ELSE 语句
学习Scala IF…ELSE 语句
2020-11-07 22:18:00 【Linux就该这么学】
scala 是一门多范式(multi-paradigm)的编程语言,设计初衷是要集成面向对象编程和函数式编程的各种特性。Scala 运行在 Java 虚拟机上,并兼容现有的 Java 程序。 |
Scala IF...ELSE 语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。
可以通过下图来简单了解条件语句的执行过程:
if 语句有布尔表达式及之后的语句块组成。
if 语句的语法格式如下:
if(布尔表达式)
{
// 如果布尔表达式为 true 则执行该语句块
}
如果布尔表达式为 true 则执行大括号内的语句块,否则跳过大括号内的语句块,执行大括号之后的语句块。
object Test {
def main(args: Array[String]) {
var x = 10;
if( x < 20 ){
println("x < 20");
}
}
}
执行以上代码,输出结果为:
$ scalac Test.scala
$ scala Test
x < 20
if 语句后可以紧跟 else 语句,else 内的语句块可以在布尔表达式为 false 的时候执行。
if...else 的语法格式如下:
if(布尔表达式){
// 如果布尔表达式为 true 则执行该语句块
}else{
// 如果布尔表达式为 false 则执行该语句块
}
object Test {
def main(args: Array[String]) {
var x = 30;
if( x < 20 ){
println("x 小于 20");
}else{
println("x 大于 20");
}
}
}
执行以上代码,输出结果为:
$ scalac Test.scala
$ scala Test
x 大于 20
if 语句后可以紧跟 else if...else 语句,在多个条件判断语句的情况下很有用。
if...else if...else 语法格式如下:
if(布尔表达式 1){
// 如果布尔表达式 1 为 true 则执行该语句块
}else if(布尔表达式 2){
// 如果布尔表达式 2 为 true 则执行该语句块
}else if(布尔表达式 3){
// 如果布尔表达式 3 为 true 则执行该语句块
}else {
// 如果以上条件都为 false 执行该语句块
}
object Test {
def main(args: Array[String]) {
var x = 30;
if( x == 10 ){
println("X 的值为 10");
}else if( x == 20 ){
println("X 的值为 20");
}else if( x == 30 ){
println("X 的值为 30");
}else{
println("无法判断 X 的值");
}
}
}
执行以上代码,输出结果为:
$ scalac Test.scala
$ scala Test
X 的值为 30
if...else 嵌套语句可以实现在 if 语句内嵌入一个或多个 if 语句。
if...else 嵌套语句语法格式如下:
if(布尔表达式 1){
// 如果布尔表达式 1 为 true 则执行该语句块
if(布尔表达式 2){
// 如果布尔表达式 2 为 true 则执行该语句块
}
}
else if...else 的嵌套语句 类似 if...else 嵌套语句。
object Test {
def main(args: Array[String]) {
var x = 30;
var y = 10;
if( x == 30 ){
if( y == 10 ){
println("X = 30 , Y = 10");
}
}
}
}
执行以上代码,输出结果为:
$ scalac Test.scala
$ scala Test
X = 30 , Y = 10
版权声明
本文为[Linux就该这么学]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/3585265/blog/4707743
边栏推荐
- 尾-递
- A compilation bug brought by vs2015 Update1 update [existing solutions]
- 一万四千字分布式事务原理解析,全部掌握你还怕面试被问?
- What magic things can a line of Python code do?
- Cpp(二) 创建Cpp工程
- Getting started with go wire dependency injection
- Three steps, one pit, five steps and one thunder, how to lead the technical team under the rapid growth?
- laravel8更新之维护模式改进
- Ladongo open source full platform penetration scanner framework
- use Xunit.DependencyInjection Transformation test project
猜你喜欢
京淘项目day09
More than 50 object detection datasets from different industries
Summary of the resumption of a 618 promotion project
Data transmission of asynchronous serial communication controlled by group bus communication
leetcode之判断路径是否相交
Face recognition: attack types and anti spoofing techniques
Got timeout reading communication packets解决方法
面部识别:攻击类型和反欺骗技术
Git代码提交操作,以及git push提示failed to push some refs'XXX'
[original] the influence of arm platform memory and cache on the real-time performance of xenomai
随机推荐
来自不同行业领域的50多个对象检测数据集
Ladongo open source full platform penetration scanner framework
Adobe Prelude /Pl 2020软件安装包(附安装教程)
Using pipe() to improve code readability in pandas
Cpp(四) Boost安装及基本使用 for Mac
A compilation bug brought by vs2015 Update1 update [existing solutions]
Writing method of field and field comparison condition in where condition in thinkphpp6
洞察——风格注意力网络(SANet)在任意风格迁移中的应用
Getting started with go wire dependency injection
On the stock trading of leetcode
【C++学习笔记】C++ 标准库 std::thread 的简单使用,一文搞定还不简单?
What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?
【原创】ARM平台内存和cache对xenomai实时性的影响
Get started, GIT
尾-递
Let's talk about the locks in the database
WPF 关于绘图个人总结
laravel8更新之维护模式改进
Cpp(三) 什么是CMake
It's time to end bertology