aalosious
aalosious

Reputation: 588

C++ Bare Boolean Expressions (without an if, while or some other statement)

I found the following code as a solution to this online C++ coding problem:

"Problem: Write a program that will find the largest element of an arbitrary array without using any conditional structures: banned { if/if-else/switch/for/while/do/?:-operator }."

# include <iostream>
int findMax(int * array, size_t size)
{
    int max, other;
    size > 2 && (other = findMax(array + 1, size - 1)) || (other = array[1]);
    other > array[0] && (max = other) || (max = array[0]);
    return max;
}

int main()
{
    int array[] = {3, 1, 4, 15, 9, 2, 6, 5, 35, 8, 97, 93, 23, 84, 62, 64, 33, 83, 27, 950, 28, 841, 971, 69, 39, 937, 510};
    std::cout << findMax(array, sizeof(array) / sizeof(array[0])) << std::endl;
    return 0;
}

My QUESTION: How can we do something like this in C++?

size > 2 && (other = findMax(array + 1, size - 1)) || (other = array[1]);

I only have limited experience in C++, and I haven't seen anything like this in java. Can someone please explain how we can have boolean expressions running bare without an if, while or some other statement. I googled it a lot, but couldn't find anything useful. Thanks a lot.


EDIT: Thanks, everyone for the replies. I understand the concept of short-circuiting. But normally I'd use it more like this:

1. if (boolean1 && boolean2 || boolean3)
2.  // do sth;
3. while(boolean1 || boolean2)
4.  // loop;
5. return boolean1 && boolean2;

Now I am more of a java user. So I tried somethin like the following in my java code:

size > 2 && (other = findMax(array + 1, size - 1)) || (other = array[1]);

But it just gave a compile-time error. I guess java is stricter on these rules. Anyway, thanks again.

Upvotes: 0

Views: 130

Answers (1)

Darth Hunterix
Darth Hunterix

Reputation: 1510

It's called short circuiting. http://en.wikipedia.org/wiki/Short-circuit_evaluation

Basically, if you have a && or || operator if there is no point in checking next value, it's never executed.

Consider this statement:

isCondition1Fulfilled(a) || isCondition2Fulfilled(a)

If isCondition1Fulfilled(a) is true, then the whole expression is true no matter what, so calling isCondition2Fulfilled(a) is only a waste of time. So it is never called.

Here is another one:

isCondition3Fulfilled(a) && isCondition4Fulfilled(a)

If isCondition3Fulfilled(a) is false, then there is no point in calling isCondition4Fulfilled(a)

Your program takes advantage of it, for example:

size > 2 && (other = findMax(array + 1, size - 1)) || (other = array[1]);

If size is lower or equal 2, then the rest is never called. If not, but findMax(array + 1, size - 1) turns out to be positive, (other = array[1]) is not called.

Figure out the rest yourself :)

EDIT (A response to EDIT in OP):

Java is indeed a lot stricter in this case. In C/C++ pretty much anything and go into if statement: int, pointer, string, constant number, char, etc. But as we discussed with @Keith Thompson, you can just place any statement like that in a line of code. It will just be evaluated and immediately discarded.

Upvotes: 1

Related Questions