Issue with \loop to create contents of tabular row

Here is an example without using \loop...\repeat (recursive):

\documentclass{article}

\newcounter{countA}
\def\myline{%
    \stepcounter{countA}\Alph{countA}&%
    \ifnum\thecountA<12%
    \myline
    \fi
}

\begin{document}
\centering 

\begin{tabular}{|*{13}{c|}}
\hline
A & B & C & D & E & F & G & H & I & G & K & L & M \\  
\hline
\end{tabular}

\bigskip

\begin{tabular}{|*{13}{c|}}
\hline
\myline M\\  
\hline
\end{tabular}

\end{document}

EDIT: This needs the counter countA to be set to 0 each time the macro is called, because the loop is executed each time \myline is called instead of saving the results of the loop into \myline. One might solve this by inserting a \else\setcounter{countA}{0}, but the loop is still executed everytime.

EDIT2: To allow stuff similar to egreg's "more generic approach" but making it even more generic:

\documentclass{article}

\newcounter{countA}
\newcommand{\myline}[2][0]{%
    \ifnum#1>0\setcounter{countA}{#1}\fi%
    \stepcounter{countA}\Alph{countA}&%
    \ifnum\numexpr#2-1>\value{countA}%
    \myline{#2}%
    \else%
    \stepcounter{countA}\Alph{countA}%
    \setcounter{countA}{0}%
    \fi%
}

\begin{document}
\centering 

\begin{tabular}{|*{13}{c|}}
\hline
A & B & C & D & E & F & G & H & I & G & K & L & M \\  
\hline
\end{tabular}

\bigskip

\begin{tabular}{|*{13}{c|}}
\hline
\myline{13}\\
\hline
\myline[13]{26}\\
\hline
\end{tabular}

\end{document}

results


The problem is that \myline is not expanded. Using \edef instead of \def and removing al the expandafters solves this.

\documentclass{article}

\newcounter{countA}

\def\myline{}
\loop\ifnum\thecountA<12
  \stepcounter{countA}
  \edef\myline{%
    \myline
    \Alph{countA} &  
  }%
\repeat

\begin{document}

\centering 

\begin{tabular}{|*{13}{c|}}
\hline
A & B & C & D & E & F & G & H & I & G & K & L & M \\  
\hline
\end{tabular}

\bigskip

\begin{tabular}{|*{13}{c|}}
\hline
\myline M\\  
\hline
\end{tabular}

\end{document}

A more generic approach:

\documentclass{article}
\usepackage{etoolbox}

\newcounter{countA}
\newcommand{\alphline}[1]{%
  % I want #1 letters
  \setcounter{countA}{1}%
  \def\finalline{A}%
  \loop\ifnum#1>\value{countA}%
    \stepcounter{countA}%
    \xappto\finalline{& \Alph{countA}}%
  \repeat
  \finalline
}

\begin{document}

\begin{tabular}{|*{13}{c|}}
\hline
\alphline{13} \\
% for comparison
A & B & C & D & E & F & G & H & I & G & K & L & M \\
\hline
\end{tabular}

\end{document}

enter image description here

Same idea in expl3 (no counter needed)

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\alphline}{m}
 {
  \tl_set:Nn \l_tmpa_tl { A }
  \int_step_inline:nnnn { 2 } { 1 } { #1 }
   {
    \tl_put_right:Nn \l_tmpa_tl { & \int_to_Alph:n { ##1 } }
   }
  \tl_use:N \l_tmpa_tl
 }
\ExplSyntaxOff

\begin{document}

\begin{tabular}{|*{13}{c|}}
\hline
\alphline{13} \\
% for comparison
A & B & C & D & E & F & G & H & I & G & K & L & M \\
\hline
\end{tabular}

\end{document}