Named for loops?

202 views
Skip to first unread message

Angel de Vicente

unread,
Oct 24, 2016, 6:37:17 AM10/24/16
to julia-users
Hi,

I don't see it in the documentation, but I'm wondering if there is a way
to have named nested loops, so that one can specify those names to break
and continue, in order to have more control.

In the following examples, continue always skips the remaining loop for
j, while break terminates the j loop in the first example and terminates
the single outer loop in the second (I guess it can be a bit confusing
at first, but I can understand the design).

,----
| println("nested")
| for i in 1:3
| for j in 1:6
| if 2 < j < 4 continue end
| if j > 5 break end
| @printf("i: %i j: %i \n",i,j)
| end
| end
|
| println("single outer loop")
| for i in 1:3, j in 1:6
| if 2 < j < 4 continue end
| if j > 5 break end
| @printf("i: %i j: %i \n",i,j)
| end
`----

But I'm used to Fortran, where one can do things like this (CYCLE ==
continue ; EXIT == break), and you specify the loop that the instruction
applies to.

,----
| INA: DO a = 1,1000
| INB: DO b=a+1,1000
| IF (a+b .GT. 1000) CYCLE INA
| INC: DO c=b+1,1000
| IF (a+b+c .GT. 1000) CYCLE INB
| IF (a+b+c .EQ. 1000 .AND. a**2 + b**2 .EQ. c**2) THEN
| PRINT*, a*b*c
| EXIT INA
| END IF
| END DO INC
| END DO INB
| END DO INA
`----


Is it possible now (or in the near future) to have named nested loops in
Julia?

Thanks,
--
Ángel de Vicente
http://www.iac.es/galeria/angelv/

Isaiah Norton

unread,
Oct 24, 2016, 8:05:46 AM10/24/16
to julia...@googlegroups.com
No, and I'm not aware of any plan to support this. But you can create custom control flow with '@goto', so it could be probably be done with a macro.

Angel de Vicente

unread,
Oct 24, 2016, 9:39:09 AM10/24/16
to julia...@googlegroups.com
Hi,

Isaiah Norton <isaiah...@gmail.com> writes:
> Is it possible now (or in the near future) to have named nested loops in
> Julia?
>
> No, and I'm not aware of any plan to support this. But you can create custom
> control flow with '@goto', so it could be probably be done with a macro.

Just learning Julia, so I didn't go through the macros yet. Good
motivation to keep going forward.

Milan Bouchet-Valat

unread,
Oct 24, 2016, 9:49:41 AM10/24/16
to julia...@googlegroups.com
Le lundi 24 octobre 2016 à 08:05 -0400, Isaiah Norton a écrit :
>
>
> On Monday, October 24, 2016, Angel de Vicente <angel.vicente.garrido@
A way to get out of multiple loops has actually been discussed, and the
consensus appears to be that it's a good idea:
https://github.com/JuliaLang/julia/issues/5334

Though it's not a high-priority feature since @goto works quite well.


Regards

Mauro

unread,
Oct 24, 2016, 10:51:16 AM10/24/16
to julia...@googlegroups.com
On Mon, 2016-10-24 at 15:49, Milan Bouchet-Valat <nali...@club.fr> wrote:
> Le lundi 24 octobre 2016 à 08:05 -0400, Isaiah Norton a écrit:
>>
>>
>> On Monday, October 24, 2016, Angel de Vicente <angel.vicente.garrido@
>> gmail.com> wrote:
...
>> > Is it possible now (or in the near future) to have named nested
>> > loops in
>> > Julia?
>>
>> No, and I'm not aware of any plan to support this. But youcan create
>> customcontrol flow with '@goto', so itcould be probably bedone
>> with a macro.
> A way to get out of multiple loops has actually been discussed, and the
> consensus appears to be that it's a good idea:
> https://github.com/JuliaLang/julia/issues/5334
>
> Though it's not a high-priority feature since @goto works quite well.

Interesting. Also note the macro posted in the comment:
https://github.com/JuliaLang/julia/issues/5334#issuecomment-206508859

might be just what you need.
Reply all
Reply to author
Forward
0 new messages