What is the usefulness and importance of "do ... while"?

19

The while command is a repeating structure with a condition at the beginning of the declaration.

Example:

  

while (condition) {
        sentences to run
  }

Already do... while has verification at the end, ie, the repeat structure code runs at least once.

Example:

  

from {         Loop Sentences
  } while (condition)

We could implement do... while using a while loop. In this case, the part of the code that would be executed at least once would come above while , following the logical sequence structure of the code.

Are there situations where we can only use do... while ?

What is the usefulness and importance of do... while ?

    
asked by anonymous 06.08.2015 / 15:22

3 answers

15

In no situation do we really need do ... while except to make code more elegant and better express the intention that block execution should occur at least once before deciding whether it will repeat itself or not.

It is always possible to do the same only with while . Of course, the code may have to be a little bigger and less elegant, having to do something artificial to make sure the first execution of the condition is true.

This may not always be so easy to do because it may involve checking for something that has side effects, in some cases even having something similar to Schrodinger's Cat paradox , where to access a value, you change the value. It can give different results depending on the moment. You can solve this, but you have to know how to do it, remember to do it, and increase the code. In general the solution is a flag or a cached value, which can be complex when access changes the value (although this should not happen in good codes).

Of course it can help you not to make mistakes too. You may be wrong to make sure that the first pass gives true and in some cases out of the ordinary give false, or you can change something in the code that ends up changing the true guarantee situation without the programmer noticing.

It gives more legibility and maintainability as any construction that helps give more semantics to the purpose.

    
06.08.2015 / 15:32
4
  

Since do ... while has verification at the end, ie the   Repeat structure is executed at least once.

You yourself answered. The ... while statement exists when we want to execute the code that is inside the repeat structure at least once without passing the logical test. There are situations where this is useful, although not so often.

  

We could implement do..while using a while loop. In this case,   the part of the code that would be executed, at least once, would come up   of while, following the logical sequence structure of the code.

Yes, it would be a way to have the do ... while functionality without using it. However, note that there will be code repetition. If the piece of code that is inside the loop is large, then you will have a significant repetition of code, which obviously is not good.

    
06.08.2015 / 15:35
3

It all depends on your logic of doing things. You can take a look at the solution I put here: For loop problem

In this situation he used the same input variable for the condition of while , his only output without using do... while was to put an arbitrary value that did not fall into the loop the first time or to create a flag .

Again, it all depends on how you program.

    
06.08.2015 / 15:33