C++:for 循环的范围?

发布于 2024-09-18 04:56:25 字数 342 浏览 3 评论 0原文

#include <iostream>
using namespace std;

int main() {
    int i;
    for(i=0; i <= 11; i+=3)
        cout << i;
    cout << endl << i << endl;
}

输出为:0 3 6 和 9,一旦退出循环,输出为 12。 循环内和循环外 i 的地址看起来相同

我需要知道的是: for 循环内的 i 与 for 循环外初始化的 i 是否相同,因为变量 i 在 for 循环 i 之前首先初始化曾经被创造过吗?

#include <iostream>
using namespace std;

int main() {
    int i;
    for(i=0; i <= 11; i+=3)
        cout << i;
    cout << endl << i << endl;
}

output is: 0 3 6 and 9 and then once it exits the loop its 12.
The addresses of i inside the loop and out appear the same

What I need to know is: Is the i inside the for loop the same as the i that was initialized outside the for loop because the variable i was first initialized before the for loops i was ever created?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

很糊涂小朋友 2024-09-25 04:56:25

是的,循环内的 i 与循环外的 i 相同,因为您只声明了一次。

如果出于某种原因你希望它不同(我强烈建议你不要这样做,你应该为不同的变量选择不同的名称),你可以在 for 循环中重新声明 i :

for (int i = 0; i ...

Yes, the i inside the loop is the same as the i outside of the loop because you've only declared it once.

If for some reason you want it to be different (which I highly recommend against, you should choose different names for different variables) you could redeclare the i in the for loop:

for (int i = 0; i ...
人间☆小暴躁 2024-09-25 04:56:25

这是相同的 'i' var

#include <iostream>
using namespace std;

int i = 0;

int main() {
    int i;
    for(i=0; i <= 11; i+=3)
        cout << i;
    cout << endl << i << endl;
    cout << endl << ::i << endl;
}

i is 12

::i is 0

It's de same 'i' var

#include <iostream>
using namespace std;

int i = 0;

int main() {
    int i;
    for(i=0; i <= 11; i+=3)
        cout << i;
    cout << endl << i << endl;
    cout << endl << ::i << endl;
}

i is 12

::i is 0

陪我终i 2024-09-25 04:56:25

为了在 C++(以及 C)中创建新对象(变量),您必须显式定义它。在您的程序中,您有且只有一个变量定义 - int i;。这立即意味着那里只有一个变量i。无论“for 循环的范围”或其他什么,都不会有任何其他 i 的机会。

In order to create a new object (variable) in C++ (as well as in C) you have to explicitly define it. In your program you have one and only one variable definition - int i;. That immediately means that there's one and only one variable i there. There's no chance for any other i, regardless of any "scopes of for loop" or anything else.

梦幻之岛 2024-09-25 04:56:25

只有一个“i”变量。您只是在 foo 循环中分配一个值。

There is only one 'i' variable. You're just assigning a value in the foor loop.

乱世争霸 2024-09-25 04:56:25

这里只有一个变量 - 是的,循环内的 i 与退出循环后输出的 i 相同。然而,该变量仅作为循环的一部分进行初始化,而不是之前。

There is only one variable extant here - and yes, the i inside the loop is the same as the one you output after exiting the loop. However the variable was only initialized as part of the loop, not before.

碍人泪离人颜 2024-09-25 04:56:25

形式的 for-loop

for (init condition; expression) statement

完全等同于:

{
    init
    while (condition)
    {
        statement
        expression;
    }
}

因此,对于您的代码:

    int i;
    {
        i=0;
        while (i <= 11)
        {
            cout << i;
            i += 3;
        }
    }
    cout << endl << i << endl;
}

您现在能告诉我吗? Andrey 说得最好:如果你没有定义它,它就会不存在。

A for-loop of the form:

for (init condition; expression) statement

Is exactly equivalent to:

{
    init
    while (condition)
    {
        statement
        expression;
    }
}

So with your code:

    int i;
    {
        i=0;
        while (i <= 11)
        {
            cout << i;
            i += 3;
        }
    }
    cout << endl << i << endl;
}

Can you tell now? Andrey puts it best: if you didn't define it, it doesn't exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文