Scala条件语句(if,if-else,嵌套if-else,if-else if)

2021年3月9日15:59:25 发表评论 692 次浏览

编程中的决策类似于现实生活中的决策。在决策中, 当满足给定条件时执行一段代码。有时这些也称为控制流语句。斯卡拉使用控制语句根据某些条件控制程序的执行流程。这些用于根据程序状态的更改使执行流程前进并分支。

Scala条件语句为:

  • if
  • if-else
  • 嵌套if-else
  • if-else, if梯子

if语句

"if陈述是所有决策陈述中最简单的决策陈述。在此语句中, 仅当给定条件为true时才执行代码块, 如果条件为false则该代码块将不执行。

语法如下:

if(condition)
{
    // Code to be executed 
}

这里,

健康)状况

评估后将为真或为假。 if语句接受布尔值–如果该值为true, 则它将执行其下的语句块。

如果我们未在之后提供大括号" {"和"}"

如果(条件)

然后默认情况下, if语句将立即的一个语句视为在其块内。

例子:

if(condition)
   statement1;
   statement2;

// Here if the condition is true, if block 
// will consider only statement1 to be inside 
// its block.

流程图:

Scala条件语句(if,if-else,嵌套if-else,if-else if)1

例子:

// Scala program to illustrate the if statement
object Test {
      
// Main Method
def main(args : Array[String]) {
      
     // taking a variable
     var a : Int = 50
  
     if (a > 30 ) 
     {
  
         // This statement will execute as a > 30
         println( "lsbin" )
     }
}
}

输出如下:

lsbin

if-else语句

仅if语句就告诉我们, 如果条件为true, 则将执行语句块;如果条件为false, 则不会。但是如果条件为假, 我们想做其他事情怎么办。这是else语句。当条件为false时, 可以将else语句与if语句一起使用以执行代码块。

语法如下:

if (condition)
{
    // Executes this block if
    // condition is true
}

else
{
    // Executes this block if
    // condition is false
}

流程图:

Scala条件语句(if,if-else,嵌套if-else,if-else if)2

例子:

// Scala program to illustrate the if-else statement
object Test {
      
// Main Method
def main(args : Array[String]) {
      
     // taking a variable
     var a : Int = 650
  
     if (a > 698 ) 
     {
  
         // This statement will not 
         // execute as a > 698 is false
         println( "lsbin" )
     }
      
     else
     {
          
         // This statement will execute
         println( "Sudo Placement" )
     }
}
}

输出如下:

Sudo Placement

嵌套if-else语句

一种如果嵌套是一个如果声明那是另一个目标如果别的声明。巢状如果别的陈述意味着如果别的if语句中或else语句中的语句。 Scala允许我们嵌套如果别的内的陈述如果别的声明。

语法如下:

// Executes when condition_1 is true
if (condition_1) 
{

   if (condition_2) 
   {

      // Executes when condition_2 is true
   }

  else
  {

     // Executes when condition_2 is false
  }

}

// Executes when condition_1 is false
else
{
     
   if (condition_3) 
   {

      // Executes when condition_3 is true
   }

  else
  {

     // Executes when condition_3 is false
  }

}

流程图:

Scala条件语句(if,if-else,嵌套if-else,if-else if)3

例子:

// Scala program to illustrate 
// the nested if-else statement
object Test {
      
// Main Method
def main(args : Array[String]) {
      
     // taking three variables
     var a : Int = 70
     var b : Int = 40
     var c : Int = 100
  
     // condition_1
     if (a > b) 
     {
         // condition_2
         if (a > c)
         {
             println( "a is largest" );
         }
          
         else
         {
             println( "c is largest" )
         }
      
     }
      
     else
     {
          
          // condition_3
         if (b > c)
         {
             println( "b is largest" )
         }
          
         else
         {
             println( "c is largest" )
         }
     }
}
}

输出如下:

c is largest

如果梯子

在这里, 用户可以在多个选项中进行选择。的if语句从上至下执行。一旦控制条件之一if是真的, 与此相关的陈述if执行, 梯形图的其余部分被绕过。如果所有条件都不成立, 则最终其他语句将被执行。

语法如下:

if(condition_1)
{

     // this block will execute 
     // when condition_1 is true
}

else if(condition_2)
{

    // this block will execute 
    // when condition2 is true
}
.
.
.

else 
{

      // this block will execute when none
     // of the condition is true
}

流程图:

Scala条件语句(if,if-else,嵌套if-else,if-else if)4

例子:

// Scala program to illustrate 
// the if-else-if ladder 
object Test {
      
// Main Method    
def main(args : Array[String]) {
      
     // Taking a variable
     var value : Int = 50
  
     if (value == 20 ) 
     {
  
         // print "value is 20" when 
         // above condition is true
         println( "Value is 20" )
     } 
      
     else if (value == 25 ) 
     {
  
         // print "value is 25" when 
         // above condition is true
         println( "Value is 25" )
     } 
      
     else if (value == 40 )
     {
      
         // print "value is 40" when 
         // above condition is true
         println( "Value is 40" )
  
     } 
      
     else 
     {
          
         // print "No Match Found" 
         // when all condition is false
         println( "No Match Found" )
     }
}
}

输出如下:

No Match Found

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: