gpt4 book ai didi

c - python和c for循环的区别

转载 作者:行者123 更新时间:2023-11-30 21:27:13 25 4
gpt4 key购买 nike

为什么要执行以下操作 for循环有不同的结果:

Python:

for i in range(0,10):
i=i-1
print(i)

输出: -1 0 1 2 3 4 5 6 7 8

C:

for(int i=0;i<10;i++)
{
i=i-1;
print("%d",i);
}

输出: infinite times -1

谢谢。

最佳答案

 - In C we declare a variable with data type,initiate it and then run upto some value with increment.
Some how Python deals with the same procedure but here we have to run a loop in the range

Let me give you an example:
In c : Syntax

for ( init; condition; increment ) {
statement(s);
}

int i;
for(i=1;i<=n;i++)
  1. 这个循环将一直运行,直到 i 的值变为 n。

  2. 在 C++ 中,您还可以在循环内声明变量。在 Python 中: 语法:

        for iterating_var in sequence:
    statements(s)

    对于范围 [1,11] 内的 x

    • 此循环将一直运行,直到 x 的值变为 10。请注意,我们必须将上限范围设置为比执行循环的上限多 1

关于c - python和c for循环的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53166230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com