返回函数后需要休息一下吗?
这是我的代码中的一个函数:
bool same_community(string s1, string s2)//check if student 1 & student 2 are in the same community
{
for(int i=0;i<number_of_communities;i++)
if(community[i].contains(s1) && community[i].contains(s2))
{
return true;
break;
}
return false
}
是否需要 return true 后的中断?
this is a function in my code:
bool same_community(string s1, string s2)//check if student 1 & student 2 are in the same community
{
for(int i=0;i<number_of_communities;i++)
if(community[i].contains(s1) && community[i].contains(s2))
{
return true;
break;
}
return false
}
is the break after return true needed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不。它是死代码,很可能会被编译器的优化器删除。删除它,因为它会降低代码的可读性。
No. It's dead code and will very likely be removed by your compiler's optimizer. Remove it, since it reduces your code's readability.
不,
return
语句足以退出循环。No, the
return
statement is enough to exit the loop.不,不需要休息。回报就够了。
No. The break is not needed. The return is enough.
不,你不需要。 return 表达式使执行流程离开函数。返回后的任何内容都不会被执行。
No, you don't need to. The return expression makes the execution flow leave the function. Anything after return won't get executed.
不,您不需要使用
break
。它用于提前终止循环,但 return(在子例程内)也将终止其所在的任何循环。return
之后的任何内容都是死代码。No you don't need to use
break
. It is used to terminate a loop early, butreturn
(within a subroutine) will also terminate any loop it is inside of. Anything that followsreturn
is dead code.