Re: decrementing problem
- To: mathgroup at smc.vnet.net
- Subject: [mg72306] Re: decrementing problem
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Tue, 26 Dec 2006 04:42:06 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <em60nv$3ar$1@smc.vnet.net>
jltrahan at mail.usf.edu wrote: > Hi, > I am having a really difficult time with decrementing values in my For > loop. I have tried i--, --i, i-=1, i=i-1, and nothing is working. The loop > is as follows: > For[i=n-1, i<=1, i--, summ=0; As written, the outer loop is flawed since either i is greater than 1 and the body of the loop is never executed, or i is less than 1 and the body will be executed forever because it decremented. For instance, say n = 3, then i = 2, i is not less or equal than 1 therefore the loop ends here. Now, let n = 0, then i = -1, -1 is less than 1, therefore the body of the loop is executed, then i is decremented and i = -2. Again i is less than 1, the body is executed, etc. > For[j=i+1, j<=n, j++, summ=summ + U[[i,j]]*X[[j]]] > ] > When I try to print i or j, nothing shows up. > Can anyone please help? > > Thank you in advance, > Jamie > Compare with In[4]:= With[{n = 3}, For[i = n - 1, i >= 1, i--, summ = 0; Print["i: ", i]; For[j = i + 1, j <= n, j++, Print["j: ", j]; summ = summ + U[[i,j]]*X[[j]] ] ] ] From In[4]:= i: \[InvisibleSpace]2 From In[4]:= j: \[InvisibleSpace]3 From In[4]:= i: \[InvisibleSpace]1 From In[4]:= j: \[InvisibleSpace]2 From In[4]:= j: \[InvisibleSpace]3 Regards, Jean-Marc