Student Support Forum: 'For loops within while loops' topicStudent Support Forum > General > Archives > "For loops within while loops"

 < Previous Comment Help | Reply To Comment | Reply To Topic
 Author Comment/Response Bill Simpson 02/25/13 6:47pm Problems: t was not initialized so your While never ran. Mathematica uses {} only to make lists, never to group statements the way other languages does. So I tried this modification of your code: In[1]:= tMatrix={t}; xMatrix={x}; Do[ Print["i=",i]; t=0; While[0≤t<6, Print["t=",t]; t=t+0.5; x=i+t; tMatrix=Append[tMatrix,t]; xMatrix=Append[xMatrix,x] ]; Print[xMatrix], {i,0,10,2} ] From In[1]:= i=0 From In[1]:= t=0 From In[1]:= t=0.5 From In[1]:= t=1. From In[1]:= t=1.5 From In[1]:= t=2. From In[1]:= t=2.5 From In[1]:= t=3. From In[1]:= t=3.5 From In[1]:= t=4. From In[1]:= t=4.5 From In[1]:= t=5. From In[1]:= t=5.5 From In[1]:= {0.5,0.5,1.,1.5,2.,2.5,3.,3.5,4.,4.5,5.,5.5,6.} From In[1]:= i=2 From In[1]:= t=0 From In[1]:= t=0.5 From In[1]:= t=1. From In[1]:= t=1.5 From In[1]:= t=2. From In[1]:= t=2.5 From In[1]:= t=3. From In[1]:= t=3.5 From In[1]:= t=4. From In[1]:= t=4.5 From In[1]:= t=5. From In[1]:= t=5.5 From In[1]:= {0.5,0.5,1.,1.5,2.,2.5,3.,3.5,4.,4.5,5.,5.5,6.,2.5,3.,3.5,4.,4.5,5.,5.5,6.,6.5,7.,7.5,8.} From In[1]:= i=4 From In[1]:= t=0 From In[1]:= t=0.5 From In[1]:= t=1. From In[1]:= t=1.5 From In[1]:= t=2. From In[1]:= t=2.5 From In[1]:= t=3. From In[1]:= t=3.5 From In[1]:= t=4. From In[1]:= t=4.5 From In[1]:= t=5. From In[1]:= t=5.5 From In[1]:= {0.5,0.5,1.,1.5,2.,2.5,3.,3.5,4.,4.5,5.,5.5,6.,2.5,3.,3.5,4.,4.5,5.,5.5,6.,6.5,7.,7.5,8.,4.5,5.,5.5,6.,6.5,7.,7.5,8.,8.5,9.,9.5,10.} etc. etc. etc. I'm guessing you want t initialized some other way, perhaps outside the Do, perhaps not, and I can't guess what that should be. But this should give you a start. URL: ,

 Subject (listing for 'For loops within while loops') Author Date Posted For loops within while loops Jessica 02/25/13 03:19am Re: For loops within while loops Bill Simpson 02/25/13 6:47pm
 < Previous Comment Help | Reply To Comment | Reply To Topic