@PSkocik's answer is fine, but I add my two cents. Unsure if I should do this as a comment, or as an answer; choosing the latter, because IMHO worth others seeing, whereas comments are frequently invisible.
Not only do I occasionally use
if(0) {
//deliberately left empty
} else if( cond1 ) {
//deliberately left empty
} else if( cond2 ) {
//deliberately left empty
...
} else {
// no conditions matched
}
But I also occasionally do
if( 1
&& cond1
&& cond2
...
&& condN
) {
or
if( 0
|| cond1
|| cond2
...
|| condN
) {
for complicated conditions. For the same reasons - easier to edit, #ifdef, etc.
For that matter, in Perl I will do
@array = (
elem1,
elem2,
...
elem1,
) {
- note the comma at the end of the list. I forget if commas are separators or delimiters in C and C++ lists. IMHO this is one thing we have learned: [Are trailing commas in Perl a bad practice? commas] are a good thing. Like any new notation, it takes a while to get used to.
I compare the if(0)
code to lisp
(cond (test1 action1)
(test2 action2)
...
(testn actionn))
which, you guessed it, I may indent as
(cond
(test1 action1)
(test2 action2)
...
(testn actionn)
)
I have sometimes tried to imagine what a more human readable syntax for this might look like.
Perhaps
IF
:: cond1 THEN code1
:: cond2 THEN code2
...
:: condN THEN codeN
FI
inspired by Dikstra's [https://en.wikipedia.org/wiki/Guarded_Command_Language#Selection:_if][Guarded Command Language].
But this syntax implies that the conditions are evaluated in parallel, whereas if...else-if
implies sequential and prioritized evaluation of conditions.
I started doing this sort of thing when writing programs that generated other programs, where it is especially convenient.
While we are at it, when writing RTL using Intel's old iHDL, I have coded stuff like
IF 0 THEN /*nothing*/
**FORC i FROM 1 TO 10 DOC**
ELSE IF signal%i% THEN
// stuff to do if signal%i% is active
**ENDC**
ELSE
// nothing matched
ENDIF
where the FORC..DOC..ENDC
is a macro preprocessor loop construct, that expands to
IF 0 THEN /*nothing*/
ELSE IF signal1 THEN
// stuff to do if signal1 is active
ELSE IF signal2 THEN
// stuff to do if signal2 is active
...
ELSE IF signal100 THEN
// stuff to do if signal100 is active
ELSE
// nothing matched
ENDIF
This was single assignment, non-imperative, code, so setting a state variable was not allowed, if you needed to do things like find first set bit.
IF 0 THEN /*nothing*/
ELSE IF signal1 THEN
found := 1
ELSE IF signal2 THEN
found := 2
...
ELSE IF signal100 THEN
found := 100
ELSE
// nothing matched
ENDIF
Come to think of it, this may have been the first place that I encountered such constructs.
BTW, the objections that some had to the if(0) style - that the else-if-conditions are sequentially dependent and cannot be arbitrarily reordered - do not apply to AND and OR and XOR logic in RTL - but do apply to short-circuit && and ||.