Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于循环中的布尔标识符,我认为 我读过李国煌的一本书,C++初学者,其中一个代码片段是: while(true) { cout << description.c_str() << "\n\n"; int response = 0; do { cout << "What would you like to do?\n"; if(enemy) cout << "1) Attack the evil " << enemyName.c_str() << "\n"; else if(!enemy) cout << " 1) Move to the next room."; if(treasure) cout << " 2) Pick up the " << treasureName.c_str() << "\n"; cin >> response; }while(response < 1 || response > 2); switch(response) { case 1 : if(enemy) { enemy = !enemy; cout << "You slay the deadly " << enemyName.c_str() << "\n"; } else if(!enemy) return; break; case 2: treasure = !treasure; cout << "You pick up the " << treasureName.c_str() << "\n"; break; } }_C++_While Loop_Infinite Loop - Fatal编程技术网

关于循环中的布尔标识符,我认为 我读过李国煌的一本书,C++初学者,其中一个代码片段是: while(true) { cout << description.c_str() << "\n\n"; int response = 0; do { cout << "What would you like to do?\n"; if(enemy) cout << "1) Attack the evil " << enemyName.c_str() << "\n"; else if(!enemy) cout << " 1) Move to the next room."; if(treasure) cout << " 2) Pick up the " << treasureName.c_str() << "\n"; cin >> response; }while(response < 1 || response > 2); switch(response) { case 1 : if(enemy) { enemy = !enemy; cout << "You slay the deadly " << enemyName.c_str() << "\n"; } else if(!enemy) return; break; case 2: treasure = !treasure; cout << "You pick up the " << treasureName.c_str() << "\n"; break; } }

关于循环中的布尔标识符,我认为 我读过李国煌的一本书,C++初学者,其中一个代码片段是: while(true) { cout << description.c_str() << "\n\n"; int response = 0; do { cout << "What would you like to do?\n"; if(enemy) cout << "1) Attack the evil " << enemyName.c_str() << "\n"; else if(!enemy) cout << " 1) Move to the next room."; if(treasure) cout << " 2) Pick up the " << treasureName.c_str() << "\n"; cin >> response; }while(response < 1 || response > 2); switch(response) { case 1 : if(enemy) { enemy = !enemy; cout << "You slay the deadly " << enemyName.c_str() << "\n"; } else if(!enemy) return; break; case 2: treasure = !treasure; cout << "You pick up the " << treasureName.c_str() << "\n"; break; } },c++,while-loop,infinite-loop,C++,While Loop,Infinite Loop,我想你可以忽略这个计划的意图,但问题是,为什么真正存在的部分?我认为,没有办法摆脱这种循环,对吗?因为,我认为真值总是返回1,whiletrue部分与whiletrue==1相同,所以这个循环就像无限循环,我错了还是错了?感谢您的帮助。这是值得回答的,因为它欺骗了至少一个5000名信誉用户,因此证明了编写清晰代码的重要性 return语句是循环终止符。这将退出该功能 它深深地埋藏在函数中。因此,我会批评代码的这种风格:很难遵循,也很难调试。是的: 这个循环就像无限循环 你是对的,whiletru

我想你可以忽略这个计划的意图,但问题是,为什么真正存在的部分?我认为,没有办法摆脱这种循环,对吗?因为,我认为真值总是返回1,whiletrue部分与whiletrue==1相同,所以这个循环就像无限循环,我错了还是错了?感谢您的帮助。

这是值得回答的,因为它欺骗了至少一个5000名信誉用户,因此证明了编写清晰代码的重要性

return语句是循环终止符。这将退出该功能

它深深地埋藏在函数中。因此,我会批评代码的这种风格:很难遵循,也很难调试。

是的:

这个循环就像无限循环

你是对的,whiletrue是一条永远循环的指令

但是,还有其他几种退出循环的方法:

return语句将退出函数,并因此终止循环 break语句将退出最接近的for、while或switch语句。 goto语句可能会导致代码跳转到循环外的标签 退出调用将导致整个程序终止。 throw语句将抛出一个异常,该异常将跳转到最近的合适catch语句,该语句可能位于循环之外。 在这种情况下,返回;接近循环底部会导致函数退出

休息;语句不会导致循环停止,因为它们属于开关

<> Po>使用GOTO程序的程序员认为它的风格很差,因为它会导致难以遵循的代码。关于这个问题还有很多进一步的讨论。在C++中,通常抛出对于Goto可能被使用的情况更合适。p> 然而,在纯C中,有些情况下goto非常有用。提供了一个很好的概述,说明了为什么goto在历史上被认为是糟糕的风格,甚至还提供了一些使用goto可能合适的示例


当然,对于初学者来说,一个好的规则可能是假装goto不存在。特别是在C++中,

如果仔细查看,就会有< /P>
return;
代码中的语句。这将退出循环和循环所在的函数。

whiletrue{…}的唯一出口是return语句,它终止周围的函数

我不知道这本书,但我从来没有见过另一个建议,用这种多余的方式写if语句:

if(enemy)
{ ...
}
else if(!enemy)
{...
}
冗余通常是要避免的,因为它使维护更加困难

我非常不喜欢:

    case 2:   treasure = !treasure;
              cout << "You pick up the "
                   << treasureName.c_str() << "\n";

这将允许您拾取宝藏,但您可以留在循环中,再次选择“2”,告诉您再次拾取x,但再次否定变量宝藏。嗯,希望这不是书中的完整引用

下面有一条中断指令,即循环出口。或者,即使返回也可以从函数退出。这个问题似乎与主题无关,因为它与不查找基本语言关键字有关,例如break和return。break会中断开关,而不是循环。但是你是对的,返回就是离开循环的内容。@DavidKernin,但我认为break只适用于切换条件,而不是while循环。为什么不先查找所有语言关键字呢。StackOverflow不是一个提问的好地方,这些问题由介绍性书籍回答。+1但是,考虑到这个答案的冗长性,请注意不要过度使用goto?我现在明白了,先生。作为一个初学者,我意识到在这本书中有一些我觉得特别的程序,但我自己也不确定。重要的是我从你和这里的所有人那里得到了知识。以防我的英语不太好。