增加 for 循环计数器的值

发布于 2024-12-11 04:05:14 字数 155 浏览 0 评论 0原文

是否有任何编程语言/脚本不允许增加循环内 FOR 循环的计数器?

例如:

for(int i = 0; i < 10; i++) 
{
    i++;
    print i
}

Output: 1 3 5 7 8 9

Is there any programming language/script who won't allow increasing the counter for an FOR loop inside the loop?

For example:

for(int i = 0; i < 10; i++) 
{
    i++;
    print i
}

Output: 1 3 5 7 8 9

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

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

发布评论

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

评论(6

小矜持 2024-12-18 04:05:14

不确定我完全理解你的问题,但是,

在 python 中:

for i in range(10):
    print i
    i+=1  #if I print i now I will get i+1, 

你将返回与以下相同的结果:

for i in range(10):
    print i

即:

0 1 2 3 4 5 6 7 8 9

Not sure I fully understand your question but,

in python :

for i in range(10):
    print i
    i+=1  #if I print i now I will get i+1, 

You will return the same result as :

for i in range(10):
    print i

i.e :

0 1 2 3 4 5 6 7 8 9

醉梦枕江山 2024-12-18 04:05:14

Pascal(举一个例子)基本上说,更改循环中的变量会产生未定义的行为(Pascal 用户手册,§C.3):“控制变量、初始值和最终值必须具有相同的标量类型(不包括类型 real),并且不能被 for 语句更改。”

IIRC,Ada 确实阻止您进行此类修改。看看圣经似乎证实了我彻底虚弱的记忆(Ada 95 RM,§5.5(10)):“循环参数是一个常量;它不能在循环的sequence_of_statements内更新”。

Pascal (for one example) basically said changing the variable in the loop gave undefined behavior (Pascal User Manual, §C.3): "The control variable, the initial value, and the final value must be of the same scalar type (excluding type real), and must not be altered by the for statement."

IIRC, Ada does prevent you from making such modifications. Looking at the bible seems to confirm my thoroughly infirm memory (Ada 95 RM, §5.5(10)): "A loop parameter is a constant; it cannot be updated within the sequence_of_statements of the loop".

ㄖ落Θ余辉 2024-12-18 04:05:14

PL/SQL 不允许修改循环变量:

SQL> begin
  2    for i in 1..10 loop
  3      i := i + 1;
  4    end loop;
  5  end;
  6  /
    i := i + 1;
    *
ERROR at line 3:
ORA-06550: line 3, column 5:
PLS-00363: expression 'I' cannot be used as an assignment target
ORA-06550: line 3, column 5:
PL/SQL: Statement ignored

PL/SQL doesn't let you modify loop variables:

SQL> begin
  2    for i in 1..10 loop
  3      i := i + 1;
  4    end loop;
  5  end;
  6  /
    i := i + 1;
    *
ERROR at line 3:
ORA-06550: line 3, column 5:
PLS-00363: expression 'I' cannot be used as an assignment target
ORA-06550: line 3, column 5:
PL/SQL: Statement ignored
天荒地未老 2024-12-18 04:05:14

我不知道有任何语言明确禁止它。但对于其后果有不同的概念。

  1. 变量,如 C 中的变量

    在 C 中,这个循环导致有效地生成 2 步,这是显而易见的。

  2. 迭代器,就像 Python 中的那样

    在 Python 中,这样的循环由迭代器提供,迭代器不断地产生独立于变量的新值。

    对于范围 (0, 100) 内的 i:
       打印“A:”,我
       我 += 1
       打印“B:”,我
    

    导致

    <前><代码>A:0
    乙:1
    答:1
    乙:2
    ....

    当你的 C 循环提供正确的输出时,会导致

    <前><代码>A:0
    乙:1
    答:2
    乙:3
    ....

I don't know of any language explicitly forbidding it. But there are different concepts of what consequences it has.

  1. Variable, like in C

    In C, this loop leads to effectively making steps of 2, as obvious.

  2. Iterator, like in Python

    In Python, a loop like this gets fed by an iterator which constantly yields new values independent of the variable.

    for i in range(0, 100):
       print "A:", i
       i += 1
       print "B:", i
    

    leads to

    A: 0
    B: 1
    A: 1
    B: 2
    ....
    

    while your loop in C, provided with the correct outputs, would lead to

    A: 0
    B: 1
    A: 2
    B: 3
    ....
    
困倦 2024-12-18 04:05:14

我认为 C++11 基于范围的 for 循环允许您阻止它:

for (const int i : some_range) {
    ++i; // forbidden
}

但我不认为该标准提供了一种简单的方法来定义整数范围,如正常的 for 循环。 some_range 可以是一个容器,或者一个 boost::counting_range

您是否认为这是“FOR 循环的计数器”取决于您。我想一个“正确的”示例是一个循环,它采用任意表达式来修改循环之间的计数器(示例中的 i++ ),但不允许在其他循环中修改计数器比那个表达。

I think C++11 range-based for loops allow you to prevent it:

for (const int i : some_range) {
    ++i; // forbidden
}

But I don't think the standard provides a simple way to define an integer range as in the normal for loop. some_range could be a container, or a boost::counting_range.

Whether you consider this "the counter for a FOR loop" is up to you. I suppose a "proper" example would be a loop that takes an arbitrary expression to modify the counter between loops (i++ in your example), but doesn't allow the counter to be modified in the loop other than by that expression.

梅窗月明清似水 2024-12-18 04:05:14

据我所知没有。但是,在 C# 中,您可以为每个执行 a 操作,如果集合发生更改,它将引发异常。

int[] myArray = new int[100];
foreach (var item in myArray)
{

}

Not that I know of. However, in C# you can do a for each and it will throw an exception if the collection is changed.

int[] myArray = new int[100];
foreach (var item in myArray)
{

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