Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 最短while-one循环条件_C_Loops - Fatal编程技术网

C 最短while-one循环条件

C 最短while-one循环条件,c,loops,C,Loops,有时我需要goto语句来接近函数的末尾,为了避免感觉我在汇编程序中,并且为了有严格定义的块来逃避…我使用替代的,一次循环,并在其中中断,它看起来更好 do { ...regular code with break possibility } while (false); 所以,正如我所希望的那样,所有的事情都以这种方式很好地工作,但我想知道有没有更漂亮的方式来实现这一点,因为若代码更大,那个么在开始时你们就看不到循环的用途了。有什么好看的主意 比如:for(inti=1;i;i=0)

有时我需要goto语句来接近函数的末尾,为了避免感觉我在汇编程序中,并且为了有严格定义的块来逃避…我使用替代的,一次循环,并在其中中断,它看起来更好

do {
     ...regular code with break possibility
} while (false);
所以,正如我所希望的那样,所有的事情都以这种方式很好地工作,但我想知道有没有更漂亮的方式来实现这一点,因为若代码更大,那个么在开始时你们就看不到循环的用途了。有什么好看的主意

比如:
for(inti=1;i;i=0){…}
(一开始就很清楚,但从do-while看不太好看。或者可能
while(true){…;break}
。最好的方法是
while(something){…}
,其中某个东西最短,而且在某种程度上它的作用是显而易见的

现实世界的例子:

#define onceloop() for (int iIi=0;!iIi++;)
void somefunction()
{                                                       onceloop() {
   if ((s=socket(AF_INET, ...)<0) { printf("create error"); break; }
   if (bind(s, addrIp,    ...)<0) { printf("bind error");   break; }
   if (c=accept(s, ...)       <1) { printf("accept error"); break; }
   ...usefull code on indent1 ...                                  
                                                                   }
   printf("something important just before the end of function");
}
#为(int iIi=0;!iIi++;)定义onceloop()
void函数()
{onceloop(){

如果((s=socket(AF_INET,…)更新:

根据您更新的代码示例,我建议您使用
return
而不是
break
,并完全删除
onceloop()
。您还需要引入另一个函数:

void somefunction()
{  ...
   otherfunction();
   printf("something important just before the end of function");
}

void otherfunction()
{
   if ((s=socket(AF_INET, ...)<0) { printf("create error"); return; }
   if (bind(s, addrIp,    ...)<0) { printf("bind error");   return; }
   if (c=accept(s, ...)       <1) { printf("accept error"); return; }
   ...usefull code on indent1 ...                                  
}
void inner() {
     //...regular code with return possibility
}

void outer() {
    inner();
    printf("something more just before end;");
}
void somefunction()
{  ...
otherfunction();
printf(“函数结束前的重要事项”);
}
void otherfunction()
{

如果((s=socket(AF_INET,…)这样的
goto
用法可以用函数替换:

void somefunction()
{  ...
   otherfunction();
   printf("something important just before the end of function");
}

void otherfunction()
{
   if ((s=socket(AF_INET, ...)<0) { printf("create error"); return; }
   if (bind(s, addrIp,    ...)<0) { printf("bind error");   return; }
   if (c=accept(s, ...)       <1) { printf("accept error"); return; }
   ...usefull code on indent1 ...                                  
}
void inner() {
     //...regular code with return possibility
}

void outer() {
    inner();
    printf("something more just before end;");
}
但是最好在惯用的地方使用
goto
,它更漂亮、更短,并且不引入新的函数名

只是在C++类内部,它会产生更多的代码

<>我不明白为什么C++会有任何不同。但是你可以避免用lambda。
void outer() {
    [] {
        //...regular code with return possibility
    }();
    printf("something more just before end;");
}

尽管如此,您可能不一定要通过以下方式避免读者的负面评论:)

标准解决方案:

#define onceloop() for (int iIi=1;!iIi++;)
void somefunction()
{                                                       onceloop() {
   if ((s=socket(AF_INET, ...)<0) { printf("create error"); break; }
   if (bind(s, addrIp,    ...)<0) { printf("bind error");   break; }
   if (c=accept(s, ...)       <1) { printf("accept error"); break; }
   ...usefull code on indent1 ...                                  }
   printf("something just before the end of function");
}
#为(int iIi=1;!iIi++;)定义onceloop()
void函数()
{onceloop(){


if((s=socket(AF_INET,…)如果您只需要执行代码一次,请不要使用循环。老实说,这些对我来说都不好。在我看来,if else应该是一个
if else
而不是“一次循环”对我来说,这看起来有点奇怪…尝试用
或者
重构你的代码。如果你不能,那么也许可以展示更多的代码,这里的人可以提供帮助。你能给我们一个真实世界的例子,也许是你能找到的最短的代码,实际展示你的用例吗?“有时我需要goto语句来接近函数的末尾,为了避免读代码的人给出不好的注释,我使用了替代的,一次循环,并在其中中断,这样看起来更好。"一些模糊不清的
goto
怎么会比普通的
goto
好呢?@BaummitAugen确实如此。如果一个
goto是你想要的,就用一个goto。如果有人批评你的选择,请他们做得更好。如果他们不能做得更好,那就这样。如果他们能做得更好,就向他们学习。我不明白怎么做他回答了这个问题。虽然是真的,但这与他的问题完全无关,他的问题是如何以不那么丑陋的方式避免一个
goto
。@DavidSchwartz使用
if…else
很可能是“以不那么丑陋的方式避免一个goto”的解决方案。我解释了OP提出的解决方案不正确的原因。哦,你的意思是将所有剩余的代码包装在
if
中,而不是使用
break
?他的问题不是执行一次的代码,而是希望绕过的代码。@DavidSchwartz“如何在c中循环一次”我不知道你怎么能把这个标题理解为不是关于执行一次的代码。同时OP也想绕过一些代码。使用
while
循环是不正确的。OP应该使用
if…else
语句,可能不止一个,这取决于他们代码的具体细节。@MetNP我不认为这是错误的可能。对于100种不同的情况,没有一个正确的答案。如果它们都一样,那么选择最简单的一种,这样我们就知道你在说什么情况。如果它们都不同,你就不能期望得到一个答案。“我想要100种不同情况的一个正确答案,甚至不会给你一个例子,这样你就知道我在说什么了。”“代码> > Goto < /Cord>是习惯用法吗?@代码学徒<代码> Goto < /Cord>是表示无条件分支的惯用方式。是的,这是最好的,而且返回比中断要好,只是在C++类内部添加更多的代码,并且在0的时候做{}”。似乎更好。我已经有一段时间没有做过任何C编程了。我认为所有的控制流都可以用
while
if…else
来编写。当然,对于非常复杂的逻辑,它可能变得笨拙,就像不加选择地使用
goto
一样,而
是循环的惯用用法。
if。。。else
是条件分支的惯用用法。
break
continue
是无条件跳出循环/进入下一个迭代的惯用用法。在循环上下文之外,没有无条件向前跳转的控制结构。常规的
goto
仍然是跳转的惯用方法。is
&%
输入错误?不错,是的,错误变量是一种方法。注意,我的案例可以通过宏oncebreak(err){printf(err);break;}来简化,这就是我喜欢的,简化了未使用的(与错误相关的)尽可能地编写代码,并将重点放在正确的部分。@MetNP请不要这样做。这会使代码更难理解。这段代码已经很简单、紧凑且严格。为什么要让人们理解您的宏?@DavidSchwartz我的真实函数执行所有udp/tcp/server/client,并且需要一个月的时间才能将其清理到完美,是的,宏可能是b广告创意,但破解技巧在许多不同的情况下都有很大的帮助。@MetNP查看您的源代码的人怎么能发现
oncebreak(err)
实际上会破解呢