0
Is it compulsory to have break statement at the end of the case....??
4 Antworten
+ 4
When you don't use break, all cases after first case which was execuded will be executed, for example:
a = 1;
switch(a) {
case 0:
print(0);
case 1:
print(1);
case 2:
print(2);
default:
print(3);
}
In this pseudocode 123 will be printed.
+ 1
C# compiler raises error if you don't use break. But it allows you to use multiple cases in a row with single body.
This is not allowed:
switch(a) {
case 0:
print(0);
case 1:
print(1);
case 2:
print(2);
default:
print(3);
}
But this is allowed:
switch(a) {
case 0:
case 1:
case 2:
print(2);
break;
default:
print(3);
break;
}
It will print 2 if a will be 0, 1 or 2
0
yes, because if you don't you may get unexpected results or error.
- 1
yes,we must give break after the each case, otherwise it ll print all the values until ll get null value in RAM.