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

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

    While Loop:

    while(test-condition)
    {
          statements;
          increment/decrement;
    }
    
    1. Lower Execution Time and Speed
    2. Entry Conditioned Loop
    3. No fixed number of iterations

    Do While Loop:

    do
    {
          statements;
          increment/decrement;
    }while(test-condition);
    
    1. Higher Execution Time and Speed
    2. Exit Conditioned Loop
    3. Minimum one number of iteration

    Find out more on this topic here: Difference Between While and Do While Loop

    This is valid for C programming, Java programming and other languages as well because the concepts remain the same, only the syntax changes.

    Also, another small but a differentiating factor to note is that the do while loop consists of a semicolon at the end of the while condition.

提交回复
热议问题