官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 南和县| 阿拉尔市| 哈尔滨市| 北流市| 嵩明县| 阿拉尔市| 衡南县| 子洲县| 大竹县| 永嘉县| 五莲县| 怀安县| 衡山县| 昌江| 肥城市| 佛教| 普陀区| 靖安县| 桐乡市| 商丘市| 平远县| 阿拉尔市| 崇义县| 云梦县| 小金县| 台东市| 凤山县| 灌阳县| 龙陵县| 蒙山县| 沛县| 阿巴嘎旗| 平泉县| 鄢陵县| 沁源县| 潞城市| 买车| 乐至县| 齐河县| 宁阳县| 高台县|