Skip to content

Simplify the “if” statement

This is probably the shortest post I’ve ever made but its content can simplify your if statements.

As a developer of it is common to have an if statement that checks if one exactly one of two conditions is true. The first thought would be something like this:

if(condition1 && !condition2 || !condition1 && condition2)
   ...

XOR operator

There is an operator named XOR and you probably know that. I knew the XOR operator but didn’t use it in presented scenario. The if statement can be rewritten:

if(condition1 ^ condition2)
   ... 

Summary

Have a nice day, bye!

Published in.NET

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *