我在C 初学者中发现了无限的循环问题

I got infinite loop issues in c++ beginner

本文关键字:无限 循环 问题 发现 初学者 我在      更新时间:2023-10-16

使用编程很新,想知道您是否可以提供帮助。用我的代码获得了无限循环

int main()
{
int age, finalMark;
cout << "enter age: ";
cin >> age;
cout << "enter mark: ";
cin >> finalMark;
while (age != 0)
{
    if(age < 30 && finalMark > 65)
    cout << "You are the an ideal candidate" << endl;
    else
        cout << "You are not the ideal candidate. Goodbye" << endl;
}
return 0;
}

任何帮助都会很感激,很抱歉,如果它非常基本/易于解决

使用循环时,请确保某个时候条件不正确,否则,您最终会出现无尽的环路。

在这里,如果年龄的价值最初与0不同,那么您将永远不会脱离循环,因为您不会在循环中任何地方更改它。

while (age != 0)
{
   if(age < 30 && finalMark > 65)
        cout << "You are the an ideal candidate" << endl;
    else
        cout << "You are not the ideal candidate. Goodbye" << endl;
}

如果您想简单地检查条件,并且只能根据其结果进行操作,请使用" If"语句:

if (age != 0)
{
   if(age < 30 && finalMark > 65)
       cout << "You are the an ideal candidate" << endl;
   else
       cout << "You are not the ideal candidate. Goodbye" << endl;
}