for循环,循环比它必须多一次(for loop , loops one more time than it have to)

编程入门 行业动态 更新时间:2024-10-28 07:18:42
for循环,循环比它必须多一次(for loop , loops one more time than it have to)

我正在研究一个简单的短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.

更多推荐

本文发布于:2023-07-30 11:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1337901.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:loop   loops   time

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!