虽然vs嵌套如果? [英] While vs nested if?

查看:71
本文介绍了虽然vs嵌套如果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之间是否有任何性能问题:

bool run = true;

while(run){


if(bob1&&){


// dothing

休息;

}

if(bob2){


// dothing

休息;

}


if(bob2){


// dothing

休息;

}

run = false;


}


(我知道可以使用开关。但有时条件

由更多部分组成,因此开关可能不适用)

使用嵌套ifs:

if(bob1){


// dothing

休息;

}否则{

if(bob2){


// dothing

休息;

}否则{

if(bob2){


// dothing

休息;

}

}}

我更喜欢while版本活动虽然它可能有点不正统。

解决方案例

desktop写道:


之间是否有任何性能问题:


bool run = true ;

while(run){


if(bob1&&){


// dothing

休息;

}


if(bob2){


//什么都没有

休息;

}


if(bob2){



这是无法访问的。你上面已经有''bob2''。我假设

你的意思是''bob3''。


>

// dothing

休息;

}


run = false;


}


(我知道可以使用开关。但有时条件

包含更多部件,因此开关可能不适用)


使用嵌套ifs:


if(bob1){


// dothing

break;



'在任何循环之外''break''是语法错误。


} else {


if(bob2){


// dothing

break;

}否则{

if(bob2){


// dothing

break;

}

}}


我更喜欢while版本事件,虽然它可能有点不正统。



我不相信问题可以是在......之间。我认为问题

可以与...同时或有时在...中。但是,在你发布的代码中,

我看到一个简单的问题:前者没有表达意图。如果您打算进行分支处理,请执行此操作。为什么将它包装成人工逻辑结构?并且没有必要嵌套

任何东西,只需链接它们(这与嵌套基本相同

只有没有花括号):


如果(bob1){

做一个

}

否则if(bob2){

做两个

}

其他fi(bob3){

做三个

}


V

-

请在通过电子邮件回复时删除资金''A'

我这样做没有回应最热门的回复,请不要问


----- BEGIN PGP签名消息-----

哈希:SHA1


桌面写道:


我更喜欢while版本事件虽然它可能有点非正统。



恕我直言我总是更喜欢嵌套方法的简单2(最多3

级别)支票,如果它更复杂的话我用`case`或`if`来使用Bit Toggle

方法,我不太明白为什么你需要一个

`而像那样,做你有一个更明确的例子吗?


- -

Guillermo Antonio Amaral Bastidas(gamaral)

#Free&开源软件代言人

#KDE开发人员:gamaral

@ http://blog.guillermoamaral.com/


irc:gamaral @ freenode pgp:0x5D297B74


-----开始PGP SIGNATURE -----

版本:GnuPG v1.4.7(GNU / Linux)

iD8DBQFG8tS3xxxW3F0pe3QRArKiAJ4 / 2vhLzT2l0gEVcrWwRxKpfgcK6ACfazTy

U0g + mOPw1LP + Gw9XKKj30nQ =

= 3Rt5

----- END PGP SIGNATURE -----

Are there any performance issues between:

bool run = true;
while(run) {

if (bob1 &&){

//dothing
break;
}
if(bob2) {

//dothing
break;
}

if(bob2) {

//dothing
break;
}

run = false;

}

(I am aware that a switch could be used. But sometimes the conditions
consists of more parts and therefore a switch might not apply)
With nested ifs:
if (bob1){

//dothing
break;
} else {
if(bob2) {

//dothing
break;
} else {
if(bob2) {

//dothing
break;
}
}}
I prefer the while version event though it might be a bit unorthodox.

解决方案

desktop wrote:

Are there any performance issues between:

bool run = true;
while(run) {

if (bob1 &&){

//dothing
break;
}
if(bob2) {

//dothing
break;
}

if(bob2) {

This is unreachable. You already have ''bob2'' above. I assume
you meant ''bob3''.

>
//dothing
break;
}

run = false;

}

(I am aware that a switch could be used. But sometimes the conditions
consists of more parts and therefore a switch might not apply)
With nested ifs:
if (bob1){

//dothing
break;

''break'' outside of any loop is a syntax error.

} else {
if(bob2) {

//dothing
break;
} else {
if(bob2) {

//dothing
break;
}
}}
I prefer the while version event though it might be a bit unorthodox.

I don''t believe that "issues" can be "between". I think "issues"
can be "with" or sometimes "in". In the code you posted, however,
I see a simple issue: the former does not convey the intent. If
you intended to have a branching processing, do it. Why wrap it
into an artificial logic structure? And there is no need to nest
anything, just chain them (which is basically the same as nesting
only without curly braces):

if (bob1) {
do one
}
else if (bob2) {
do two
}
else fi (bob3) {
do three
}

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

desktop wrote:

I prefer the while version event though it might be a bit unorthodox.

IMHO I have always preferred the nested approach for simple 2 ( max 3
level ) checks, if it''s something more complicated I use a Bit Toggle
approach with `case` or `if`, I don''t quite understand why you would need a
`while` like that, do you have a more clear example?

- --
Guillermo Antonio Amaral Bastidas (gamaral)
# Free & Open-source Software Advocate
# KDE Developer: gamaral
@ http://blog.guillermoamaral.com/


irc: gamaral@freenode pgp: 0x5D297B74

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFG8tS3xxxW3F0pe3QRArKiAJ4/2vhLzT2l0gEVcrWwRxKpfgcK6ACfazTy
U0g+mOPw1LP+Gw9XKKj30nQ=
=3Rt5
-----END PGP SIGNATURE-----


这篇关于虽然vs嵌套如果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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