# \#loops

**URL:** https://discourse.julialang.org/tag/loops/188.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Multi-thread for nested loop with internal conditions, tricky](https://discourse.julialang.org/t/multi-thread-for-nested-loop-with-internal-conditions-tricky/135041)

<div class="topic-metadata">

**Author:** [@apolion](https://discourse.julialang.org/u/apolion)\
**Replies:** 1\
**Last updated:** [January 14, 2026, 7:09am UTC](https://discourse.julialang.org/t/multi-thread-for-nested-loop-with-internal-conditions-tricky/135041 "2026-01-14T07:09:08Z")

</div>

Hello everyone, I am computing some interactions between systems from the results of other simulations, sort of post-processing. As noted in the code, for system 1 I have 1000 simulations (variations), system 2 has 400. …

---

## [Huge performance variance with \`if\` options in loops](https://discourse.julialang.org/t/huge-performance-variance-with-if-options-in-loops/130462)

<div class="topic-metadata">

**Author:** [@hz-xiaxz](https://discourse.julialang.org/u/hz-xiaxz)\
**Replies:** 4\
**Last updated:** [July 4, 2025, 6:51am UTC](https://discourse.julialang.org/t/huge-performance-variance-with-if-options-in-loops/130462 "2025-07-04T06:51:35Z")

</div>

I’m trying to optimize a non-allocating matrix update. Here is the simple code. function update\_W\_original!( W::AbstractMatrix, l::Int, K::Int, col\_cache::AbstractVector, row\_cache::AbstractVector, )…

---

## [Why mem allocation may surge when function reads single scalar global variable?](https://discourse.julialang.org/t/why-mem-allocation-may-surge-when-function-reads-single-scalar-global-variable/128394)

<div class="topic-metadata">

**Author:** [@brownlight](https://discourse.julialang.org/u/brownlight)\
**Replies:** 4\
**Last updated:** [May 2, 2025, 7:39pm UTC](https://discourse.julialang.org/t/why-mem-allocation-may-surge-when-function-reads-single-scalar-global-variable/128394 "2025-05-02T19:39:36Z")

</div>

Two functions: pi\_1( ) sums N terms in for loop; N is defined in global scope. pi\_2(nterms) same as pi\_1( ) except that N is passed as a parameter. Output (copied from notebook cells): N = 10\_000\_000 # First run @ti…

---

## [Multiple Loops in Julia](https://discourse.julialang.org/t/multiple-loops-in-julia/127814)

<div class="topic-metadata">

**Author:** [@MarcoBPCode](https://discourse.julialang.org/u/MarcoBPCode)\
**Replies:** 7\
**Last updated:** [April 8, 2025, 7:47am UTC](https://discourse.julialang.org/t/multiple-loops-in-julia/127814 "2025-04-08T07:47:31Z")

</div>

HI, I’m writing a code that need multiples loops inside each other and i want to make parallelization it. Loop1{ Loop2{ Loop3{ Loop4{ … } } } } Can recommended some techique ? o maybe something to read o impl…

---

## [Iteration counter](https://discourse.julialang.org/t/iteration-counter/122394)

<div class="topic-metadata">

**Author:** [@mary](https://discourse.julialang.org/u/mary)\
**Replies:** 3\
**Last updated:** [November 8, 2024, 5:56pm UTC](https://discourse.julialang.org/t/iteration-counter/122394 "2024-11-08T17:56:59Z")

</div>

Hi, I want to track the load steps in my code using gridap but i got operation cell field out of this code and I cant get the values. Is this way right to implement this? Also I added this print lines to check the proce…

---

## [Macro to reorder conditional nested loop](https://discourse.julialang.org/t/macro-to-reorder-conditional-nested-loop/93588)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 4\
**Last updated:** [October 18, 2024, 5:31pm UTC](https://discourse.julialang.org/t/macro-to-reorder-conditional-nested-loop/93588 "2024-10-18T17:31:33Z")

</div>

I have a loop of the form for j=1:n for k = max(1,j-d\_u):min(j-u-1,m) f(j, k) end end and it turns out that computing the limits on k in each iteration is adding a lot of overhead. Turns ou…

---

## [Why \`for/if\` syntax is not allowed in loops](https://discourse.julialang.org/t/why-for-if-syntax-is-not-allowed-in-loops/119393)

<div class="topic-metadata">

**Author:** [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Replies:** 6\
**Last updated:** [September 20, 2024, 3:53am UTC](https://discourse.julialang.org/t/why-for-if-syntax-is-not-allowed-in-loops/119393 "2024-09-20T03:53:53Z")

</div>

Hi, I wonder why the combination of for and if is allowed in comprehension like nbe = sum(count(iseven,rv) for rv ∈ rvv if isodd(length(rv));init=0) but has to be written in two separate control flows statement in lo…

---

## [Performance of loops](https://discourse.julialang.org/t/performance-of-loops/118273)

<div class="topic-metadata">

**Author:** [@Matthijs\_1971](https://discourse.julialang.org/u/Matthijs_1971)\
**Replies:** 30\
**Last updated:** [August 19, 2024, 8:20pm UTC](https://discourse.julialang.org/t/performance-of-loops/118273 "2024-08-19T20:20:45Z")

</div>

Julia has several ways to define loops or repeat code, each with advantages and disadvantages. I am looking at the performance of some also inspired by previous posts on this forum. #!/usr/bin/env julia # https://disco…

---

## [Using @distributed for loop](https://discourse.julialang.org/t/using-distributed-for-loop/117831)

<div class="topic-metadata">

**Author:** [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Replies:** 10\
**Last updated:** [August 5, 2024, 10:26am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831 "2024-08-05T10:26:41Z")

</div>

Hello everyone, Can someone point out why following setup is not working ? Issue is that I want to copy function result in xm variable that is defined by @everywhere to not have issue with workers. But when I run loop i…

---

## [Function works correctly for single file, but not in a loop?](https://discourse.julialang.org/t/function-works-correctly-for-single-file-but-not-in-a-loop/116988)

<div class="topic-metadata">

**Author:** [@GeodeticR](https://discourse.julialang.org/u/GeodeticR)\
**Replies:** 8\
**Last updated:** [July 19, 2024, 4:57pm UTC](https://discourse.julialang.org/t/function-works-correctly-for-single-file-but-not-in-a-loop/116988 "2024-07-19T16:57:34Z")

</div>

Okay, I am at a loss here. I have this loop here to skip any data that doesn’t meet a certain threshold. But what I cannot seem to figure out is why this isn’t working in the loop I have, but works fine when I enter it …

---

## [For .... if ... syntax?](https://discourse.julialang.org/t/for-if-syntax/64559)

<div class="topic-metadata">

**Author:** [@dstarerstor](https://discourse.julialang.org/u/dstarerstor)\
**Replies:** 10\
**Last updated:** [June 3, 2024, 5:43pm UTC](https://discourse.julialang.org/t/for-if-syntax/64559 "2024-06-03T17:43:40Z")

</div>

I was just wondering what thoughts people have on the following syntax for iter if cond #stuff here end which would basically be short for for iter if cond #stuff here end end Obviously this isn’t…

---

## [Iterative Filter Loop Help:](https://discourse.julialang.org/t/iterative-filter-loop-help/114036)

<div class="topic-metadata">

**Author:** [@GeodeticR](https://discourse.julialang.org/u/GeodeticR)\
**Replies:** 3\
**Last updated:** [May 9, 2024, 7:51am UTC](https://discourse.julialang.org/t/iterative-filter-loop-help/114036 "2024-05-09T07:51:16Z")

</div>

I was able to create a single filter quickly such that every station that matched was collected and written to a file for that particular epoch. This is how I wrote it: # Individual Filter for GPS time series: c\_glob …

---

## [Storing comparisons](https://discourse.julialang.org/t/storing-comparisons/110513)

<div class="topic-metadata">

**Author:** [@Enlil50](https://discourse.julialang.org/u/Enlil50)\
**Replies:** 1\
**Last updated:** [February 21, 2024, 1:58pm UTC](https://discourse.julialang.org/t/storing-comparisons/110513 "2024-02-21T13:58:54Z")

</div>

I have a program which computes a number, stores it and uses it for many times. Then it proceeds to the next iteration (it’s a loop), generates another number, stores it and uses it as well for many times. Considering …

---

## [Bounds check outside loop affects loop performance](https://discourse.julialang.org/t/bounds-check-outside-loop-affects-loop-performance/109524)

<div class="topic-metadata">

**Author:** [@wheeheee](https://discourse.julialang.org/u/wheeheee)\
**Replies:** 0\
**Last updated:** [January 31, 2024, 4:10pm UTC](https://discourse.julialang.org/t/bounds-check-outside-loop-affects-loop-performance/109524 "2024-01-31T16:10:50Z")

</div>

While playing around with a function, I noticed that adding a bounds check (si\_check) had a drastic impact on its performance. Although for N \<= 18, there is no noticeable performance impact, and the emitted LLVM IR show…

---

## [Parallelizing a code when we are evaluating a function for different parameters independently and storing in an array](https://discourse.julialang.org/t/parallelizing-a-code-when-we-are-evaluating-a-function-for-different-parameters-independently-and-storing-in-an-array/107516)

<div class="topic-metadata">

**Author:** [@Uranium238](https://discourse.julialang.org/u/Uranium238)\
**Replies:** 6\
**Last updated:** [December 14, 2023, 9:06am UTC](https://discourse.julialang.org/t/parallelizing-a-code-when-we-are-evaluating-a-function-for-different-parameters-independently-and-storing-in-an-array/107516 "2023-12-14T09:06:10Z")

</div>

I am working on a problem where we have to evaluate a function at different values of a parameter called t, each evaluation is time consuming and involves a lot of matrix-multiplication and inversion. The rough outline o…

---

## [Recursion and while loops: pros and cons](https://discourse.julialang.org/t/recursion-and-while-loops-pros-and-cons/106559)

<div class="topic-metadata">

**Author:** [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Replies:** 4\
**Last updated:** [November 22, 2023, 9:22am UTC](https://discourse.julialang.org/t/recursion-and-while-loops-pros-and-cons/106559 "2023-11-22T09:22:46Z")

</div>

This recursive function: function foo1(x) y = someoperations(y) if somecondition(y) return y else return foo1(y) end end does essentially the same as with the loop: function foo2(x) …

---

## [The Optimization Problem with Nested Loops in Julia](https://discourse.julialang.org/t/the-optimization-problem-with-nested-loops-in-julia/104833)

<div class="topic-metadata">

**Author:** [@Umut\_Can\_Turhan](https://discourse.julialang.org/u/Umut_Can_Turhan)\
**Replies:** 8\
**Last updated:** [October 23, 2023, 8:02am UTC](https://discourse.julialang.org/t/the-optimization-problem-with-nested-loops-in-julia/104833 "2023-10-23T08:02:55Z")

</div>

Hi everyone, I have a problem related to the optimization problem with nested loops. I use QuantumOptics.jl (qojulia.org) module to calculate for some physical situation. First of all, let me share my code and explain it…

---

## [Unexpected behavior of loop over set](https://discourse.julialang.org/t/unexpected-behavior-of-loop-over-set/104371)

<div class="topic-metadata">

**Author:** [@Yly](https://discourse.julialang.org/u/Yly)\
**Replies:** 1\
**Last updated:** [September 29, 2023, 1:34am UTC](https://discourse.julialang.org/t/unexpected-behavior-of-loop-over-set/104371 "2023-09-29T01:34:09Z")

</div>

I have the following (minimized, but perhaps not minimal) code snippet which is intended to group nodes of graph components. The surprising thing is that it seems to imply that elements of a set are not in the set. ns …

---

## [How do I append row vectors into an array when I don't know the exact dimension of the array before calculations end?](https://discourse.julialang.org/t/how-do-i-append-row-vectors-into-an-array-when-i-dont-know-the-exact-dimension-of-the-array-before-calculations-end/103762)

<div class="topic-metadata">

**Author:** [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Replies:** 4\
**Last updated:** [September 11, 2023, 11:27pm UTC](https://discourse.julialang.org/t/how-do-i-append-row-vectors-into-an-array-when-i-dont-know-the-exact-dimension-of-the-array-before-calculations-end/103762 "2023-09-11T23:27:24Z")

</div>

I’m using a while statement to carry out calculations for as long as a condition holds. At every iteration, I calculate a vector v. I want to append every new v vector as a row vector to the previous v vector. The probl…

---

## [Extreme memory usage in nested loop](https://discourse.julialang.org/t/extreme-memory-usage-in-nested-loop/103471)

<div class="topic-metadata">

**Author:** [@daviyan5](https://discourse.julialang.org/u/daviyan5)\
**Replies:** 8\
**Last updated:** [September 3, 2023, 1:05pm UTC](https://discourse.julialang.org/t/extreme-memory-usage-in-nested-loop/103471 "2023-09-03T13:05:41Z")

</div>

I need to conduct extensive memory and performance testing on a memory-intensive function that creates mutable structs, temporary arrays, and more, even though most of the variables are type-defined. Running a single ins…

---

## [Combining data of different GCNs in a single file](https://discourse.julialang.org/t/combining-data-of-different-gcns-in-a-single-file/102013)

<div class="topic-metadata">

**Author:** [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Replies:** 21\
**Last updated:** [August 1, 2023, 10:33am UTC](https://discourse.julialang.org/t/combining-data-of-different-gcns-in-a-single-file/102013 "2023-08-01T10:33:42Z")

</div>

I want to create an empty table having headers :point\_right: Time(s) | Magnitude | MagErr | Filter | System | Telescope | GalExtCorr | GCN\_no. | GRB\_no. and then fill data from different telescopes GCNs in those…

---

## [Optimizing nested loops with conditional inside](https://discourse.julialang.org/t/optimizing-nested-loops-with-conditional-inside/101724)

<div class="topic-metadata">

**Author:** [@Luis\_Alberto\_R](https://discourse.julialang.org/u/Luis_Alberto_R)\
**Replies:** 10\
**Last updated:** [July 19, 2023, 3:30am UTC](https://discourse.julialang.org/t/optimizing-nested-loops-with-conditional-inside/101724 "2023-07-19T03:30:34Z")

</div>

Hello, I’m trying to build hydrodynamic codes in 1D, 2D and 3D in Julia. So far I have the codes working with first order solvers, and I’ve managed to make the 1D and 2D faster than Python and C++, now I’m trying to impl…

---

## [Return the ith iteration value of a variable in a for loop when the broken from the outside using @async](https://discourse.julialang.org/t/return-the-ith-iteration-value-of-a-variable-in-a-for-loop-when-the-broken-from-the-outside-using-async/100301)

<div class="topic-metadata">

**Author:** [@phantom](https://discourse.julialang.org/u/phantom)\
**Replies:** 7\
**Last updated:** [June 15, 2023, 1:10am UTC](https://discourse.julialang.org/t/return-the-ith-iteration-value-of-a-variable-in-a-for-loop-when-the-broken-from-the-outside-using-async/100301 "2023-06-15T01:10:13Z")

</div>

Hi sorry if I am being dense here. I would like the option of terminating a for loop from outside the function. If the loop is completed it should return the value of the variable being modified. However if the loop …

---

## [Performance scaling of broadcasting compared to looping](https://discourse.julialang.org/t/performance-scaling-of-broadcasting-compared-to-looping/92000)

<div class="topic-metadata">

**Author:** [@f.ij](https://discourse.julialang.org/u/f.ij)\
**Replies:** 19\
**Last updated:** [March 21, 2023, 11:24am UTC](https://discourse.julialang.org/t/performance-scaling-of-broadcasting-compared-to-looping/92000 "2023-03-21T11:24:56Z")

</div>

I’m writing a simulation where speed is very important. The program consists of a main loop where I need to collect some scalar by multiplying some floats based on connections in an adjacency matrix. As a very simplified…

---

## [How to skip output for \`nothing\` during ternary evaluation in one-liner list comprehension?](https://discourse.julialang.org/t/how-to-skip-output-for-nothing-during-ternary-evaluation-in-one-liner-list-comprehension/96211)

<div class="topic-metadata">

**Author:** [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Replies:** 2\
**Last updated:** [March 16, 2023, 11:15pm UTC](https://discourse.julialang.org/t/how-to-skip-output-for-nothing-during-ternary-evaluation-in-one-liner-list-comprehension/96211 "2023-03-16T23:15:49Z")

</div>

Suppose I only want to know success but not failure in a statement like: julia\> \[i%2==1 && "even" for i in 1:10\] 10-element Vector{Any}: "even" false "even" false "even" false "even" false …

---

## [How to access partitions used in previous iterations of a loop over a partitioned set?](https://discourse.julialang.org/t/how-to-access-partitions-used-in-previous-iterations-of-a-loop-over-a-partitioned-set/96127)

<div class="topic-metadata">

**Author:** [@phantom](https://discourse.julialang.org/u/phantom)\
**Replies:** 10\
**Last updated:** [March 16, 2023, 11:41am UTC](https://discourse.julialang.org/t/how-to-access-partitions-used-in-previous-iterations-of-a-loop-over-a-partitioned-set/96127 "2023-03-16T11:41:24Z")

</div>

Suppose I am looping over a partitioned set. I would like to check a condition or compare a value in the current partition to that of a partition used in a previous iteration or to be used in a future iteration. What wo…

---

## [Elegant way to initiate a loop once a condition is met and then check the condition holds every nth iteration](https://discourse.julialang.org/t/elegant-way-to-initiate-a-loop-once-a-condition-is-met-and-then-check-the-condition-holds-every-nth-iteration/95751)

<div class="topic-metadata">

**Author:** [@phantom](https://discourse.julialang.org/u/phantom)\
**Replies:** 1\
**Last updated:** [March 8, 2023, 6:05pm UTC](https://discourse.julialang.org/t/elegant-way-to-initiate-a-loop-once-a-condition-is-met-and-then-check-the-condition-holds-every-nth-iteration/95751 "2023-03-08T18:05:55Z")

</div>

Suppose I have a function that initiates a loop when a condition is met and then checks back on the condition every nth iteration. (note: Condition does NOT have to hold for iterations 2 : (n-1) ) e.g. function fun (s…

---

## [Best Way to Break out of Recursive Loop in Julia](https://discourse.julialang.org/t/best-way-to-break-out-of-recursive-loop-in-julia/95653)

<div class="topic-metadata">

**Author:** [@phantom](https://discourse.julialang.org/u/phantom)\
**Replies:** 4\
**Last updated:** [March 8, 2023, 10:07am UTC](https://discourse.julialang.org/t/best-way-to-break-out-of-recursive-loop-in-julia/95653 "2023-03-08T10:07:07Z")

</div>

This is probably not a great example of recursion but suppose I have a recursive function, usually in a loop I can break out of the first layer with something simple like function check(stuff) cont = Ref(true) @…

---

## [Best way to break out of recursive loop in Julia?](https://discourse.julialang.org/t/best-way-to-break-out-of-recursive-loop-in-julia/95652)

<div class="topic-metadata">

**Author:** [@phantom](https://discourse.julialang.org/u/phantom)\
**Replies:** 0\
**Last updated:** [March 7, 2023, 8:22am UTC](https://discourse.julialang.org/t/best-way-to-break-out-of-recursive-loop-in-julia/95652 "2023-03-07T08:22:14Z")

</div>

(topic deleted by author)

---

## [I am new in the programming world, I am starting with Julia, any suggestions?](https://discourse.julialang.org/t/i-am-new-in-the-programming-world-i-am-starting-with-julia-any-suggestions/95109)

<div class="topic-metadata">

**Author:** [@xArcoirisXx](https://discourse.julialang.org/u/xArcoirisXx)\
**Replies:** 8\
**Last updated:** [February 24, 2023, 4:21am UTC](https://discourse.julialang.org/t/i-am-new-in-the-programming-world-i-am-starting-with-julia-any-suggestions/95109 "2023-02-24T04:21:17Z")

</div>

I am new in the programming world, I am starting with Julia, any suggestions?

[Next page](https://discourse.julialang.org/tag/loops/188.md?match_all_tags=true&page=1&tags%5B%5D=loops)
