when I'm using a switch(in Java in this case) I normally use the default case if needed. One of my teachers told me that when he used to program in Pascal, that case didn't exist. He said that if it didn't exist in Pascal it shouldn't be something good to use.
My questions are:
Thanks in advance.
If no default clause is found, the program continues execution at the statement following the end of switch . By convention, the default clause is the last clause, but it does not need to be so. A switch statement may only have one default clause; multiple default clauses will result in a SyntaxError .
A 'switch' statement should have 'default' as the last label. Adding a 'default' label at the end of every 'switch' statement makes the code clearer and guarantees that any possible case where none of the labels matches the value of the control variable will be handled.
A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
2) Default: This keyword is used to specify the set of statements to execute if there is no case match. Note: Sometimes when default is not placed at the end of switch case program, we should use break statement with the default case. 2) Duplicate case values are not allowed. 3) The default statement is optional.
I would consider it a bad habit not to use it.
Edit:
This is Pascal, just to prove your teacher wrong
case place of
1: writeln('Champion');
2: writeln('First runner-up');
3: writeln('Second runner-up');
else writeln('Work hard next time!');
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With