1. 程式人生 > >Scala學習筆記(二)(for循環相關)

Scala學習筆記(二)(for循環相關)

spa nts multiple bool val turn 優化 n) 例子

Scala裏if...else語句

if語句不管是在哪種語言裏是使用最多的語句了.

scala的if語句與java如出一轍.

舉個栗子就不再贅述:

一個 if 語句的語法:

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

如果布爾表達式的值為true,那麽if語句裏面的代碼模塊將被執行。如果不是這樣,第一組碼if語句結束後(右大括號後)將被執行。

示例:

object Test {
   def main(args: Array[String]) {
      var x = 10;

      if( x < 20 ){
         println("This is if statement");
      }
   }
}


if...else語句:

if語句可以跟著一個可選的else語句,當 else 塊執行時,布爾表達式條件是假的。

語法:

if...else的語法是:

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

示例:

object Test {
   def main(args: Array[String]) {
      var x = 30;

      if( x < 20 ){
         println("This is if statement");
      }else{
         println("This is else statement");
      }
   }
}


if...else if...else語句:

if語句可以跟著一個可選的else if ... else語句,這是非常有用的使用 if...else if如果測試各種條件聲明。

當使用 if , else if , else 語句有幾點要牢記。

  • if可以有零或一個else,它必須跟在else if後面。

  • 一個if 可以有零到多個else if,並且它們必須在else之前。

  • 一旦一個 else if 匹配成功,剩余的else if或else不會被測試匹配。

語法:

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.
}

示例:

object Test {
   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");
      }
   }
}

while語句也是一樣

while 循環語句多次執行,只要給定的條件為真執行目標語句。

語法:

Scala while循環的語法是:

while(condition){
   statement(s);
}

在這裏,聲明可以是單個語句或語句塊。所述條件可以是任何表達式,真值是任何非零值。當條件為true,則循環叠代。當條件為faklse,則程序控制進到緊接在循環之後的行。

舉個栗子:

object Test {
   def main(args: Array[String]) {
      // Local variable declaration:
      var a = 10;

      // while loop execution
      while( a < 20 ){
         println( "Value of a: " + a );
         a = a + 1;
      }
   }
}

在程序運行之後產生如下結果:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

do....while循環

不像while循環,測試循環條件在循環頂部,do ... while循環循環在底部檢查狀態。do... while循環類似於while循環,不同的是do ... while循環是保證至少執行一次。

語法:

Scala中do... while循環的語法是:

do{
   statement(s);
}while( condition );

註意,條件表達式出現在循環結束,所以在循環語句(多個)執行一次前的狀態進行測試。如果條件為真,控制流跳轉回後將循環語句(S)再次執行。重復這個過程,直到給定的條件為假。

代碼:

object Test {
   def main(args: Array[String]) {
      // Local variable declaration:
      var a = 10;

      // do loop execution
      do{
         println( "Value of a: " + a );
         a = a + 1;
      }while( a < 20 )
   }
}
-------------------------------------------------------------------------------(一條分界線,因為接下來是比較重要的部分)------------------------------------------------------------------------------------------------------
比較重要的部分是for循環;
scala中的for循環與java相同又不同
java的for循環是for(int i=0; i<xxx; i++)
scala的for循環是for(a <- 0 to xxxxx) (to可以替換成until,不過是一個包不包含右側數字的關系)
說他們相同是因為底層或者說理念相同:
他們都是一個賦值的過程,java賦值的過程更底層一點,而scala更像是封裝好for底層一樣.
而scala的for循環還有一個特點:

for循環中,循環將遍歷給定範圍內的所有可能的計算,可以使用分號 (;) 分隔多個範圍。下面是使用兩個範圍的例子,也可以使用兩個以上的範圍。

object Test {
   def main(args: Array[String]) {
      var a = 0;
      var b = 0;
      // for loop execution with a range
      for( a <- 1 to 3; b <- 1 to 3){
         println( "Value of a: " + a );
         println( "Value of b: " + b );
      }
   }
}

讓我們編譯和運行上面的程序,這將產生以下結果:

Value of a: 1
Value of b: 1
Value of a: 1
Value of b: 2
Value of a: 1
Value of b: 3
Value of a: 2
Value of b: 1
Value of a: 2
Value of b: 2
Value of a: 2
Value of b: 3
Value of a: 3
Value of b: 1
Value of a: 3
Value of b: 2
Value of a: 3
Value of b: 3

使用for循環遍歷list集合:
object Test {
   def main(args: Array[String]) {
      var a = 0;
      val numList = List(1,2,3,4,5,6);
    
      // for loop execution with a collection
      for( a <- numList ){
      //註意此時a被賦值的是list集合中的元素
println( "Value of a: " + a ); } } }

讓我們編譯和運行上面的程序,這將產生以下結果:

C:/>scalac Test.scala
C:/>scala Test
value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 5
value of a: 6
在scala中for循環可以添加條件進行過濾

Scala for循環允許過濾出使用一個或多個某些元素if語句(多個)。以下是對用於沿使用過濾器循環的語法。

for( var x <- List
      if condition1; if condition2...
   ){
   statement(s);
}

要添加多個過濾器到一個for表達式,分離過濾用分號(;)。

例子:

以下是for循環使用濾器的例子:

object Test {
   def main(args: Array[String]) {
      var a = 0;
      val numList = List(1,2,3,4,5,6,7,8,9,10);

      // for loop execution with multiple filters
      for( a <- numList
           if a != 3; if a < 8 ){
         println( "Value of a: " + a );
      }
   }
}

讓我們編譯和運行上面的程序,這將產生以下結果:

C:/>scalac Test.scala
C:/>scala Test
value of a: 1
value of a: 2
value of a: 4
value of a: 5
value of a: 6
value of a: 7

for循環采用yield:

可以從存儲中返回for循環中的變量的值,也可以通過函數返回。要做到這一點,可以通過關鍵字yield前綴的for表達式體,如下所示:

var retVal = for{ var x <- List
     if condition1; if condition2...
}yield x

註意在大括號已被用來保持變量和條件以及retVal的是其中x的所有值將被儲存在收集的形式的變量。

舉個栗子:

object Test {
   def main(args: Array[String]) {
      var a = 0;
      val numList = List(1,2,3,4,5,6,7,8,9,10);

      
      var retVal = for{ a <- numList 
                        if a != 3; if a < 8
                      }yield a

         for( a <- retVal){
         println( "Value of a: " + a );
      }
   }
}
運行結果為:
C:/>scalac Test.scala
C:/>scala Test
value of a: 1
value of a: 2
value of a: 4
value of a: 5
value of a: 6
value of a: 7
此處需要解釋一下,yield的作用是在for循環結束後返回的數據封裝進yield中
我把代碼進行了優化,如下:
object Demo02 {
def main(args: Array[String]) {
var a = 0;
val numList = List("a","b","c","d","e","f","g","h","i","j","k");

// for loop execution with a yield
var retVal = for{ a <- numList
if a != "c" ; if a!="a"
}yield a

// Now print returned values using another loop.
for( a <- retVal){
println( "Value of a: " + a );
}
}
}

結果為:

Value of a: b
Value of a: d
Value of a: e
Value of a: f
Value of a: g
Value of a: h
Value of a: i
Value of a: j
Value of a: k

Process finished with exit code 0

 

Scala學習筆記(二)(for循環相關)