0

I am a bit confused regarding my program below. In my if statement, I am calling the boolean function twice. For the i, and j variable I spicked, the "else" statement should be executed as both function calls will yield true. But my "i" variable is not being modified (it outputs as 1001 instead of 1000), and I have no idea why. The j variable is being modified as expected. The first function call passes in "j" variable, and the second function call passes in "i" variable. Could someone explain why the "i" variable is not being modified?

#include <iostream>
using namespace std;

const int MINCOLOR = 0;
const int MAXCOLOR = 1000;

bool clipColor(int &amountColor);


int main()
{
    int i=1001;
    int j = 3333;
    int k;
    bool check;

    if (clipColor(j) == false && clipColor(i) == false)
    {
        check = false;
    }
    else 
    {
        check = true;
    }

    cout << i << " " << j << " " << check << " " << endl;
    return 0;
}


bool clipColor(int &amountColor)       
{
  if (amountColor > MAXCOLOR)
  {
    amountColor = MAXCOLOR;
    return true;
  }
  else if (amountColor < MINCOLOR)
  {
    amountColor = MINCOLOR;
    return true;
  }
  else
  {
    return false;
  }
}

2 Answers 2

1

This is because of how the && and || operators work. For &&, if the first operand is false, then the second operand is never even evaluated. Therefore, the second function call is not made.

This is known as short-circuit evaluation, and is a feature not only of C and C++, but of most programming languages.

If you explicitly want to evaluate both sides, then for bool returns, the single & operator arguably does what you want. By specification, it does bit-wise AND, but since bool values only use exactly one bit, the result should be exactly what you need.

By the way, clipColor(j) == false is the same as !clipColor(j).

Sign up to request clarification or add additional context in comments.

13 Comments

I'm pretty sure we have a dupe for that question.
Is there any way to make it so the second function call is evaluated though?
Thanks guys. I just wanted to condense some code. I guess I need to perform the two function calls before the if statement and assign them to 2 separate variables and then check the 2 variables instead.
Thanks Dolda2000. I'll try that.
Is using the single "&" to do what I wanted to do considered bad practice? Since we usually don't see single ampersands in conditions.
|
0

In your code

if (clipColor(j) == false && clipColor(i) == false)
{
    check = false;
}
else 
{
    check = true;
}

The function clipcolor(j) is executing first and returning true. As a result there is no need for clipColor(i) to execute because whatever value clipColor(i) returns, the condition (clipColor(j) == false && clipColor(i) == false) will always be false.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.