# Drawing tubes with a While loop

I'm trying to make a plot with many objects in it using a While loop.

Something like the following:

Graphics3D[
While[ϕforwhile < Pi/3,
{Lighter[Blue, .1],
{Tube[
Table[
u*{Sin[Pi/2]*Cos[ϕforwhile], Sin[Pi/2]*Sin[ϕforwhile], Cos[Pi/2]},
{u, 0, 1, 1/10}],
.005]}};
ϕforwhile = ϕforwhile + Pi/12;]]


Does anyone know how to make this work?

• The important thing to realize here is that Tube does not draw a tube. It represents a tube. In other words, Mathematica's graphics language is declarative. Thus what you need to do it not evaluate those Tubes one by one but collect them in a list, and wrap that list with Graphics3D. – Szabolcs Mar 25 '14 at 1:27

The important thing here is that While produces no output. For that you will need to use something like the Sow Reap suggested by @rasher.

I would recommend using Table here which produces output.

Graphics3D[{Lighter[Blue, .1],
Table[{Tube[
Table[u*{Sin[Pi/2]*Cos[\[Phi]forwhile],
Sin[Pi/2]*Sin[\[Phi]forwhile], Cos[Pi/2]}, {u, 0, 1,
1/10}], .005]}, {\[Phi]forwhile, 0, Pi/3, Pi/12}]}]

ϕforwhile = 0;

res = Reap[
While[ϕforwhile < Pi/3,
Sow[{Lighter[
Blue, .1], {Tube[
Table[u*{Sin[Pi/2]*Cos[ϕforwhile],
Sin[Pi/2]*Sin[ϕforwhile], Cos[Pi/2]}, {u, 0, 1,
1/10}], .005]}}]; ϕforwhile = ϕforwhile +
Pi/12;]][];

Graphics3D[res] The While on its own will not give you what you're looking for - you need to use things like Sow and Reap, or use Table, etc. While is simply a looping construct, any results are from side-effects and explicit generation like above. Also, note the initialization of the tested symbol. Without that, your initial test fails (try doing such a test on an uninitialized symbol interactively, observe the result...)