Sorry, but this was only an example of using a loop variable outside the 
loop.
The real code was not that trivial -- was that not clear from the question: 
"Imagine the following function ..."?
Then I apologize for poor wording.
Wanting to know when at what point a loop breaks is a natural request in 
many numerical algorithms.


On Wednesday, May 21, 2014 12:30:50 AM UTC+2, [email protected] wrote:
>
> Also, your program is weird. My guess is that you want to give the largest 
> k such that x[1],..., x[k] are not equal to 0. If you want that, your 
> program is wrong when x does not contain any 0.
>
> The fact that if x does not contain any 0, your program return length(x) - 
> 1 is very weird to me.
>
> On Wednesday, May 21, 2014 12:18:23 AM UTC+2, [email protected] wrote:
>>
>> This method is also a bit faster as you can check with @time. I guess 
>> that it's because there is only one "if" per loop as opposed to 2 "if" per 
>> loop in your design (Are we at the end of the array ? Is it a 0.0 ?).
>>
>> François
>>
>> On Wednesday, May 21, 2014 12:09:02 AM UTC+2, [email protected] wrote:
>>>
>>> Hi,
>>>
>>> I would do a while as the loop is a conditional loop :
>>>
>>> function test(x::Vector{Float64})
>>>     i = 1
>>>     while i <= length(x) && x[i] != 0.0
>>>         i += 1
>>>     end
>>>     return i - 1
>>> end
>>>
>>> or I would use a return inside the for loop even though I prefer to 
>>> avoid a return in the middle of a function. Using a loop variable outside 
>>> the loop looks ugly to me. But it's a matter of taste.
>>>
>>

Reply via email to