Handling Bool Value With an If Statement

74,570

Solution 1

The value you return using the return statement is literally returned whenever you call the function.

So if you write this code somewhere else in your program:

bool returnValue = trueOrFalse();

then the returnValue variable will contain a Boolean value equivalent to whatever was returned by the trueOrFalse() function.

The function itself doesn't "hold" the value, the caller of the function defines a variable that will hold the value after the function call has completed.


As for your second question, you are using the return statement correctly, but you can simplify your code in the trueOrFalse() function substantially. Instead of this:

   if (validate == true)
   {
       return true;
   }
   else
   {
       return false;
   }

All you need is this:

return validate;

Because the validate local variable is already a bool!

This removes the redundancy of checking a Boolean value against a Boolean constant (true), which prevents strange errors from creeping in and makes the code easier to read and understand.

In fact, you can use this general pattern any time you're working with Boolean values (bool). Rather than comparing them against the literal false or true, you can just write:

if (validate)  // test for truth
{
    // do whatever...
}

or

if (!validate)  // test for falsity
{
    // do whatever...
}

Solution 2

Replace

 if (validate == true)
       {
       return true;
       }
    else
       {
       return false;
       }

with

return validate;

There is no reason to test it and return a different boolean.

While you're at it, replace the previous

if (validate == true)

with

if (validate)
Share:
74,570
Wylie Coyote SG.
Author by

Wylie Coyote SG.

SE Student

Updated on January 13, 2020

Comments

  • Wylie Coyote SG.
    Wylie Coyote SG. over 4 years

    I am a student programmer currently designing a GUI for my company with Qt and I have a rather simple question that I just cant seem to find the answer to on the web. It seems like someone has had to of asked it before so if you know where the answer is Id be happy with a reference. My question is; can a Boolean datatype in c++ be handled using an if statement. So a bool value is either one or zero so can you do something like this

    bool trueOrFalse()
    {
        myclass temp;
        QString tempstr;
        double candidate;
        bool validate;
    
        tempstr = ui->tableWidgetInjectionLocations->item(i,9)->text();
        candidate = tempstr.toDouble(&validate);
                    if(validate == true)
                    {
                        temp.tempProperty = candidate;
                    }
                    else
                    {
                        QMessageBox error;
                        error.setText("Error");
                        error.exec();
                    }
        if (validate == true)
           {
           return true;
           }
        else
           {
           return false;
           }
    }
    

    What I am really looking for here is in the last section of this bool function. When I use return am I actually returning a value that this function would then hold or am I using a the keyword return inappropriately? Once validation has past Id like to be able to use the function to indicate whether or not to proceed in another function Please keep my criticism constructive please. As a student I am only interested in improving.

  • Wylie Coyote SG.
    Wylie Coyote SG. over 12 years
    The reason I have the format if (validate == true) { return true; } else { return false; } is because my original if statement is like if (validate1 == true && validate2 == true && validate3 == true && ... and if my validate bools do return true I want the data to append to a vector that I have built and then give the parent function a value of true; Am I set up right for this in that case?
  • Cody Gray
    Cody Gray over 12 years
    Yes, that's correct. But like Blastfurnace says, you can still use the simplified syntax (if (validate1 && validate2 && validate3)). The reason it works properly is because of the && operator, which ensures that all of the cases are true.
  • Wylie Coyote SG.
    Wylie Coyote SG. over 12 years
    how does the if statement know that I'm looking for the data to be true then?Is it because bool values are all true unless prefixed with a !? I also need the if statement to declare whether or not the trueOrFlase function is a true or false bool so that it can be used in another function to decide on whether or not to proceed.
  • Cody Gray
    Cody Gray over 12 years
    Yes, you're always checking for truth in an if statement. Implicitly, you're checking validate1 == true for truth, so you could write (validate1 == true) == true, but that's silly. The same pattern applies though, allowing you to reduce it down even further to just validate1. The ! operator ("not" operator) negates whatever you apply it to, so it's the same thing as checking for falsity.
  • Wylie Coyote SG.
    Wylie Coyote SG. over 12 years
    Thanks, You have defiantly helped me today! I also greatly appreciate the way you were inclusive!