gpt4 book ai didi

c++ - while循环在if语句里面有相同的条件吗?

转载 作者:行者123 更新时间:2023-12-01 14:40:00 25 4
gpt4 key购买 nike

一个非常有经验的程序员写了这样的东西:

#include <string>
using namespace std;

class Fraction
{
float nominator;
float denominator;

void load()
{
cin>>nominator; cin>>denominator;
if(denominator==0)
{
while(denominator==0)
{
cout<<"denominator can not be equal 0!"<<endl;
cin>>denominator;
}
}
}
};

我不知道为什么会有一个if语句。真的有必要吗?

最佳答案

在此特定示例中,

while(denominator==0)
{
cout<<"denominator can not be equal 0!"<<endl;
cin>>denominator;
}

完全相同。

在您提供的上下文中,没有任何东西可以告诉我们为什么有人将循环嵌套在无用的 if中,但是有人可以提出解释。在该代码的早期版本中, if块中可能存在某些更改程序/功能行为的代码。

这也可能是一个无辜的错误。

Stroustrup的猫可能已经在键盘上走路。

关于c++ - while循环在if语句里面有相同的条件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59721520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com