# Printing in for loop

**URL:** <https://discourse.julialang.org/t/printing-in-for-loop/42127>\
**Category:** General Usage\
**Tags:** question, juliapro\
**Created:** [June 26, 2020, 8:35pm UTC](https://discourse.julialang.org/t/printing-in-for-loop/42127 "2020-06-26T20:35:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ehamid](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@ehamid](https://discourse.julialang.org/u/ehamid)\
**Post date:** [June 26, 2020, 8:35pm UTC](https://discourse.julialang.org/t/printing-in-for-loop/42127/1 "2020-06-26T20:35:48Z")

</div>

In the following for loop, the cases i = 10,20 for d = 1 are not printed:

```julia
using LinearAlgebra, Random, Distributions
p = 1000
Σ = I(p)
X = zeros((p,p))
for d in 1:5
    println("\nd: $d")
    for i in 1:20
        if i % 10 == 0
            print(" $i, ")
        end
        X = rand(MvNormal(zeros(p), Σ), 1000)
    end
end

```

The code prints the following:

```julia
d: 1
 
d: 2
 10, 20, 
d: 3
 10, 20, 
d: 4
 10, 20, 
d: 5
 10, 20,

```

Update: It seems it’s a problem with JuliaPro. It works as expected if I copy-paste directly into the REPL, but if I use command+enter in the editor I get the strange result.

I am a bit intrigued by the behavior and would appreciate it if someone could explain the logic behind it.  
If I comment out `X = rand(MvNormal(zeros(p), Σ), 1000)` then it works as expected.  
Here’s my Julia and System info:

```julia
Julia Version 1.4.2
Commit 44fa15b150* (2020-05-23 18:35 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin18.7.0)
  CPU: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-8.0.1 (ORCJIT, goldmont)
Environment:
  JULIA_NUM_THREADS = 4
  JULIA_PKG_SERVER = pkg.juliacomputing.com

```

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [June 26, 2020, 8:58pm UTC](https://discourse.julialang.org/t/printing-in-for-loop/42127/2 "2020-06-26T20:58:20Z")

</div>

Weird. Your example works for me on Julia 1.4.2, Linux x86. Maybe someone with macOS can reproduce?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 26, 2020, 9:00pm UTC](https://discourse.julialang.org/t/printing-in-for-loop/42127/3 "2020-06-26T21:00:51Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/3/f/3fd9cad9eef8c0d4d771601e4a9e93adbdc35b9a.png)

either a Terminal related bug or OS related one

---

<div class="post-metadata">

**Author:** ![ehamid](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@ehamid](https://discourse.julialang.org/u/ehamid)\
**Post date:** [June 26, 2020, 9:18pm UTC](https://discourse.julialang.org/t/printing-in-for-loop/42127/4 "2020-06-26T21:18:55Z")

</div>

Thanks. It seems it’s a problem with JuliaPro. It works as expected if I copy-paste directly into the REPL, but if I use command+enter in the editor I get the strange result.

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [June 26, 2020, 10:08pm UTC](https://discourse.julialang.org/t/printing-in-for-loop/42127/5 "2020-06-26T22:08:19Z")

</div>

I remember having similar issues with hidden whitespace or whatever characters which messed up things when I copy pasted code from Jupyter notebooks. Someone who copied my code and pasted it in their terminal noticed it because of the missing unicode representation (they saw the block with the questionmark). My terminal simply “showed” or ignored the invisible char.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [June 26, 2020, 11:43pm UTC](https://discourse.julialang.org/t/printing-in-for-loop/42127/6 "2020-06-26T23:43:19Z")

</div>

Not answering your question, but: you are allocating `X` in each loop iteration. You need `X .= ...` instead.

---

<div class="post-metadata">

**Author:** ![ehamid](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@ehamid](https://discourse.julialang.org/u/ehamid)\
**Post date:** [June 26, 2020, 11:57pm UTC](https://discourse.julialang.org/t/printing-in-for-loop/42127/7 "2020-06-26T23:57:08Z")

</div>

Thanks, that’s a good point.
