for循环,循环一次比它有 [英] for loop , loops one more time than it have to

查看:154
本文介绍了for循环,循环一次比它有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的短C ++代码,并且for循环循环一个比它(使用代码::块开发):

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;
}

输出是20,但据我所知,它必须是19

The out put is 20 but as far as I know it has to be 19.

图片链接: 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.

这篇关于for循环,循环一次比它有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