Questions tagged [fall-through]

Use fall-through to refer to an idiom where a multi-way branch statement (such as a switch statement in C) intentionally enters the next branch after completing the current one (falling through to the next case).

45 questions
323
votes
7 answers

Test for multiple cases in a switch, like an OR (||)

How would you use a switch case when you need to test for a or b in the same case? switch (pageid) { case "listing-page": case "home-page": alert("hello"); break; case "details-page": alert("goodbye"); break; }
Andres
  • 5,002
  • 6
  • 31
  • 34
94
votes
1 answer

Why is GCC warning me about a fallthrough even when I use [[fallthrough]]?

In the following piece of code, I use the standard [[fallthrough]] attribute from C++1z to document that a fallthrough is desired: #include int main() { switch (0) { case 0: std::cout << "a\n"; …
s3rvac
  • 9,301
  • 9
  • 46
  • 74
91
votes
2 answers

Do Go switch/cases fallthrough or not?

What happens when you reach the end of a Go case, does it fall through to the next, or assume that most applications don't want to fall through?
mcandre
  • 22,868
  • 20
  • 88
  • 147
21
votes
5 answers

Switch fallthrough in Dart

I started learning Dart today, and I've come across something that my google skills are having trouble finding. How do I have a fall-through in a non-empty case? My use case is this: I'm writing a sprintf implementation (since dart doesn't have this…
Naddiseo
  • 1,014
  • 1
  • 7
  • 17
15
votes
1 answer

Fall through in pattern matching

currently in c#7 (version 15.3.4) following code is valid to compile but both variables are legitimately unusable. switch(fruit) { case Apple apple: case Orange orange: // impossible to use apple or orange break; case Banana…
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
12
votes
2 answers

Should there be a diagnostic from GCC compiler for this ill-formed C++ code involving [[fallthrough]] attribute?

I was testing C++17 features on GCC compiler version 7.1.0. This is related to the fallthrough attribute and the following example (live example) is adapted from online CPP reference here #include "iostream" using namespace std; int f(int n) { …
P.W
  • 26,289
  • 6
  • 39
  • 76
9
votes
1 answer

Does bash have a `default`-equivalent for the case statement when using fallthrough?

case "$action" in a|b) echo for a or b ;;& b|c) echo for c or b ;;& *) echo for everything ELSE ;;& esac So, as you can see, I'm using ;;& instead of ;; so that if action=b it will trigger both of the first two cases. However, a…
Hashbrown
  • 12,091
  • 8
  • 72
  • 95
9
votes
3 answers

How to use __attribute__((fallthrough)) correctly in gcc

Code sample: int main(int argc, char **argv) { switch(argc) { case 0: argc = 5; __attribute__((fallthrough)); case 1: break; } } Using gcc 6.3.0, with -std=c11 only, this code gives a warning: :…
M.M
  • 138,810
  • 21
  • 208
  • 365
9
votes
2 answers

Duff's device in Swift

We know that a Duff's device makes use of interlacing the structures of a fallthrough switch and a loop like: send(to, from, count) register short *to, *from; register count; { register n = (count + 7) / 8; switch (count % 8) { case 0:…
loretoparisi
  • 15,724
  • 11
  • 102
  • 146
8
votes
3 answers

golang's fallthrough seems unexpected

I have the following code: package main import ( "fmt" ) func main() { switch { case 1 == 1: fmt.Println("1 == 1") fallthrough case 2 == 1: fmt.Println("2 == 1") } } Which prints both lines on the go…
domoarigato
  • 2,802
  • 4
  • 24
  • 41
5
votes
0 answers

C-style switch statement with fall-through in Rust

The match in Rust only executes one arm. I found this code snippet from Murmurhash 3: switch(len & 15) { case 15: k2 ^= ((uint64_t)tail[14]) << 48; case 14: k2 ^= ((uint64_t)tail[13]) << 40; case 13: k2 ^= ((uint64_t)tail[12]) << 32; case…
JACK M
  • 2,627
  • 3
  • 25
  • 43
5
votes
3 answers

Confusing output on fizzbuzz with switch case statement

Here the famous "fizz buzz" program in Go using switch/case and if/else conditionals. The problem is that using switch/case is generating unexpected output while if/else (with same conditions) works fine. I know that switch/case in golang is…
5
votes
5 answers

Is it possible to use || in PHP switch?

switch ($foo) { case 3 || 5: bar(); break; case 2: apple(); break; } In the above code, is the first switch statement valid? I want it to call the function bar() if the value of $foo is…
Ali
  • 261,656
  • 265
  • 575
  • 769
4
votes
3 answers

Why not force explicit fall-through rather than explicit break for switch statements?

This is not specifically a C# question but it is the C# version of switch that makes me ask the question. Why do I have to explicitly state that I do not want to fall through from one case statement to the next instead of instead indicating when I…
Justin
  • 8,853
  • 4
  • 42
  • 42
4
votes
1 answer

Indicating that a switch case falls through

A case falling through to the next case, may indicate an error, but sometimes it is intentional; if so, it is good practice to mark it so, for the benefit of both humans and the compiler. The former can be done with a comment: switch…
rwallace
  • 31,405
  • 40
  • 123
  • 242
1
2 3