Bool Question

Jul 25, 2012 at 2:25am
Hi Everyone. I have a question regarding Boolean logic. I understand that even numbers are determined by not having a remainder, and odd numbers are determined by having a remainder. However, I am having a little bit of difficulty understanding how the compiler is understanding this.

For example, I would understand if there was an "if else" statement after bools' { and before return, but I'm having trouble understanding how everything is done in the return line.

Is it that it's only returning a value in the case of no remainder? Please help and thanks in advance!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool isEven(int number) {
    return ((number % 2) == 0);
}

int main()
{
    int num;
    cout << "Enter a number: ";
    cin >> num;
    if(isEven(num))
        cout << num << " is even." << endl;
    else
        cout << num << " is odd." << endl; 

   return 0;
}
Jul 25, 2012 at 2:28am
It works like an if statement does.

If you want to think about it like an if statement:
return x;
is the same as:
1
2
if(x) return true;
else return false;


So basically, isEven will get the result of ((number % 2) == 0) (which is a bool result, true or false), and then return that.
Jul 25, 2012 at 2:32am
Well if I remember a bool command just checks if the argument is true or not. So if the compiler does the math and finds the bool value to be true it returns a value (1) if the argument is false then the program nothing.

So for your program if you enter a number and that number has no remainder when it is divided by 2, the program returns a value and does the first option. If the program doesn't return a value then it defaults to the second option.
Jul 25, 2012 at 2:34am
Well if I remember a bool command just checks if the argument is true or not. So if the compiler does the math and finds the bool value to be true it returns a value (1) if the argument is false then the program nothing.

So for your program if you enter a number and that number has no remainder when it is divided by 2, the program returns a value and does the first option. If the program doesn't return a value then it defaults to the second option.


No, the function will return true or false. Functions that return values cannot return "nothing". Using something like:

if(x) //x is a bool

is the same as saying:

if(x == true)
Last edited on Jul 25, 2012 at 2:34am
Jul 25, 2012 at 2:40am
Please don't do this:
if(x != 0)
Remember that the if statement already checks to see if whatever in the parenthesis is a nonzero value. Hopefully your compiler will optimize it, but if it doesn't, it checks if it's a nonzero value- twice.
Instead, use:
if(x)
Last edited on Jul 25, 2012 at 2:41am
Jul 25, 2012 at 2:41am
Thanks a lot for clearing that up for me!

ありがとう、アンドリュー。 100パーセント分かった!
Jul 25, 2012 at 2:52am
Instead, use:
if(x)

On the other hand, JAVA won't accept that if x is a non-Boolean. It's nice to make functions with portability. It can also be confusing that way. RastaWolf's original function could have this:
return !( x % 2 );
But even writing it I need to do some thinking just to make sure it's correct.
Last edited on Jul 25, 2012 at 2:54am
Jul 25, 2012 at 2:56am
On the other hand, JAVA won't accept that if x is a non-Boolean.


This is not a JAVA forum. Please don't encourage such practice in the C++ language. Its flexibility with the if() statement, as well as the for() loop should be encouraged for optimization.
Jul 25, 2012 at 3:02am
Remember that the if statement already checks to see if whatever in the parenthesis is a nonzero value.


I might be wrong here, but I believe they are the same. Equality for ints requires a subtraction (1 instruction) and a status bit check. For bools, you still have to set the status bit somehow before (also 1 instruction) and a status bit check. So it's the same either way.

This is not a JAVA forum. Please don't encourage such practice in the C++ language. Its flexibility with the if() statement, as well as the for() loop should be encouraged for optimization.


Personally, I write if(x != 0) all the time as it is more clear. I only use the if(x) statement if x is a bool or a pointer.

It's a stylistic issue IMO.
Jul 25, 2012 at 3:10am
The ISO/IEC C++ Standard states that the expression is implicitly converted to bool, returns TRUE if either the opperand is a nonzero value, or false otherwise.

It is safe with any built in type, (int, long, char, bool, pointers) except void.
Last edited on Jul 25, 2012 at 3:11am
Jul 25, 2012 at 3:21am
Just to be 100% sure, can someone verify this please?

1
2
3
bool isEven(int number) {
    return ((number % 2) == 0);
}


The above sets the condition and returns a value, true or false.

if(isEven(num))

The above is the same as saying if(isEven(num) == true)

Is my understanding correct?

Thanks for all the input!
Jul 25, 2012 at 3:26am
You got it :)
Jul 25, 2012 at 3:31am
Thanks!
Jul 25, 2012 at 3:31am
Yes. The two if() statements yield the same result.
Topic archived. No new replies allowed.