Reputation: 1988
Using FizzBuzz as an example, this switch case inside the for loop uses break after each case.
for (num = 1; num <= 100000; num ++) {
switch (true) {
case (num % 15 == 0):
console.log("FizzBuzz");
break;
case (num % 3 == 0):
console.log("Fizz");
break;
case (num % 5 == 0):
console.log("Buzz");
break;
default:
console.log(num);
break;
}
}
In Mozilla's docs, they say that...
The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.
I'm assuming what they mean is that when the script checks that num % 15 == 0, if there's no break and eventhough it matches true, it goes to num % 3 ==0 and so on. So then I did two tests with 100,000 numbers. One with the breaks and one without them.
I found about 2 seconds difference between break and no break, with break shorter and faster. But that's over many iterations.
My question is, does it really matter for most developers building small scale apps to use breaks in their loops? Is it a matter of habit and preference? Thoughts?
Upvotes: 1
Views: 4585
Reputation: 808
To elaborate on Callum Morrisson's answer:
One case where you can remove break
gracefully is when you are returning a value in a case.
For example,
switch(expression) {
case x:
return *something*
case y:
return *something different*
}
This does not cause fallthrough due to return
breaking you out of the statement. You can think of return
as doubling for a break
in this example.
If you were to run a linter on a switch statement with both a return
and a break
within a case, you would receive an unreachable code error since the return
is breaking you out of the statement before the break
has a chance to execute.
// eslint
switch(expression) {
case x:
return *something*
break; // error unreachable code
case y:
return *something different*
break; // error unreachable code
}
Upvotes: 0
Reputation: 8945
If you do not include a break
statement, you have undoubtedly introduced a bug(!) into your program!
Here's why:
If you do not include a break
statement, the logic will "simply fall-through" into the code for the additional conditions . . . but it will not(!) evaluate those conditions! It will simply crash right through, like the proverbial bull in the proverbial china-shop.
In other words, without the break
statements, the case for (num % 15 == 0)
would console.log()
four(!!) (not "one") messages. Similarly, (num % 3 == 0)
would log three, and so on.
"Heh... Don't go there."
=====
EDIT: It may be helpful to consider that the original ("C...") concept of switch
was that of "a multi-way goto
" occurring at the top of the construct, which branched to one of the case
-points within the body. The break
statement generated a goto
that sent you to the end of the switch
. If you did not include it, the "helpful" assumption was that you intended to do what otherwise would happen: that one case
would simply "fall through" to the next one. (Which, absent break
, is precisely what it does.) "Caveat Coder!"
Upvotes: 1
Reputation:
When a case
condition is met in a switch statement it will execute all of the code blocks within that switch statement until it hits an exit command (think break
or return
). So the break
is not only changing performance speed it's also changes functionality.
An easy way to think about switch statements is as alternative's to chained if
-else
statements.
your switch
code can just as easily be written
if (num % 15 == 0) {
console.log("FizzBuzz");
} else if (num % 3 == 0) {
console.log("Fizz");
} else if (num % 5 == 0) {
console.log("Buzz");
} else {
console.log(num);
}
if you remove the break;
statements you'll change how the code actually functions. It will perform more like
if (num % 15 == 0) {
console.log("FizzBuzz");
}
if (num % 3 == 0 || num % 15 == 0) {
console.log("Fizz");
}
if (num % 5 == 0 || num % 3 == 0 || num % 15 == 0) {
console.log("Buzz");
}
console.log(num);
The main difference being the lack of an else
condition.
Upvotes: 12
Reputation: 7308
You get a different result with and without break
. Without a break
it passes through so on num = 15
for example it will print out "FizzBuzz"
, "Fizz"
, "Buzz"
, and "15"
. Whereas with break
it will just print out "FizzBuzz"
. Having said that you don't need a break after the default
case, if it is at the end of the switch.
Upvotes: 2