An expandable and more complete 'switch' solution is:
\documentclass{article}
\makeatletter
\def\identoendif#1\endif{#1}
\def\oneoftoendif#1#2\endif{#1}
\def\gobbletoendif#1\endif{}
\newcommand*\ifstrsame[2]{%
\@nameuse{@\ifnum\pdfstrcmp{\detokenize{#1}}%
{\detokenize{#2}}=0first\else second\fi oftwo}%
}
\def\dimexpr@i#1{#1\dimexpr}
\def\ifnumcase#1{%
\ifstrsame{#1}\elseif\gobbletoendif{%
\ifstrsame{#1}\endif\@gobble\ifnumcase@i
}{#1}%
}
\def\ifnumcase@i#1#2{%
\ifstrsame{#2}\elseif\identoendif{%
\ifstrsame{#2}\endif{}{%
\ifdim\dimexpr#1pt\relax\dimexpr@i#2pt\relax
\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\oneoftoendif}{\ifnumcase@ii{#1}}%
}%
}%
}
\def\ifnumcase@ii#1#2#3{\ifnumcase@i{#1}{#3}}
\makeatother
%% Examples:
\edef\x{%
\ifnumcase{6}% <- number to test
{<1}{less than 1}
{<3}{less than 3}
{>5}{greater than 5}
% add other tests if needed
\elseif
between 3 and 5% all tests failed
\endif
}
\show\x -> greater than 5
\edef\x{%
\ifnumcase{3.14}% <- number to test
{<2}{less than 1}
{<3}{less than 3}
{<4}{less than 4}
{<10}{less than 10}
% add possible other tests
\endif
}
\show\x -> less than 4
\edef\x{%
\ifnumcase{314}% <- number to test
{<2}{less than 1}
{<3}{less than 3}
{<4}{less than 4}
{<10}{less than 10}
% add possible other tests
\endif
}
\show\x -> empty
\begin{document}
% Nothing to do:
\ifnumcase\elseif\endif
\ifnumcase{3}\elseif\endif
\ifnumcase\endif
\ifnumcase{3}\endif
\end{document}
unbonpetit's latest solution fails for
\ifnumcasse{3}
{=<1}{lt or equal 1}
{=>3}{gt or equal 3}
\elseif
beteween 1 and 3
\endif
and
\def\gobto@endif#1\endif{}
is redundant.
A more general solution is
\makeatletter
\def\elseif{\@gobble\elseif}
\def\endif{\@gobble\endif}
\def\swap#1#2{#2#1}
\def\ifstrsame#1#2{%
\ifnum\pdfstrcmp{\detokenize{#1}}{\detokenize{#2}}=\z@
\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}
\def\ifstrnull#1{%
\ifnum\pdfstrcmp{\detokenize{#1}}{}=\z@
\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}
\def\ifcmdeq#1#2{%
\ifx#1#2\endif\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}
\def\if@eqin#1=#2\@nil{%
\ifstrnull{#2}\@secondoftwo{\ifstrnull{#1}\@secondoftwo\@firstoftwo}%
}
\def\if@dimwitheq#1#2=#3\@nil{\unless\ifdim#1pt\if<#2>\else<\fi#3pt }
\def\docasecallback#1#2\endif{#1}
\def\doelsepart#1\endif{#1}
\def\ifnumcase#1#2{%
\expandafter\ifcmdeq\@car#2\@nil\elseif\@firstoftwo{%
\expandafter\ifcmdeq\@car#2\@nil\endif\@gobbletwo\@secondoftwo
}%
\doelsepart{%
\expandafter\expandafter\expandafter
\if@eqin\checkcomparators#2\@nil=\@nil{%
\expandafter\expandafter\expandafter\swap
\expandafter\expandafter\expandafter
{\checkcomparators#2\@nil}{\if@dimwitheq{#1}}\@nil
}{%
\ifdim#1pt#2pt %
}
\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
\docasecallback{\ifnumcase@i{#1}}%
}%
}
\def\ifnumcase@i#1#2{\ifnumcase{#1}}
\def\checkcomparators#1#2#3\@nil{%
\romannumeral
\ifstrsame{#1}={\ifstrsame{#2}<{0<=#3}{\ifstrsame{#2}>{0>=#3}{0 #1#2#3}}%
}{0 #1#2#3}%
}
\makeatother
\elseif,\elif; and needs\fifor every\if. So usually we always write bad-looking code. – Leo Liu Nov 26 '11 at 07:34