目录

斯卡拉 - 如果是的话( IF ELSE)

本章将指导您完成Scala编程中的条件构造语句。 以下是在大多数编程语言中发现IF ... ELSE结构的典型决策的一般形式。

流程图 (Flow Chart)

以下是条件语句的流程图。

Scala IF ... ELSE结构

如果声明

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

语法 (Syntax)

'if'语句的语法如下。

if(Boolean_expression) {
   // Statements will execute if the Boolean expression is true
}

如果布尔表达式的计算结果为true,那么将执行'if'表达式中的代码块。 如果没有,将执行'if'表达式结束后(在结束大括号之后)的第一组代码。

尝试使用以下示例程序来理解Scala编程语言中的条件表达式(如果表达式)。

例子 (Example)

object Demo {
   def main(args: Array[String]) {
      var x = 10;
      if( x < 20 ){
         println("This is if statement");
      }
   }
}

将上述程序保存在Demo.scala 。 以下命令用于编译和执行此程序。

Command

\>scalac Demo.scala
\>scala Demo

输出 (Output)

This is if statement

If-else 语句

'if'语句后面可以跟一个可选的else语句,该语句在布尔表达式为false时执行。

语法 (Syntax)

if ... else的语法是 -

if(Boolean_expression){
   //Executes when the Boolean expression is true
} else{
   //Executes when the Boolean expression is false
}

尝试使用以下示例程序来理解Scala编程语言中的条件语句(if- else语句)。

例子 (Example)

object Demo {
   def main(args: Array[String]) {
      var x = 30;
      if( x < 20 ){
         println("This is if statement");
      } else {
         println("This is else statement");
      }
   }
}

将上述程序保存在Demo.scala 。 以下命令用于编译和执行此程序。

Command

\>scalac Demo.scala
\>scala Demo

输出 (Output)

This is else statement

If-else-if-else 语句

'if'语句后面可以跟一个可选的' else if...else '语句,这对于使用单个if ... else if语句测试各种条件非常有用。

当使用if,else if,else语句时,要记住几点。

  • 一个'if'可以有零个或一个其他的,它必须在任何其他if之后。

  • 一个'if'如果有的话可以有零到多个,并且它们必须在else之前。

  • 一旦其他成功,如果其他人或其他人将被测试,他将不会留下其他人。

语法 (Syntax)

以下是'if ... else if ... else'的语法如下 -

if(Boolean_expression 1){
   //Executes when the Boolean expression 1 is true
} else if(Boolean_expression 2){
   //Executes when the Boolean expression 2 is true
} else if(Boolean_expression 3){
   //Executes when the Boolean expression 3 is true
} else {
   //Executes when the none of the above condition is true.
}

尝试使用以下示例程序来理解Scala编程语言中的条件语句(if- else- if- else语句)。

例子 (Example)

object Demo {
   def main(args: Array[String]) {
      var x = 30;
      if( x == 10 ){
         println("Value of X is 10");
      } else if( x == 20 ){
         println("Value of X is 20");
      } else if( x == 30 ){
         println("Value of X is 30");
      } else{
         println("This is else statement");
      }
   }
}

将上述程序保存在Demo.scala 。 以下命令用于编译和执行此程序。

Command

\>scalac Demo.scala
\>scala Demo

输出 (Output)

Value of X is 30

Nested if-else 语句

嵌套if-else语句总是合法的,这意味着你可以在另一个ifelse-if语句中使用一个ifelse-if语句。

语法 (Syntax)

嵌套if-else的语法如下 -

if(Boolean_expression 1){
   //Executes when the Boolean expression 1 is true
   if(Boolean_expression 2){
      //Executes when the Boolean expression 2 is true
   }
}

尝试使用以下示例程序来理解Scala编程语言中的条件语句(nested-if语句)。

例子 (Example)

object Demo {
   def main(args: Array[String]) {
      var x = 30;
      var y = 10;
      if( x == 30 ){
         if( y == 10 ){
            println("X = 30 and Y = 10");
         }
      }
   }
}

将上述程序保存在Demo.scala 。 以下命令用于编译和执行此程序。

Command

\>scalac Demo.scala
\>scala Demo

输出 (Output)

X = 30 and Y = 10
↑回到顶部↑
WIKI教程 @2018