There is something I've always wondered about switch statments that follow directly after an else. For example, if you do a null check before a switch statement, you usually do something like this (for example in Java):
if (testVar == null) {
System.out.println("It's null");
} else {
switch (testVar) {
case "test1":
System.out.println("test1");
break;
}
}
An alternative way to achieve the same thing is using a switch-statement that is not sourrounded by curly braces:
if (testVar == null) {
System.out.println("It's null");
} else switch (testVar) {
case "test1":
System.out.println("test1");
break;
}
It's clear that any statement can follow after "else", but why does it seem to be standard to use a block after "else" when it's a switch statement? This is not the case for "else if" where it's standard to use an if-statement directly. Why is "else switch" and "else if" generally treated differently in this respect?
Aucun commentaire:
Enregistrer un commentaire