scala if 语句缩写

Scala If-Else statement is a decision making statement which executes the conditional code if the statement is true otherwise executes the else block code. Decision making code is present in almost every application and every programming language, so we will go through with if-else Scala programming language syntax and usage in this post.

Scala If-Else语句是一个决策语句,如果该语句为true,则执行条件代码,否则执行else块代码。 决策代码几乎存在于每种应用程序和每种编程语言中,因此本文将介绍if-else Scala编程语言的语法和用法。

Scala If语句 (Scala If Statement)

The if statement consists of Boolean expression followed by one or more statements.

if语句由布尔表达式组成,后跟一个或多个语句。

The syntax for an if statement is

if语句的语法是

if(boolean_expression) {
statements
}

If the boolean expression is true then the statements in the if block execute gets executed else if the expression is false then the if block is terminated and the statements outside the if block gets executed.

如果布尔表达式为true,则执行if块中的语句;如果表达式为false,则终止if块,并执行if块之外的语句。

Consider an example for if statement;

考虑一下if语句的示例;

object Student {def main(args:Array[String]) {
var studmarks = 75if( studmarks > 65) {
studmarks += 20
println("Student passed the exam with distinction")
}
}
}

We created Student object with the main method. The variable studmarks is initialized with a value of 75. In the if block, if the marks is greater than 65 then the marks is incremented by 20 and the statement “student passed the exam with distinction” is printed. Since the marks is initialized to 75 which is greater than 65 the if loop statements are executed.

我们使用main方法创建了Student对象。 变量studmarks的初始值为75。在if块中,如果分数大于65,则分数递增20,并打印“学生以优异的成绩通过考试”的陈述。 由于标记被初始化为75(大于65),因此将执行if循环语句。

Run the above code by typing Student.main(null) and you will see output as Student passed the exam with distinction.

通过键入Student.main(null)运行以上代码,当Student passed the exam with distinction您将看到输出。

Till now I have used Scala shell to run programs, we can also save Scala class in a file with .scala extension and then compile and run it. Save the above code in the file named Student.scala and then run commands shown below to compile and execute it. After compiling you will also see some .class files created.

到目前为止,我已经使用Scala shell运行程序,我们还可以将Scala类保存在扩展名为.scala的文件中,然后编译并运行它。 将上面的代码保存在名为Student.scala的文件中,然后运行下面显示的命令来编译和执行它。 编译后,您还将看到一些创建的.class文件。

Pankaj:~ pankaj$ scalac Student.scala
Pankaj:~ pankaj$ scala Student
Student passed the exam with distinction
Pankaj:~ pankaj$

Scala If其他语句 (Scala If Else Statement)

An if statement can be followed by an else statement which gets executed when the if condition is false.

if语句之后可以是else语句,如果if条件为false,则执行else语句。

The syntax of an if else statement is;

if else语句的语法为;

if(boolean_expression) {
statements
} else {
statements
}

If the boolean expression is true the if block statements are executed else if the if condition returns false the statements in the else block gets executed.

如果布尔表达式为true,则执行if块语句;如果if条件返回false,则执行else块中的语句。

Consider an example for if else statement;

考虑一个if else语句的例子;

object Student {def main(args:Array[String]) {
var marks = 55if( marks > 65) {
marks += 20
println("Student passed the exam with distinction")
} else {
println("Student marks is less than 65")
}
}
}

We are creating an object Student with main method. The variable marks is initialized with a value of 55. The if statement returns false and hence the control is transferred to else block and the statements in the else block gets executed. Since the value of marks is 55 the if statement returns false and print the else block of statement that is “Student marks is less than 65”.

我们正在使用main方法创建对象Student。 变量标记用值55初始化。if语句返回false,因此将控制权转移到else块,并执行else块中的语句。 由于标记的值为55,因此if语句返回false,并打印“学生标记小于65”的else语句块。

Run the above code by typing Student.main(null) and you will see output as Student marks is less than 65. Again you can save the code in file and compile/run it using scalac and scala commands.

通过键入Student.main(null)运行以上代码,您将看到输出,因为Student marks is less than 65 。 同样,您可以将代码保存在文件中,并使用scalac和scala命令对其进行编译/运行。

Scala If Else-If Else语句 (Scala If Else-If Else statements)

The if statement can be followed by multiple else-if else statements that can be used to test several conditions in a single if else if statement.

