Lengths and for-loops

It seems that lengths are not updated in for loops. Compiling the code below:

\documentclass{article}
\usepackage{tikz}
\newlength{\test}

\begin{document}

Test: .\hspace{\test}.

\inc

Test: .\hspace{\test}.

\setlength{\test}{0cm}

\incs

Test: .\hspace{\test}.

\end{document}

We expect to see

Test: ..
Test: .--.
Test: .------.

But the last line shows

Test: ..

It is a bit annoying because I wanted to use a floating point number to be remembered in a TikZ picture, each time I call a function in a for loop.

EDIT I want to write code for a piecewise function. Here is what I have

\documentclass[12pt]{article}
\usepackage{tikz}

\newlength{\currX}
\setlength{\currX}{0mm}
\newlength{\currY}
\setlength{\currY}{0mm}
\newcommand{\drawbar}[2]{
\draw (\currX,\currY) -- (\currX,\currY+{#2}) -- (\currX+{#1},\currY+{#2});
}
\newcommand{\drawbars}[1]{
\foreach \x/\y in {#1}{
\drawbar{\x}{\y}
}
}

\begin{document}

\begin{figure}
\begin{tikzpicture}
\drawbar{2mm}{4mm}
\drawbar{20mm}{40mm}
\end{tikzpicture}
\end{figure}

\begin{figure}
\begin{tikzpicture}
\drawbars{2mm/4mm, 20mm/40mm}
\end{tikzpicture}
\end{figure}

\end{document}

I get a compile error:

You can't use a prefix with `\begingroup'.

\begingroup

l.23     \drawbar{2mm}{4mm}

EDIT2

The second question turns out to be the same as TikZ, foreach and sum

Prefixing with global as suggested does not compile in my more specific example above, the solution above does well.

-

The commands \addtolength and related length macro adjusters are designed to behave locally. You can make it global but I'm not sure if that's the good way to go.

\documentclass{article}
\usepackage{pgffor}

\newlength{\test}
\newcommand{\incs}[1]{\foreach \x in {1,...,#1} {\global\addtolength{\test}{1cm}} }

\begin{document}
\the\test
\inc

\the\test
\setlength{\test}{0cm}

\the\test

\incs{5}
\the\test

\setlength{\test}{0cm}
\incs{3}
\the\test
\end{document}

Also you can check this question for a similar query.

For Edited Question

The following works for the edited version of your problem.

\documentclass[12pt]{article}
\usepackage{tikz}
\newlength{\currX}\setlength{\currX}{0mm}
\newlength{\currY}\setlength{\currY}{0mm}
\newcommand{\drawbar}[2]{
\draw (\currX,\currY) -- (\currX,\currY+#2) -- (\currX + #1,\currY+ #2);
}
\newcommand{\drawbars}[1]{\foreach \x/\y in #1{\drawbar{\x}{\y}}}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\node at (0,0){O};
\drawbar{2mm}{4mm}
\drawbar{20mm}{40mm}
\end{tikzpicture}
\end{figure}
\begin{figure}
\begin{tikzpicture}
\node at (0,0){O};
\drawbars{{2mm/4mm, 10mm/20mm}}
\end{tikzpicture}
\end{figure}
\end{document}

with the following output

-
Sorry, I have trouble copy pasting –  Bruno Bauwens May 5 '12 at 19:22
@BrunoBauwens Can you put this in your question please for other visitors of the site to make it more informative? –  percusse May 5 '12 at 19:22
My question is the same as tex.stackexchange.com/questions/33125/tikz-foreach-and-sum and the answer works. –  Bruno Bauwens May 5 '12 at 19:43
@BrunoBauwens If you describe your problem better it's much easier to reach to the solution. So that we don't duplicate our efforts each time you edit the question. I've changed the dimensions to make it a smaller image. –  percusse May 5 '12 at 19:59
Thank you very much for the answer! –  Bruno Bauwens May 5 '12 at 21:10