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编程语言的语法和用法。
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 = 75
if( 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$
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 = 55
if( 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命令对其进行编译/运行。
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
,您将获得输出。
An if statement can be nested inside another if statement which is supported in Scala.
一个if语句可以嵌套在另一个Scala支持的if语句中。
The syntax is
语法是
if(boolean_expression1) {
statements
if(boolean_expression2) {
statements
}
}
Consider an example;
考虑一个例子;
object Student {
def main(args:Array[String]) {
var marks = 70
var id =10
if(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 语句缩写