本文介绍了while()&中是否有区别? ... while()循环吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,愚蠢的问题,我完成了软件设计课程,其中的口头禅是..while至少执行一次,而while()可能永远不会执行.
我写了一个应用程序,该应用程序在一个早上不工作后因某种原因才开始工作,它突然出现在生活中(!),它在经过一会儿循环后发现昨天是或"的三个循环之后才开始工作. ed

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

 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();
        }
    }
}


现在没有执行,它已经开始了.为什么为什么?我无法发布无法可靠工作的代码.可以看到该值发生了变化,但并非总是会评估该值,而此工作的前两个版本就像一个咒语一样,我想知道C#是否遇到中断,然后跳转到服务例程,然后返回,则看到while的条件之一遇到了叶子. ,或获取另一个中断并触发while循环条件的其余部分

Glenn


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

推荐答案

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


由于条件为假,因此以下while循环将不会执行.


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);


那是两者之间的主要区别.

您还可以在这些循环内使用breakcontinue来使代码短路,以退出循环或开始新的迭代.

最后,您可以执行以下操作:


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);


它创建一个循环,直到遇到break语句为止.


It creates a loop that keeps going until it encounters a break statment.



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.


这篇关于while()&amp;中是否有区别? ... while()循环吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 19:41