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

Asked

Viewed 1,786 times

21

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

Example:

while (condição) {
    sentenças a executar
}

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

Example:

do {
    sentenças do loop
} while (condição)

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

There are situations where we can only use do...while?

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

  • Please read this excellent reply: http://answall.com/a/81431

  • In Visual Basic, the Do...While is called Do...Until :)

3 answers

16


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

It is always possible to do the same with just the while. Of course the code may have to be a little larger and less elegant, having to do something artificial to ensure that the first execution of the condition is true.

This may not always be so easy to do because it may involve checking something you own side effects, in some cases until you have something similar to Schrödinger’s Cat Paradox, where to access a value, you change the value. You 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 become 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 mistaken to ensure that the first pass is true and in some unusual case it is false, or you may change something in the code that ends up changing the real warranty situation without the programmer noticing.

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

5

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

You answered it yourself. The do...while exists when we want to execute the code that is within the repeating structure at least once without going through the logical test. There are situations where this is useful, although not so often.

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

Yes, it would be a way to have the functionality of...while 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 significant code repetition, which is obviously not good.

4

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

In this situation he used the same input variable for the while, the only way out of it without using do... while was to place an arbitrary value that did not fall into the loop the first time or else create a flag.

Again, it all depends on how you program.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.