# What Does CompileSetIterate Do?

The list of compilable functions (given by CompileCompilerFunctions[] // Sort) shows one of the more interesting- and core-sounding functions: CompileSetIterate. It does not appear in the Mathematica Documentation Center. Moreover, except for references to merely the list of compilable functions, a search for CompileSetIterate on the Mathematica Stack Exchange comes up with nothing and a Google search for CompileSetIterate reveals nothing I can see relating to Mathematica.

What does CompileSetIterate do?

It sets an iterator:

cfun = Compile[{{n, _Integer}},
Table[If[x == 2, CompileSetIterate[x, 4]]; x, {x, n}]
]


Note that 2 and 3 are skipped and the entries beyond the x = n one are junk:

cfun[5]
(*  {1, 4, 5, -5, 0}  *)

cfun[8]
(*  {1, 4, 5, 6, 7, 8, 5244412656, 5244438248}  *)


You need to truncate the table to avoid the uninitialized entries. [A table of size n is preallocated, and essentially a for-loop intializes the table entries. Here CompileSetIterate short-circuits the for-loop and the initialization.]

• I would think that decreasing the iterate would cause the loop to overrun the end of the table, but no error is given and you get a table of size n. E.g. Compile[{{n, _Integer}}, Block[{go = True}, Table[If[go && x == 4, go = False; CompileSetIterate[x, 2]]; x, {x, n}] ] ][8]` Sep 16, 2021 at 17:12