我正在研究一个简单的短C ++代码,并且for循环比它更多地循环(使用code :: blocks开发):
#include <iostream> using namespace std; int main() { int x = 0; for (x=10; x<20; x++); cout<<x; return 0; }输出是20,但据我所知它必须是19。
图片链接: https : //drive.google.com/file/d/0B9WsVzm6FTagbC1uNHpMZ1p6SW8/edit?usp=sharing
I'm working on a simple short C++ code and the for loop is looping one more than it have to (developed using code::blocks):
#include <iostream> using namespace std; int main() { int x = 0; for (x=10; x<20; x++); cout<<x; return 0; }The out put is 20 but as far as I know it has to be 19.
link for image : https://drive.google.com/file/d/0B9WsVzm6FTagbC1uNHpMZ1p6SW8/edit?usp=sharing
最满意答案
x < 20是为了保持在循环内必须满足的条件,这意味着当x >= 20时你只能退出循环,所以当你达到x = 19你仍然会再次迭代,因为19 < 20 。
x < 20 is the condition that must be met in order to stay inside the loop, which means that you'll only exit the loop when x >= 20, so when you reach x = 19 you'll still iterate one more time because 19 < 20.
更多推荐
发布评论