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

前端 未结 16 1103
爱一瞬间的悲伤
爱一瞬间的悲伤 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:18

    The most important difference between while and do-while loop is that in do-while, the block of code is executed at least once, even though the condition given is false.

    To put it in a different way :

    • While- your condition is at the begin of the loop block, and makes possible to never enter the loop.
    • In While loop, the condition is first tested and then the block of code is executed if the test result is true.

提交回复
热议问题