Different ways of writing the “if” statement [closed]

For cases like this, there’s also the conditional operator:

output = (val % 2 == 1) ? "Number is odd" : "Number is even";

If you’re definitely going to use an “if” I’d use version 2 or version 4, depending on the rest of your bracing style. (At work I use 4; for personal projects I use 2.) The main thing is that there are braces even around single statements.

BTW, for testing parity it’s slightly quicker to use:

if ((val & 1) == 1)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)