[Tex/LaTex] \ifcase and macro expansion

conditionalsexpansionmacros

There are some code. I strip out unrelated parts and get test one:

\documentclass[a4paper,11pt]{extarticle}
\begin{document}

\newcommand{\mystrA}{%
  a%
  \or b%
  \or c%
  \else d%
}
\newcommand{\mystrB}{%
  e%
  \or f%
  \else g%
}
\newcommand{\mystr}{\mystrA}

\newcounter{str}

\newcommand{\usestr}{%
  \ifcase\value{str}\mystr\fi%
  \addtocounter{str}{1}%
}

\usestr,\usestr,\usestr,\usestr,\usestr
\end{document}

and after compilation with pdflatex I get:

The resalt of compilation

But I was expecting to get "a,b,c,d," as result…

So question. What is going wrong?
Why \ifcase provide nothing for values of counter more than zero?

And how right rewrite the code to get I expected?

Best Answer

 \ifcase\value{str}\mystr\fi%

So TeX expands \value to get a number to test with \ifcase if that is >0 it skips ahead not expanding to find a matching \or or \fi and finds the \fi so the expansion of is empty.

I would use

\documentclass[a4paper,11pt]{extarticle}
\begin{document}

\newcommand{\mystrA}[1]{%
 \ifcase#1%
  a%
  \or b%
  \or c%
  \else d%
 \fi
}
\newcommand{\mystrB}[1]{%
 \ifcase#1%
  e%
  \or f%
  \else g%
 \fi
}
\newcommand{\mystr}{\mystrA}

\newcounter{str}

\newcommand{\usestr}{%
  \mystr{\value{str}}%
  \addtocounter{str}{1}%
}

\usestr,\usestr,\usestr,\usestr,\usestr
\end{document}
Related Question