- Learning Scala Programming
- Vikash Sharma
- 214字
- 2021-06-30 19:07:54
The while Loop
Like in most other languages, the while loop is another looping construct used. The while loop can do any repetitive task until a condition is satisfied. It means that the condition provided has to be true for the code execution to stop. Generic syntax for the while loop is:
while (condition check (if it's true))
... // Block of Code to be executed
The condition to be checked is going to be a Boolean expression. It gets terminated when the condition is false. One of the ways we can use them is:
scala> val stocks = List("APL", "GOOG", "JLR", "TESLA")
stocks: List[String] = List(APL, GOOG, JLR, TESLA)
scala> val iteraatorForStocks = stocks.iterator
iteraatorForStocks: Iterator[String] = non-empty iterator
scala> while(iteraatorForStocks.hasNext) println(iteraatorForStocks.next())
APL
GOOG
JLR
TESLA
We've used our list of stocks with a few stock names. Then we called the iterator method on the list to get an iterator for our sequence. Here iteraatorForStocks of Type Iterator[String] is a non-empty iterator that we can use to iterate over the list. Iterators have hasNext methods to check if there's a component remaining in sequence. Calling next on the iterator gives the resulting element. We've printed by iterating over the elements of our stock list. Let's take a look at do while loops.
- 觸·心:DT時代的大數據精準營銷
- Learning Apex Programming
- C#編程入門指南(上下冊)
- C++面向對象程序設計(微課版)
- 樂高機器人設計技巧:EV3結構設計與編程指導
- Python自然語言處理(微課版)
- Java設計模式及實踐
- 教孩子學編程:C++入門圖解
- Python編程與幾何圖形
- PhoneGap:Beginner's Guide(Third Edition)
- C語言程序設計上機指導與習題解答(第2版)
- Getting Started with Nano Server
- HTML5+CSS3+JavaScript 從入門到項目實踐(超值版)
- 石墨烯改性塑料
- Practical Maya Programming with Python