Logic programming help

大兔子大兔子 提交于 2019-12-11 01:28:03

问题


A = if infos !empty and inputs empty - do remove;

B = if infos empty and inputs !empty - do add;

C = if infos !empty and inputs !equal to infos - do add;

We can have like:

if B //it's the most common operation, so at the beginning.
{
  //add 
}
else
{
 //remove
}
elseif(c) 
{
 //the same add
} 

I believe this can be better thinking. Can I have your help?

Thanks in advance,


回答1:


if (infos != inputs) {
    if (empty(inputs)) {
        // remove
    } else {
        // add
    }
}

Remember, the outermost condition checks that both values are never empty (never the same, actually). E.g.,

A = if infos !empty and inputs empty - do remove;

If inputs is empty infos can not be empty. Therefore, remove.

B = if infos empty and inputs !empty - do add;
C = if infos !empty and inputs !equal to infos - do add;

Different and inputs not empty => it doesn't mather whether info is empty => add.




回答2:


if (B || C) 
{
  //add 
}
else
{
 //remove
}



回答3:


it's if, elseif (as much elseif's as you want) and finally else:

if B //it's the most common operation, so at the beginning.
{
  //add 
} elseif(something else) 
{
 //the same add
} elseif(c) 
{
 //the same add
} else
{
 //remove
}



回答4:


You sure can do that. Just order the conditional blocks: If... Else If... Else.




回答5:


if( !A ) {
  add;
} else {
  remove;
}

Short and clear in my opinion.




回答6:


You can combine the conditions of B and C with the OR operator:

if (empty($infos) && !empty($inputs) || !empty($infos) && $inputs != $infos) {
    // add
} else if (!empty($infos) && empty($inputs)) {
    // remove
}


来源:https://stackoverflow.com/questions/3635089/logic-programming-help

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!