how Exactly Do while works

hello.

i have the following example and do while loops ends before coroutine ends, what i mean that the log “here” will be printed before “over”, what i missing up?

public class Test : MonoBehaviour
{

    private bool _ready;

    void Awake()
    {
        StartCoroutine(WaitAndReady());
        DoSomething();

    }


    private void DoSomething()
    {
        do{
            if(_ready)
            {
                break;
            }
        }
        while(false);

        Debug.Log("here");
    }

    private IEnumerator WaitAndReady()
    {

        yield return new WaitForSeconds(5);
        Debug.Log("over");
        _ready = true;
    }
}

Coroutine does not guarantee order of execution.
Why you need it anyway? What is your usage case?

Most cases can be resolved without it. You only run yourself into debugging issues.

i’m trying to understand do while loop better, because it will break the loop before the bool will be true, what i missing up?

It will break, because while is false.
You would otherwise use while (true) but is very unwise, as you lock main thread and you can crash app.

do…while loop just repeats instructions inside while condition is true. For this particular case you should use something like this

do
{
   Debug.Log("Not ready yet...")
}
while(!_ready)

In normal program, this would log until _ready becomes true. Note this will lock main thread if called from it.

1 Like

In thhis case is not an infinite loop? Then will freeze unity?

In your first post is normal.

Also you’re using while(false) → will exit. But it will break before executing while(false).

If you want to execute the while loop for 5 seconds better use threads.

Or just use this Unity - Scripting API: WaitWhile

One solution is to never use a do-while loop. WHILE and FOR loops are the basic ones, and are fine for everything. do-while loops were supposed to be shortcuts for one trick, but are more trouble than they’re worth – whenever you see a do-while loop, there’s a better way to write it as a while loop.

2 Likes

Agree with @Owen-Reynolds . The use-cases for do-while are there, but they are few and far between.

I don’t know what you think do-while is supposed to do, @Sylon87 – a do-while is just a while loop where the block comes before the condition check. There’s no circumstances in which your original snippet would print “here” before “over”, and I’m curious to understand why you think there might be one.

the first example was a my mistake, because i put false instead of true on the loop, what i missed up was that it will freeze the thread, so i have to change the bool inside the loop and i can’t use do while loop to wait a bool that become true… that’s all.

so to wait i should use a coroutine or await, but both are going to stop the main thread



Gives you an idea of while vs. do while. Sorry lads, had to do it to em lol.
You could also use Invoke(“method name”, interval) or InvokeRepeating(“method name”, start interval, repeating rate)

4 Likes

Coroutines don’t freeze the main thread. If you want a loop to continue in a coroutine until some condition is met, put a yield return null inside the loop, it will check the condition every frame until it is true.

// inside a coroutine

while( !someCondition ) {
  yield return null;
}

await doesn’t block the thread either.

But they do. Coroutines runs on the main thread and if you write such an infinite loop without yield instruction inside, the main thread will be blocked. Coroutines only allows to slice amount of work into few frames.

Coroutines only freeze the main thread in the sense that any infinite loop on the main thread will freeze the main thread.

They don’t hold a thread in the Java sense, whereby a thread is stalled while work is performed.

I was specifically responding to his comment where he was asserting that using a coroutine or await would “stop the main thread”. This is false. Misusing a coroutine holds the possibility of freezing unity, but we’re talking about uses, not misuses.

I completely agree with the sentiment that DO-WHILE loops should only be used when you really must, and can always be replaced by a more readable alternative.

Personally, I cringe every time I see that construct - but for other reasons most people think: it’s a jarring and completely unneccessary misuse of semantics. Obviously, the difference between WHILE-DO and DO-WHILE loops is that in the former, the block of statements is executed zero to n times, while in the latter it is executed at least once.
In the bad old days, when we wrote our own scanners/parsers instead of using frameworks, there was the recurring need to read a character (or value) at least once, process it, and then decide to quit or loop – a technique so common, it had its own name: “pre-read one”. It was so common that people felt it warranted its own construct and thus we got DO-WHILE, in the typical unvarnished Kernighan/Ritchie style that feels wrong for most because of the inverted syntactical use of ‘while’ (english spoken language semantics apply a while clause before execution even if it was added syntactically at the end of the sentence: “Feed me while I’m hungry” will not cause you to feed me at least once). Contrast that with Wirth’s much more elegant ‘REPEAT-UNTIL’ in Pascal that essentially does the same, but reads much more naturally. Neither of them is required for programming, and is ideed syntactical sugar, so best to avoid entirely in C by explicitly invoking and then looping with WHILE-DO.

So why am I telling you this? Because I’m waiting for my godson, and am bored :slight_smile:

Hmmm… repeat-until is about flipping the condition. do {} while(n<wanted) or repeat {} until(n>=wanted). The idea was “when the loop stops, this will be true” was more natural. But it turned out not to matter, and having “run on true” and “run on false” loops side-by-side was confusing.

There’s also an UNTIL loop (while and do-while, until and repeat-until). COBOL had the old file loops (something like PERFORM READ, and you just had to know it looped over each line). We had for i=10 to 2 step -2 loops that could only use number sequences (FOR loops replaced them).

Basically, computer languages went through a phase where having as many redundant commands as possible seemed like a good idea. But after using them, we realized it was just confusing and cut them all out but one – the do-while loop.

Ideed. Last time I checked, though, current COBOL still has 521 (‘+’ through ‘ZEROES’) reserved words (C has 32, Pascal 54 and c# has 79), so while we definitely saw some cleanup of computer languages, it was not universal. I believe I once attended an (extremely challenging) class at university that proved you can create a mathematically solution-complete language with just 8-10 reserved words (and only one loop: the WHILE-Loop), but no-one in their right mind would want to use a language like that, so people add sugar. To quote MS on the question ‘what is syntactical sugar’ they responded ‘Byte is syntactical sugar for Bits, and Assembly Language is sytactical sugar for Machine Language’. Since generally speaking, all compilers are glorified macro assemblers, the only question is where to draw the line. Good languages made/make smart decisions here, COBOL didn’t. :slight_smile:

I didn’t know C had for-to-step loops. I thought they were (and still are) part of Pascal (I know ‘step’ was removed from Modula-2, FOR was entirely removed from Oberon, but returned to Oberon-2).