有条件地改变“for循环”的方向?

发布于 2024-10-03 01:18:42 字数 198 浏览 0 评论 0原文

是否可以有条件地更改 ActionScript 中 for 循环的方向?

示例:

for(if(condition){var x = 0; x<number; x++}else{var x=number; x>0; x--}){
  //do something
}

Is it possible to conditionally change the direction of a for loop in ActionScript?

Example:

for(if(condition){var x = 0; x<number; x++}else{var x=number; x>0; x--}){
  //do something
}

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

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

发布评论

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

评论(3

郁金香雨 2024-10-10 01:18:42

有趣的要求。保留 for 的一种方法是:

var start, loop_cond, inc; 
if(condition) 
{ 
    start = 0; 
    inc = 1; 
    loop_cond = function(){return x < number}; 
} 
else 
{ 
    start = number - 1; 
    inc = -1; 
    loop_cond = function(){return x >= 0}; 
} 
for(var x = start; loop_cond(); x += inc) 
{ 
    // do something
}

我们设置起始值、终止条件的函数以及正增量或负增量。然后,我们只需调用该函数并使用 += 来进行递增或递减。

Interesting requirement. One way to keep the for is:

var start, loop_cond, inc; 
if(condition) 
{ 
    start = 0; 
    inc = 1; 
    loop_cond = function(){return x < number}; 
} 
else 
{ 
    start = number - 1; 
    inc = -1; 
    loop_cond = function(){return x >= 0}; 
} 
for(var x = start; loop_cond(); x += inc) 
{ 
    // do something
}

We setup the start value, a function for the termination condition, and either a positive or negative increment. Then, we just call the function and use += to the do the increment or decrement.

友欢 2024-10-10 01:18:42

ActionScript 有三元运算符,因此您可以执行以下操作:

for (var x = cond ? 0 : number; cond ? x < number : x > 0; cond ? x++ : x--) {
}

但这非常难看。 :-)

您可能还需要/想要在其中的各个部分周围放置一些括号。我不确定运算符的优先级。

您还可以考虑使用高阶函数。想象一下你有:

function forward (count, func) {
    for (var x = 0; x < count; x++) {
        func(x);
    }
}

function backward (count, func) {
    for (var x = count - 1; x >= 0; x--) {
        func(x);
    }
}

那么你可以这样做:

(condition ? forward : backward) (number, function (x) {
     // Your loop code goes here
})

ActionScript has the ternary operator, so you could do something like:

for (var x = cond ? 0 : number; cond ? x < number : x > 0; cond ? x++ : x--) {
}

But this is pretty ugly. :-)

You might also need/want to put some parens around pieces of that. I'm not sure about the operator precedence.

You might also consider using a higher order function. Imagine you have:

function forward (count, func) {
    for (var x = 0; x < count; x++) {
        func(x);
    }
}

function backward (count, func) {
    for (var x = count - 1; x >= 0; x--) {
        func(x);
    }
}

Then you could do:

(condition ? forward : backward) (number, function (x) {
     // Your loop code goes here
})
东京女 2024-10-10 01:18:42

您可能需要一个 while 循环:

var continueLooping, x;

if(condition)
{
  x = 0
  continueLooping = (x < number);
}
else
{
  x = number;
  continueLooping = (x > 0);
}

while (continueLooping)
{
  // do something
  if(condition)
  {
    x++;
    continueLooping = (x < number);
  }
  else
  {
    x--;
    continueLooping = (x > 0);
  }
}

如果您确实想要一个 for 循环,则应该使用其中两个:

function doSomething()
{
  //doSomething
}

if(condition)
{
  for(var x = 0; x<number; x++)
  {
    doSomething(x);
  }
}
else
{
  for(var x=number; x>0; x--})
  {
    doSomething(x);
  }
}

You probably want a while loop instead:

var continueLooping, x;

if(condition)
{
  x = 0
  continueLooping = (x < number);
}
else
{
  x = number;
  continueLooping = (x > 0);
}

while (continueLooping)
{
  // do something
  if(condition)
  {
    x++;
    continueLooping = (x < number);
  }
  else
  {
    x--;
    continueLooping = (x > 0);
  }
}

If you really want a for loop, you should use two of them:

function doSomething()
{
  //doSomething
}

if(condition)
{
  for(var x = 0; x<number; x++)
  {
    doSomething(x);
  }
}
else
{
  for(var x=number; x>0; x--})
  {
    doSomething(x);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文