Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to simplify this logical expression in a single return statement?

I have been trying to simplify this function in a single return A ... B ... C statement but some cases always slip out. How could this checks be expressed in a logical way (with and, or, not, etc.)?

bool f(bool C, bool B, bool A)
{
if (A)
    return true;

if (B)
    return false;
   
if (C)
    return true;

return false;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

bool f(bool C, bool B, bool A)
{
if (A)
    return true;

if (B)
    return false;
   
if (C)
    return true;

return false;
}

is equivalent to

bool f(bool C, bool B, bool A)
{
if (A)
    return true;
else if (B)
    return false;
else if (C)
    return true;
else 
    return false;
}

is equivalent to:

bool f(bool C, bool B, bool A)
{
if (A)
    return true;
else if (!B)
{
    if (C)
        return true;
    else
        return false;
}
else
    return false;
}

is equivalent to:

bool f(bool C, bool B, bool A)
{
if (A)
    return true;
else if (!B and C)
    return true;
else
    return false;
}

is equivalent to:

bool f(bool C, bool B, bool A)
{
if (A or (!B and C))
    return true;
else
    return false;
}

is equivalent to:

bool f(bool C, bool B, bool A)
{
return (A or (!B and C));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading