Code Inspection: Fallthrough in 'switch' statementReports a switch statement where control can proceed from a branch to the next one. Such "fall-through" often indicates an error, for example, a missing break or return .
Following rules govern the fall through the behavior of switch statement. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
Fall through is a type of error that occurs in various programming languages like C, C++, Java, Dart …etc. It occurs in switch-case statements where when we forget to add break statement and in that case flow of control jumps to the next line.
C++ Attributes [[fallthrough]] From C++17 on, a standard attribute was introduced to indicate that the warning is not needed when the code is meant to fall through. Compilers can safely give warnings when a case is ended without break or [[fallthrough]] and has at least one statement.
Use a vertical bar (|
) for "or".
case "$C" in
"1")
do_this()
;;
"2" | "3")
do_what_you_are_supposed_to_do()
;;
*)
do_nothing()
;;
esac
Recent bash
versions allow fall-through by using ;&
in stead of ;;
:
they also allow resuming the case checks by using ;;&
there.
for n in 4 14 24 34
do
echo -n "$n = "
case "$n" in
3? )
echo -n thirty-
;;& #resume (to find ?4 later )
"24" )
echo -n twenty-
;& #fallthru
"4" | [13]4)
echo -n four
;;& # resume ( to find teen where needed )
"14" )
echo -n teen
esac
echo
done
sample output
4 = four
14 = fourteen
24 = twenty-four
34 = thirty-four
()
behind function names in bash unless you like to define them.[23]
in case to match 2
or 3
''
instead of ""
If enclosed in ""
, the interpreter (needlessly) tries to expand possible variables in the value before matching.
case "$C" in
'1')
do_this
;;
[23])
do_what_you_are_supposed_to_do
;;
*)
do_nothing
;;
esac
For case insensitive matching, you can use character classes (like [23]
):
case "$C" in
# will match C='Abra' and C='abra'
[Aa]'bra')
do_mysterious_things
;;
# will match all letter cases at any char like `abra`, `ABRA` or `AbRa`
[Aa][Bb][Rr][Aa])
do_wild_mysterious_things
;;
esac
But abra
didn't hit anytime because it will be matched by the first case.
If needed, you can omit ;;
in the first case to continue testing for matches in following cases too. (;;
jumps to esac
)
Try this:
case $VAR in
normal)
echo "This doesn't do fallthrough"
;;
special)
echo -n "This does "
;&
fallthrough)
echo "fall-through"
;;
esac
If the values are integer then you can use [2-3]
or you can use [5,7,8]
for non continuous values.
#!/bin/bash
while [ $# -gt 0 ];
do
case $1 in
1)
echo "one"
;;
[2-3])
echo "two or three"
;;
[4-6])
echo "four to six"
;;
[7,9])
echo "seven or nine"
;;
*)
echo "others"
;;
esac
shift
done
If the values are string then you can use |
.
#!/bin/bash
while [ $# -gt 0 ];
do
case $1 in
"one")
echo "one"
;;
"two" | "three")
echo "two or three"
;;
*)
echo "others"
;;
esac
shift
done
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