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

Something like the following:

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

Does anyone know how to make this work?

  • 2
    $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Mar 25, 2014 at 1:27

2 Answers 2


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], 
        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, 
        Blue, .1], {Tube[
            Sin[Pi/2]*Sin[ϕforwhile], Cos[Pi/2]}, {u, 0, 1, 
           1/10}], .005]}}]; ϕforwhile = ϕforwhile + 


enter image description here

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...)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.