
- Scala 教程
- Scala - 首頁
- Scala - 概述
- Scala - 特性
- Scala - 環境搭建
- Scala - 構建工具 (SBT)
- Scala - 基本語法
- 資料型別和變數
- Scala - 資料型別
- Scala - 變數
- Scala - 字串
- Scala - 陣列
- Scala 運算子
- Scala - 運算子
- Scala - 算術運算子
- Scala - 關係運算符
- Scala - 邏輯運算子
- Scala - 位運算子
- Scala - 賦值運算子
- Scala 條件語句
- Scala - IF ELSE
- Scala 迴圈語句
- Scala - 迴圈語句
- Scala - while迴圈
- Scala - do-while迴圈
- Scala - for迴圈
- Scala - break語句
- Scala 類與物件
- Scala - 類與物件
- Scala - 訪問修飾符
- Scala 方法與函式
- Scala - 函式
- Scala - 按名稱呼叫函式
- Scala - 帶命名引數的函式
- Scala - 帶可變引數的函式
- Scala - 遞迴函式
- Scala - 預設引數值
- Scala - 高階函式
- Scala - 巢狀函式
- Scala - 匿名函式
- 部分應用函式
- Scala - 柯里化函式
- Scala 集合
- Scala - 集合
- Scala - 列表
- Scala - 集合
- Scala - 對映
- Scala - 元組
- Scala - 迭代器
- Scala - 選項
- Scala 模式匹配
- Scala - 模式匹配
- Scala - 異常處理
- Scala - 提取器
- Scala - 正則表示式
- Scala 檔案 I/O
- Scala - 檔案 I/O
- Scala 高階概念
- Scala - 閉包
- Scala - 特質
- Scala 有用資源
- Scala - 快速指南
- Scala - 線上編譯器
- Scala - 有用資源
- Scala - 討論
Scala - for迴圈
for 迴圈是一種重複控制結構,允許您高效地編寫需要執行特定次數的迴圈。Scala 中有各種形式的 for 迴圈,如下所述:
語法 - 帶範圍的 for 迴圈
帶範圍的 for 迴圈最簡單的語法如下:
for( var x <- Range ){ statement(s); }
這裡,Range 可以是數字範圍,表示為 i to j 或有時像 i until j。左箭頭 ← 運算子稱為生成器,之所以這樣命名是因為它從範圍中生成單個值。
嘗試以下示例程式以瞭解 Scala 程式語言中的迴圈控制語句(for 語句)。
示例
object Demo { def main(args: Array[String]) { var a = 0; // for loop execution with a range for( a <- 1 to 10){ println( "Value of a: " + a ); } } }
將上述程式儲存為 Demo.scala。以下命令用於編譯和執行此程式。
命令
\>scalac Demo.scala \>scala Demo
輸出
value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5 value of a: 6 value of a: 7 value of a: 8 value of a: 9 value of a: 10
嘗試以下示例程式以瞭解迴圈控制語句(for 語句)以列印範圍為 i until j 的迴圈,在 Scala 程式語言中。
示例
object Demo { def main(args: Array[String]) { var a = 0; // for loop execution with a range for( a <- 1 until 10){ println( "Value of a: " + a ); } } }
將上述程式儲存為 Demo.scala。以下命令用於編譯和執行此程式。
命令
\>scalac Demo.scala \>scala Demo
輸出
value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5 value of a: 6 value of a: 7 value of a: 8 value of a: 9
您可以在 for 迴圈 中使用多個用分號 (;) 分隔的範圍,在這種情況下,迴圈將遍歷給定範圍的所有可能的計算。以下是僅使用兩個範圍的示例,您也可以使用兩個以上的範圍。
示例
object Demo { 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 ); } } }
將上述程式儲存為 Demo.scala。以下命令用於編譯和執行此程式。
命令
\>scalac Demo.scala \>scala Demo
輸出
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 迴圈
帶集合的 for 迴圈的以下語法。
for( var x <- List ){ statement(s); }
這裡,List 變數是一種包含元素列表的集合型別,for 迴圈遍歷所有元素,每次返回一個元素到 x 變數中。
嘗試以下示例程式以瞭解帶數字集合的迴圈。這裡我們使用 List() 建立了此集合。我們將在單獨的章節中學習集合。Scala 程式語言中的迴圈控制語句(for 語句)。
示例
object Demo { 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 ){ println( "Value of a: " + a ); } } }
將上述程式儲存為 Demo.scala。以下命令用於編譯和執行此程式。
命令
\>scalac Demo.scala \>scala Demo
輸出
value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5 value of a: 6
語法 - 帶過濾器的 for 迴圈
Scala 的 for 迴圈允許使用一個或多個 if 語句過濾掉某些元素。以下是帶過濾器的for 迴圈的語法。要向“for”表示式新增多個過濾器,請用分號 (;) 分隔過濾器。
for( var x <- List if condition1; if condition2... ){ statement(s); }
嘗試以下示例程式以瞭解帶過濾器的迴圈。
示例
object Demo { 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 ); } } }
將上述程式儲存為 Demo.scala。以下命令用於編譯和執行此程式。
命令
\>scalac Demo.scala \>scala Demo
輸出
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 迴圈
您可以將“for”迴圈的返回值儲存在變數中,或者可以透過函式返回。為此,您需要在“for”表示式的正文前加上關鍵字 yield。以下是語法。
示例
var retVal = for{ var x <- List if condition1; if condition2... } yield x
注意 - 花括號用於儲存變數和條件,而 retVal 是一個變數,其中所有 x 的值都將以集合的形式儲存。
嘗試以下示例程式以瞭解帶 yield 的迴圈。
示例
object Demo { 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 a yield var retVal = for{ a <- numList if a != 3; if a < 8 }yield a // Now print returned values using another loop. for( a <- retVal){ println( "Value of a: " + a ); } } }
將上述程式儲存為 Demo.scala。以下命令用於編譯和執行此程式。
命令
\>scalac Demo.scala \>scala Demo
輸出
value of a: 1 value of a: 2 value of a: 4 value of a: 5 value of a: 6 value of a: 7