'Multiple conditions in a for loop?

I am trying to figure out what this pseudocode would be in Julia code:

Pseudocode:

for (i = 0; (i < 32) && (array[i] ≠ nil); i += 1) do
            result := merge(array[i], result)
            array[i] := nil

The multiple conditions seem to trip me up. I don't know how to format it in Julia. If anyone knows how I would appreciate it. I am new to the language.



Solution 1:[1]

There are multiple ways to do it in Julia. For example, you can use break statement

for i in 1:32          # in Julia we usually start numbering from 1
   array[i] == nothing && break
   result = merge(array[i], result)
   array[i] = nothing
end

or while loop

i = 1
while i <= 32 && array[i] != nothing
   result = merge(array[i], result)
   array[i] = nothing
   i += 1
end

Solution 2:[2]

You could do the looping and put the conditional inside:

nil = nothing  #or any other sentinel value
for i in 1:32 #for i in eachindex(array) 
  if array[i] != nil #if cond1 & cond2 & cond3... &condn
    result = merge(result, array[i]) 
    array[i] = nil
  end
end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrej Oskin
Solution 2 longemen3000