I want to use foreach to draw connections between a sequence of nodes. But, for some reason, foreach forgets the position of the last node.
Here is an MWE illustrating my question: why are the two tikz figures different? As far as I can see, foreach used in the second figure should produce exactly the same code as in the first figure, where nodes are explicitly named. But it seems to forget updating the position of the last drawn node. Why?
Is there a way of producing the desired output (as in the first figure) using foreach? I don't want to use the coordinates, since I want the lines disconnected at nodes.
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node (n1) at (0,0) {1};
\node (n2) at (0,1) {2};
\node (n3) at (1,1) {3};
\node (n4) at (1,0) {4};
\draw (n1) -- (n2) -- (n3) -- (n4);
\end{tikzpicture}
\begin{tikzpicture}
\node (n1) at (0,0) {1};
\node (n2) at (0,1) {2};
\node (n3) at (1,1) {3};
\node (n4) at (1,0) {4};
\draw (n1) \foreach \i in {2,...,4} { -- (n\i)};
\end{tikzpicture}
\end{document}