Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: for loop
Replies: 4   Last Post: May 13, 2013 4:37 PM

 Messages: [ Previous | Next ]
 dpb Posts: 9,850 Registered: 6/7/07
Re: for loop
Posted: May 13, 2013 4:01 PM

On 5/13/2013 2:17 PM, Tarik wrote:
> probably it is not understood properly...
> my expectation is that this codes calculate n(9) for l=0 and =1(both)
> hence the result would be ans =

>>
>> 0 0 3
>> 0 3 0
>> 3 0 0

> and
>> ans =
>>
>> 1 2 2
>> 2 1 2
>> 2 2 1

>
> but here I think for loop doesn't work properly
> but where is the problem...
> I hope someone can understand and help...
> (sorry for this confusion)

...

>> my problem is that both ans are in my expectation... I mean, for loop
>> doesn't work well here... it looks that it stops after a solution...
>> but why???
>>
>> for l=0:sqrt(ni)
>> x=l;

y=real(fix(sqrt(ni-x^2)));
if fix(y)==y
>> z=real(fix(sqrt(ni-x^2-y^2)));
if fix(z)==z
if ni==x^2+y^2+z^2;
>> n=[x,y,z];
result=unique(perms(n),'rows');
return
end
>> end
>> end
>> end

Well, I've no idea what it is you're trying to describe as the result
but the reason the code will quit is the 'return' statement inside the
innermost if clause. If that IF... is ever satisfied, the RETURN will
then terminate the function at that point and (not surprisingly) return
to the calling routine or command line.

doc return

Perhaps you're looking for

doc break
doc continue

mayhaps???

--

--

Date Subject Author
5/13/13 Tarik
5/13/13 Tarik
5/13/13 dpb
5/13/13 dpb
5/13/13 Tarik