Difference between “while” loop and “do while” loop

前端 未结 16 1120
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 06:44

What is the difference between while loop and do while loop. I used to think both are completely same.Then I came across following piece of code:

do {
                


        
16条回答
  •  佛祖请我去吃肉
    2020-12-13 07:29

    The difference between a while constructs from Step 1 versus a do while is that any expressions within the do {} will be running at least once regardless of the condition within the while() clause.

    println("\nStep 2: How to use do while loop in Scala")
    var numberOfDonutsBaked = 0
    do {
      numberOfDonutsBaked += 1
      println(s"Number of donuts baked = $numberOfDonutsBaked")
    } while (numberOfDonutsBaked < 5)
    

    Here is detail explaination: Explanation Visit: coderforevers

提交回复
热议问题