c# - 这个 while 循环中发生了什么?

标签 c# while-loop

double number;
bool isParsed = false;

while(!isParsed)
{
    Console.WriteLine("Enter the value");
    isParsed = double.TryParse(Console.ReadLine(), out number);
    if(isParsed)
    {
        break;
    }
    Console.WriteLine("Invalid value");
}

我和一个 friend 正在研究这个代码块。我发现这部分可以理解:

bool isParsed = false;

while(!isParsed)

我认为如果 isParsed = false,while 循环将检查否定 (!isParsed) 以查看它是否应该运行,这不是逻辑吗:

while(!isParsed) => while(NOT(false)) => while (true)?

因此,while 循环永远不会运行。但它确实运行。后来我才知道是在检查:

while (!isParsed) => while((!isParsed) == true),

但他说实际情况并非如此。

有人可以解释这里发生了什么吗?

最佳答案

你说得对:while (true)。真正的 bool 条件表示下一次(和第一次)迭代将运行。

!false == true

查看说明 while 循环行为的 MSDN 文档:https://msdn.microsoft.com/en-us/library/2aeyhxcd.aspx

关于c# - 这个 while 循环中发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32418906/

相关文章:

c# - 如何在 Visual C# 中清除数组

c# - 是什么导致 Linq 错误 : This method cannot be translated into a store expression?

java - 在二十一点中提问时

javascript - while 循环中的 if else 语句,其中 if else 语句确定 while 循环何时停止

java - 摆脱多余的 while 语句

c# - 奇怪的报告查看器错误 : Could not load file or assembly 'App_WebReferences. e8ik1wuc

c# - 从静态方法访问类成员

C# 正则表达式匹配字符串中的多个单词

bash - "while read LINE do"和 grep 问题

c - 如何计算循环中的迭代次数