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;
}
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.
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:
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)
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.
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.
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.
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.