if语句后可以跟多个else-if else语句,这些语句可用于在单个if else if语句中测试多个条件。

if(boolean_expression1) {
statements
} else if(boolean_expression2) {
statements
} else if(boolean_expression3) {
statements
} else if(boolean_expression4) {
statements
} else {
statement
}

If the boolean_expression1 returns true the statements following the if block will be executed else the control is transferred to else if statement and if the boolean_expression2 returns true the statements following this else if block is executed else the control is transferred to next else if block and if the boolean_expression 3 returns true the statements in this block gets executed else the control is transferred to next else if statement and if the boolean_expression 4 returns true the statements in this else if block gets executed. If none of the above boolean expressions are satisfied the else block of statement gets executed.

如果boolean_expression1返回true,则将执行if块之后的语句,否则将控制转移到else if语句,如果boolean_expression2返回true,则将执行else else之后的语句,如果执行了else块,则控件将转移到下一个else if块和if boolean_expression 3返回true,则执行该块中的语句,否则控制权转移到下一个else if语句,如果boolean_expression 4返回true,则执行else块中的语句。 如果以上布尔表达式都不满足,则执行语句的else块。

Consider an example for this.

考虑一个例子。

object Student {def main(args:Array[String]) {
var marks = 80
if(marks == 60 ) {
println("Grade is C")
} else if(marks == 70) {
println("Grade is B")
} else if(marks == 80) {
println("Grade is A")
} else if(marks == 90) {
println("Grade is A+")
} else {
println("Student is not performing well hence no grade is awarded")
}
}
}

Here we are creating a Student object with main method and the variable marks is initialized with a value of 80. There are multiple else if statements checking for the marks 60,70, 80 and 90 and if the boolean expressions are satisfied the else if statements are executed.
If none of the expressions are satisfied the else block statement shall be executed. Since the marks value is 90 the else if statement for this block is executed.

在这里,我们使用main方法创建一个Student对象,并且将变量标记初始化为值80。有多个else if语句检查标记60,70、80和90,如果布尔表达式满足,则else if语句被执行。
如果没有一个表达式满足,则应执行else块语句。 由于标记值是90,因此将执行此块的else if语句。

Run the above code by typing Student.main(null) in Scala shell and you will get output as Grade is A.

通过在Scala shell中键入Student.main(null)来运行上面的代码,由于Grade is A ,您将获得输出。

Scala嵌套if其他语句 (Scala Nested If Else statement)

An if statement can be nested inside another if statement which is supported in Scala.

一个if语句可以嵌套在另一个Scala支持的if语句中。

The syntax is

语法是

if(boolean_expression1) {statementsif(boolean_expression2) {statements}}

Consider an example;

考虑一个例子;

object Student {def main(args:Array[String]) {
var marks = 70
var id =10if(id == 10 ) {
if( marks == 70) {
println("Student id is 10 and marks secured is 70")
}
}
}
}

Here we have created Student object by initializing marks and id with values 70 and 10 respectively. We are using two if statements first and if the id is 10 and marks is 70 then the statement gets executed.

在这里,我们通过分别用值70和10初始化标记和id来创建Student对象。 我们首先使用两个if语句,如果id为10且标记为70,则该语句将被执行。

Run the above code by typing Student.main(null) or by saving into a file and then compile/run it. You will get output as Student id is 10 and marks secured is 70.

通过键入Student.main(null)或保存到文件中,然后编译/运行它来运行以上代码。 您将获得输出,因为Student id is 10 and marks secured is 70

That’s all for Scala decision making if-else statements example, we will look into more Scala features in coming posts.

这就是Scala决策if-else语句示例的全部内容,我们将在以后的文章中探讨更多Scala功能。

翻译自: https://www.journaldev.com/7898/scala-if-else-statement-example-tutorial

scala if 语句缩写

