c - python和c for循环的区别

标签 c python-3.x

为什么要执行以下操作 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/

相关文章:

c - 尝试用 C 语言运行程序

c - 鱿鱼 url_rewrite_program。标准输入始终为空。无法使用 wgets 解析鱿鱼消息

c - 如何确定 Linux TTY 是否正在控制进程组

Python - 遍历前一周的同一天

c - IsDouble函数原型(prototype)语法错误&警告(数据定义没有类型或存储类)

c - 让进程等待它的 'brothers' 进程

python - Linux 和 Windows 中的 .values() 有区别吗?

python - 在列表中排序列表

python - python - 如何在python中断言一个字典包含另一个没有assertDictContainsSubset的字典?

python - 通过避免 NaN 滚动系列