Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Okay silly question I have done the Software design courses where the mantra is do..while executes at least once, while() may never execute.
I have written an App thats has just started working for some reason after a morning of not working it has sprung into life(!) it started to work yesterday after fiddling with a while loop that had three bools in that were 'or'ed
C#
 if (Tolerance3 == false)
{
    DUT3 = Convert.ToInt16(txtDUTVal3.Text);
    Gold3 = Convert.ToInt16(txtGold3.Text);
    if (DUT3 < Gold3)
    {
        IncreaseDUT3();
    }
    if (DUT3 > Gold3)
    {
        DecreaseDUT3();
    }                  
    if ((!(DUT3 < GoldTol3Min)&&!(DUT3>GoldTol3Max)))
    {
        Tolerance3 = true;
        MessageBox.Show("Tolerance 3");
    }
    if (Tolerance3 == true)
    {
        MessageBox.Show("Tolerance3 = true");
        FileName = INIPath + "\\MagZero.txt";
        if (!File.Exists(FileName))
        {
            StreamWriter sw = new StreamWriter(FileName);
            sw.WriteLine("Mag Zero " 
                         + dt.ToString("M_d_yyyy_HH:mm") 
                         + "\n\t Sensor3: " 
                         + txtDUTVal3.Text 
                         + " PWM Value: " 
                         + txtPWMvalue3.Text 
                         + "\n");
            sw.Close();
        }
        else
        {
            StreamWriter sw = File.AppendText(FileName);
            sw.WriteLine("\n" 
                         + "\n\t Sensor3: " 
                         + txtDUTVal3.Text 
                         + " PWM Value: " 
                         + txtPWMvalue3.Text 
                         + "\n");
            sw.Close();
        }
    }
}

wasn't executing now it has started. WHY?, WHY? I can't ship code that won't reliably work. The value can be seen to change but not always evaluated while the first two versions of this work like a charm I am wondering if C# gets an interrupt jumps to the service routine and then returns sees one of the coditions of the while met a leaves it, or gets another interrupt and ignors the rest of the while loop conditions

Glenn
Posted
Updated 30-Sep-10 1:39am
v2
Comments
ARopo 30-Sep-10 7:31am    
Have you posted the correct code? I can't see any while loops here

A while() loop will not run unless the codition is true. A do...while() loop will always run at least once.

The following will run at least one time

bool x = true;
while (x)
{
    // do something that could possibly change x to false
}

The following while loop will not execute because the condition is false.

bool x = false;
while (x)
{
    // do something that could possibly change x to false
}


The following will always execute at least once

bool x = true;
do
{
    // do something that may change the value of x
} while (x);

That's the primary difference between the two.

You can also use break or continue inside those loops to short-circuit code to either exit the loop or start a new iteration.

Lastly, you can do this:

while (true)
{
}


...or this:

do
{
} while (true);

It creates a loop that keeps going until it encounters a break statment.
 
Share this answer
 
Example :

int x = 6;

while (x <= 5)
{
Console.WriteLine("while "+x);
x++;
}

x = 6;

do{

Console.WriteLine("Do While "+x);
x++;
}while(x<=5);

see the output of this code, then you will understand the difference.
 
Share this answer
 
Comments
glennPattonWork3 30-Sep-10 8:09am    
Yeah, I know that what I'm wondering is if you have multiple conditions in your while not just one are they evaluated seperatly.

Glenn
First, change this:

if ((!(DUT3 < GoldTol3Min)&&!(DUT3>GoldTol3Max)))    
{
    Tolerance3 = true;
    MessageBox.Show("Tolerance 3");
}

to this:

if (DUT3 >= GoldTol3Min && DUT3 <= GoldTol3Max)
{
    Tolerance3 = true;
    MessageBox.Show("Tolerance 3");    
}


Second, there's no wy we can tell you why your code just starts to work on its own. We don't knwo ANYTHING about the rest of the code.
 
Share this answer
 
Comments
glennPattonWork3 30-Sep-10 7:55am    
if C# gets an interrupt jumps to the service routine and then returns sees one of the coditions of the while met a leaves it, or gets another interrupt and ignors the rest of the while loop conditions, that is really what I'm wondering if the while() is evaluated seperatly! a=b & a=c is it (a=b), true/false and the (a=c) can it be interrupted or is it treated as a whole?
glennPattonWork3 30-Sep-10 9:06am    
Well I have tried the above method >= & <= with no luck, since that change DUT3 has started not behaving as well.
#realJSOP 1-Oct-10 14:16pm    
Then you have bigger problems. BTW, I woudl use Int32.TryParse() if you're converting from a string to an int. That way, you could head-off problemns with invalid characters in the string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900