scala if 语句缩写_Scala If-Else语句示例教程相关推荐

  1. hql 字符串where语句_hibernate的hql查询语句总结

    4.3 使用HQL查询 Hibernate提供了异常强大的查询体系,使用Hibernate有多种查询方式.可以选择使用Hibernate的HQL查询,或者使用条件查询,甚至可以使用原生的SQL查询语句 ...

  2. python语言基本语句-Python中的基本语句

    本文简单的介绍下Python的几个基本语句. print语句 print可同时打印多个表达式,只要将他们用逗号隔开. >>> name='Gumy' >>> gre ...

  3. python中正确的输入语句x、y=input_语句x=input()执行时,如果从键盘输入12并按回车键,则x的值是( )。_学小易找答案...

    [多选题]听障儿童辨音训练包括( ) [简答题]实现栈类(顺序栈跟链栈均可),并利用栈实现十进制到二进制的转换. 将源代码.py文件作为附件上传. [简答题]什么是空字典和空集合?如何创建 [简答题] ...

  4. 零基础入门学习Python(32)-丰富的else语句及简洁的with语句

    丰富的else语句 1.else与if语句搭配,"要么怎样,要么不怎样" if 条件:条件为真执行 else:条件为假执行 2.else与循环语句(for语句或者while语句)搭 ...

  5. R语言构建仿真数据库(sqlite)并使用dplyr语法和SQL语法查询数据库、将dplyr语法查询语句翻译为SQL查询语句

    R语言构建仿真数据库(sqlite)并使用dplyr语法和SQL语法查询数据库.将dplyr语法查询语句翻译为SQL查询语句 目录

  6. R语言嵌套的ifelse语距:将一条If语句放在另一条If语句中,该语句作为嵌套的If else调用。If else语句允许我们根据表达式结果(TRUE或FALSE)打印不同的语句,执行不同的语句块

    R语言嵌套的ifelse语句:将一条If语句放在另一条If语句中,该语句作为嵌套的If else调用.If else语句允许我们根据表达式结果(TRUE或FALSE)打印不同的语句,执行不同的语句块 ...

  7. SQL语句之DWL、DCL语句

    SQL语句之DWL.DCL语句 =============================================================================== 概述: ...

  8. python中的选择结构语句是语句_python3控制语句---选择结构语句

    python中的控制语句主要有if.if--else.if--slif--else.pass语句.其实python的控制语句与其他语言的控制语句工作原理基本一样.控制语句可以分为选择结构语句和循环结构 ...

  9. python条件语句-Python if else条件语句详解

    前面我们看到的代码都是顺序执行的,也就是先执行第1条语句,然后是第2条.第3条--一直到最后一条语句,这称为顺序结构. 但是对于很多情况,顺序结构的代码是远远不够的,比如一个程序限制了只能成年人使用, ...

最新文章

  1. SQLite-Java-Hibernate类似hibernate的数据库辅助工具
  2. java中上传文件有哪些框架,Spring MVC系列教材 (八)- SPRING MVC 上传文件
  3. AI商业产品经理:我眼中的AI简史
  4. 【.NET Core 跨平台 GUI 开发】第一篇:编写你的第一个 Gtk# 应用
  5. ArcGIS Server 开发系列(一)--编程框架总览
  6. 智能家居实训系统的项目有感!
  7. 如何让你的网站快速被百度收录。
  8. java项目加载器_Java程序的类加载器
  9. Python题库(100例)第一天
  10. 循环输出100以内的素数
  11. 全面了解什么是TPS、QPS以及两者的区别
  12. Codeforces 1744B. Even-Odd Increments
  13. 美容院 php源代码,基于ThinkPHP+B-JUI框架开发的微信美容院SPA预约消费管理系统PHP源码...
  14. c语言大一课程设计,大一课程设计(C语言).doc
  15. 污水处理问题多,泵站自动化控制系统是这样解决的
  16. 已知圆柱体的底面半径c语言,C语言编程题带答案
  17. 人工智能在教育中的应用场景
  18. 使用联通云OSS小程序直传
  19. 2014人人校招 笔试总结
  20. 用linuxdeployqt打包程序遇到qmake找不到的问题如何解决

热门文章

  1. SKlearn——逻辑斯蒂回归(LR)参数设置
  2. [转载] python中pass的使用_Python pass详细介绍及实例代码
  3. [转载] python字符串_一文详解Python字符串条件判断方法
  4. [转载] Python学习笔记——用装饰器decorator和Memoization记忆化提高效率,原理讲清楚了
  5. Vivado中的Incremental Compile增量编译技术详解
  6. UVA - 1262 Password (注意空集的特殊情况)
  7. 数据结构之链表及实现
  8. 【习题 3-1 UVA - 1585】Score
  9. 51nod 平均数(二分+树状数组)
  10. apt-get update : pulic key error