C# Exit a while loop for an if condition within the while loop
<p>I am trying to exit Dowhile loop, when the if condition is satisfied within the loop. The reason to use Dowhile loop is that tagIDNumber sometimes takes garbage value. Here is my intention of the code</p>

<ol>
<li>First time when the application is launched and the function TagID() is called, previoustagIDNumber will be empty string, tagIDNumber can be "NO TAG", actual value or "RESTART" application again.</li>
<li>When  tagIDNumber  gets the actual value for first time, it will be stored in previoustagIDNumber.</li>
<li>The dowhile loop is used to execute the function Identify(), until tagIDNumber  == previoustagIDNumber. </li>
</ol>

<p>But the tagIDNumber  can also be empty string, in that case do while loop has to exit.
The below code doesnt seem to work for exiting dowhile loop when the if condition is satisfied.</p>

<pre><code>public string TagID()
    {
        SetAttenuation(Attenuation);

        StringBuilder tagIDNumber = new StringBuilder(); // TagID 
        int TagType;

        TagType = Identify(tagIDNumber, SEGOTAGTYPE);

        // When the application is launched first time, previous Tag ID value is empty
        if (Globals.previoustagIDNumber != "")
        {
            if (tagIDNumber.ToString().ToUpper() != Globals.previoustagIDNumber)
            {
                do
                {
                    tagIDNumber.Clear();
                    TagType = Identify(tagIDNumber, SEGOTAGTYPE);

                    if (tagIDNumber.ToString()  == "")
                    {
                        return "NO TAG";
                        break;
                    }
                }
                while (tagIDNumber.ToString().ToUpper() != Globals.previoustagIDNumber);
            }
            return tagIDNumber.ToString().ToUpper();
        }
        else
        {
            if (TagType == SEGOTAGTYPE)
            {
                if (tagIDNumber.ToString() == "")
                {
                    return "NO TAG";
                }
                else
                {
                    return tagIDNumber.ToString().ToUpper();
                }
            }
            else
            {
                return "RESTART";
            }
        }           
    }
</code></pre>