Justification of elseif or elif entries in some languages
I'm not sure if this is a good question, but it just bothered me from a theoretical point of view. Can anyone explain why some languages like php have an explicit elseif / elif, while others like C ++ or java only do it with if and else. It would seem that fewer keywords to remember are preferable to more keywords. Is there any benefit to having an elseif clause?
PS I am writing a book on programming languages and I am collecting opinions on language theory.
EDIT: Thanks for pointing this out. Now I understand that this is what matters in Python because of the white space / indentation rule. I changed my example from Python to PHP.
source to share
elseif
Avoids excessive indentation in my opinion, as @kalhartt commented, for python (required) and for other languages like C ++ (optional but normal).
But more importantly, I think it elseif
is a useful function that indicates a workflow in the sense that
1) else
there is nothing else in the branch , i.e. we do not have complex structures such as
if (A) {
}
else {
if (B) {...} else { ... }
i++;...
}
2) it helps to create a linear structure similar to an instruction switch
where
switch (a) {
case 1:
...
case 2:
...
case 3
...
default:
}
displays:
if (a == 1) {
}
elseif (a == 2) {
}
elseif (a == 3) {
}
else{
}
The form if ... elseif ...
is a little more verbose, but more powerful, that is, with a less stringent conditional requirement for each "case". For example, you can use
elseif (a > 3 && a < 7)
source to share