将复合语句放在for循环的条件中 [英] Putting a composite statement in the condition of a for loop

查看:283
本文介绍了将复合语句放在for循环的条件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个人为的例子来演示一个特定功能的请求 - 我想知道是否有人有一个聪明的技巧来做到这一点。



以下是一个问题遇到相当频繁:

打印一系列数字;在它们之间打印一个空格,最后输入一个回车符(但是没有空格)。 b
$ b

显而易见的解决方案是使最后一个(或第一个)语句成为一个特例。我正在思考如何使这个更高效/紧凑。
$ b $对于(ii = 0; ii <4; ii ++)而言,强力: {
printf(%d,ii);
if(ii <3)printf(); else printf(\\\
);
}

请注意,这涉及一个条件的两个评估。

展开:

  for(ii = 0; ii <3; ii ++){
printf(%d,ii):
}
printf(%d\\\
,ii);

利用事实上,当我们离开循环时, ii 将会最后一次递增。



功能我想

  ii = 0; 
while(1){
printf(%d,ii);
ii ++;
if(ii <3)printf();
else {printf(\\\
); break;}
}

我想知道是否可以在用于语句。我修了一下,发现下面的工作(有点令我惊讶......它确实需要括号,而且它在?: operators - 参见 http://codepad.org/wFa2YwCg ) :(ii)(ii)(ii <3)?(printf(%d,ii),1):

pre $ :(输出( %d\\\
,ⅱ),0);ⅱ++);

我基本上将评估这个条件 for循环的一部分变成了一个在大部分循环中执行这个语句,而这另一个用于最后一个循环语句。



我想知道是否有更好的方法来做到这一点 - 既有效率又可读?

许多方式应该关闭这个问题,因为它是基于意见的。]



这个问题经常出现。我总是选择一个解决方案来最小化迭代部分的指令。

  {/ *不要污染外部范围ii * / 
int ii; (ii = 0; ii <3; ++ ii / *)的
,我总是喜欢这个到ii ++ * /){
printf(%d,ii);
}
printf(%d \\\
,ii);

$ / code>

三元, if 声明只是混淆的东西。在我看来。


I have a contrived example to demonstrate the request for a specific functionality - I wonder if anyone has a clever trick to do this.

The following is a problem one encounters quite frequently:

"Print a series of numbers; print a space between them, and a carriage return (but no space) at the end.

The obvious solution is to make the last (or first) statement a special case. I was pondering ways to make this more efficient/compact.

brute force:

for(ii=0; ii<4; ii++) {
  printf("%d", ii);
  if(ii<3) printf(" "); else printf("\n");
}

Note that this involves two evaluations of a condition.

unrolling:

for(ii=0; ii<3; ii++) {
  printf("%d ", ii):
}
printf("%d\n", ii);

Leverages the fact that ii will be incremented one last time as we leave the loop.

functionality I would like

ii = 0;
while(1) {
  printf("%d", ii);
  ii++;
  if(ii<3) printf(" "); 
  else {printf("\n"); break;}
}

And I was wondering if it's possible to make this work inside the for statement. I tinkered a bit, and found that the following works (somewhat to my surprise... it did need the parentheses, and it quite unreadable between the ?: and the , operators - see http://codepad.org/wFa2YwCg):

for(ii=0; (ii<3)?(printf("%d ",ii),1):(printf("%d\n",ii),0);ii++);

I am essentially turning the evaluate this condition part of the for loop into a execute this statement for most of the loop, and this other one for the last pass statement.

I was wondering if there is a better way to do this - both efficient, and readable?

解决方案

[In many ways this question should be closed as it's opinion based.]

This problem crops up often. I always opt for a solution that minimises the instructions in the iterative part.

{ /*don't pollute the outer scope with ii*/
    int ii;
    for (ii = 0; ii < 3; ++ii/*I've always preferred this to ii++*/) {
        printf("%d ", ii);
    }
    printf("%d\n", ii);
}

Ternaries, if statements etc. just obfuscate things. In my opinion.

这篇关于将复合语句放在for循环的条件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