[Tex/LaTex] Trouble using pgfmathsetmacro in foreach loop more than once

foreachpgfmathtikz-pgf

I'm trying to draw a series of lines where the x-coordinate is given by a geometric sequence, as in the following example:

\begin{tikzpicture}
    \foreach \i in {0,...,3}
        \pgfmathsetmacro{\result}{7-pow(2,-\i)}
        \draw [red, ultra thick] (\result,3) -- (\result,5);
        \draw [blue, ultra thick] (\result,2.5) -- (\result,3);
        \draw [green, ultra thick] (\result,5) -- (\result,5.5);
\end{tikzpicture}

However, I can't seem to use \result more than once. If I comment out 2 of the \draw lines it works, yet if 2 or more \draw lines are uncommented I receive an error:

! Undefined control sequence.
<argument> \result

Best Answer

There are curly braces around the block inside the foreach missing:

\begin{tikzpicture}
    \foreach \i in {0,...,3} {
        \pgfmathsetmacro{\result}{7-pow(2,-\i)}
        \draw [red, ultra thick] (\result,3) -- (\result,5);
        \draw [blue, ultra thick] (\result,2.5) -- (\result,3);
        \draw [green, ultra thick] (\result,5) -- (\result,5.5);
    }
\end{tikzpicture}

works for me.