I need to find in an image if a pixel has the maximum value compared to the 8 pixels around it.
I am not sure what is the most optimal way, so my idea is to use the if statement like this:
if(pixel > pixel1 && pixel > pixel2 && pixel > pixel3 && ... && pixel> pixel8)
My question is the following: if it found that for instance pixel is not bigger than pixel1, will it still check the rest of the statement or since it’s only ANDs, it will already discard the instruction and go further?
And if the answer is the first one, that would make it very computationally heavy to check each pixel all the time, can somebody give me a hint as how to approach more efficiently this simple problem?
This is called Short Circuit Evaluation.
Since the condition is
&&, it will NOT check further if it gets afalsein any of the conditions.Similarly if the condition were
||, it would stop checking once it finds atrue.Btw, I am not absolutely certain of the precedence rules, and because of that I would surround each condition in parentheses just to be safe.
Edit: Operator precedence rules seem to indicate that the parentheses in this case are unnecessary.