if(condition1) if(condition2) if(condition3) if(condition4) < Body> else { printf(" Condition4 failed"); } else { printf(" Condition3 failed"); } else { printf(" ;条件2失败); } else { printf(条件1失败); } 实际上,我只是再次查看代码并看到其他 分支不包含return语句所以你可能是正确的: if(condition1 ) if(condition2) if(condition3) if(condition4) < Body> else { printf(" Condition4 failed"); } else { printf(条件3失败); } else { printf(" Condition2 failed"); } else { printf(" Condition1 failed"); } 但是,如果(!c1)printf(" blah"); $ b,该代码可以写成更紧密的关联 $ b else if(!c2)printf(" bleh" ); else if(!c3)printf(" blih"); else if(!c4)printf(" bloh"); else< body> (布局和大括号的味道),所以原来的布局没有 建议不得不坚持一个出口的风格。 [所有这些printfs的末尾应该有一个\ n吗?] - Chris" seeker" Dollin 我还在这里,我正在拿着答案 - 卡纳塔克邦,/爱与感情/ I recently came across s function (actually many) whose structure is aseries of nested ifs with the meat of the function at the very centre:if (condition1)if (condition2)if (condition3)if (condition4)<Body>else {printf("Condition4 failed");return;}else {printf("Condition3 failed");return;}else {printf("Condition2 failed");return;}else {printf("Condition1 failed");return;}To me this is really unreadable and I would re-write as:if(!condition1) {printf("Condition 1 failed");return;}....<Body>I got to thinking why would you write code this way; seperating thecondition from it''s consequences. It occured to me that there may havebeen performance related concerns with respect to jumps so with theoriginal code, in the case of all conditions suceeding, you get nojumps but with my code you may get as many jumps as there areconditions because each if block would need to be skipped. I amcertainly not an expert in compilers or machine code and this isprobably a case of premature optimization in either case I just tryingto get into the head of the guy that wrote it in the first place.Cheers,Charlie. 解决方案Nah. It''s probably just another Wirthian with blinkers on: One Entry,One Exit, Or Else!RichardActually, I just looked at the code again and saw that the elsebranches do not contain return statement so you may well be correct:if (condition1)if (condition2)if (condition3)if (condition4)<Body>else {printf("Condition4 failed");}else {printf("Condition3 failed");}else {printf("Condition2 failed");}else {printf("Condition1 failed");}Actually, I just looked at the code again and saw that the elsebranches do not contain return statement so you may well be correct:if (condition1) if (condition2) if (condition3) if (condition4) <Body> else { printf("Condition4 failed"); } else { printf("Condition3 failed"); } else { printf("Condition2 failed"); }else { printf("Condition1 failed");}But that code could be written with closer association asif (!c1) printf( "blah");else if (!c2) printf( "bleh" );else if (!c3) printf( "blih" );else if (!c4) printf( "bloh" );else <body>(layout and braces to taste), so the original layout doesn''tsuggest having to stick to the one-exit style.[Should there be a \n at the end of all these printfs?]--Chris "seeker" Dollin"I''m still here and I''m holding the answers" - Karnataka, /Love and Affection/ 这篇关于嵌套ifs和速度。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-30 16:20