# Two questions about Julia

**URL:** <https://discourse.julialang.org/t/two-questions-about-julia/123957>\
**Category:** New to Julia\
**Created:** [December 18, 2024, 12:03pm UTC](https://discourse.julialang.org/t/two-questions-about-julia/123957 "2024-12-18T12:03:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 18, 2024, 12:03pm UTC](https://discourse.julialang.org/t/two-questions-about-julia/123957/1 "2024-12-18T12:03:07Z")

</div>

Hello,  
I have two questions:  
1- Why can’t I assign `''` to a variable?

```julia
julia> ch = ''
ERROR: ParseError:
# Error @ REPL[17]:1:7
ch = ''
# └ ── empty character literal
Stacktrace:
 [1] top-level scope
   @ none:1

```

2- Why doesn’t Julia have a reverse for loop?

```julia
for h = 10 : 1

```

I know that this problem can be solved as follows:

```julia
for h = last : -1 : first

```

Thank you.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 18, 2024, 12:09pm UTC](https://discourse.julialang.org/t/two-questions-about-julia/123957/2 "2024-12-18T12:09:03Z")

</div>

> [@hack3rcon](#):
>
> 1- Why can’t I assign `''` to a variable?
> 
> ```julia
> julia> ch = ''
> ERROR: ParseError:
> # Error @ REPL[17]:1:7
> ch = ''
> # └ ── empty character literal
> Stacktrace:
> [1] top-level scope
> @ none:1
> 
> ```

This isn’t about assigning to a variable. The problem is that `''` itself isn’t valid syntax. When you write `'a'` that is syntax specifically for the single **character** `a`, not a string of characters. If you want to write an empty string you can do

```julia-repl
julia> ch = ""
""

```

which creates an empty _string_. There is no definitive empty character, so `''` is made to be a syntax error instead.

> [@hack3rcon](#):
>
> 2- Why doesn’t Julia have a reverse for loop?
> 
> ```julia
> for h = 10 : 1
> 
> ```
> 
> I know that this problem can be solved as follows:
> 
> ```julia
> for h = last : -1 : first
> 
> ```
> 
> Thank you.

If you already know how to construct a reversed loop, then I’m not sure what to say. Yes, the second thing you wrote is the correct way to express a loop from `last` _down_ to `first`.

The first thing you wrote is a loop over an empty range:

```julia-repl
julia> length(10:1)
0

julia> length(10:-1:1)
10

```

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [December 18, 2024, 1:19pm UTC](https://discourse.julialang.org/t/two-questions-about-julia/123957/3 "2024-12-18T13:19:58Z")

</div>

You could also write `reverse(1:10)`, which is considered identical:

```julia
julia> reverse(1:10) == 10:-1:1
true

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 18, 2024, 1:47pm UTC](https://discourse.julialang.org/t/two-questions-about-julia/123957/4 "2024-12-18T13:47:39Z")

</div>

> [@hack3rcon](#):
>
> Why doesn’t Julia have a reverse for loop?
> 
> `for h = 10 : 1`

See [The colon punctuation: Why doesn't 5:1 return [5, 4, 3, 2, 1]?](https://discourse.julialang.org/t/the-colon-punctuation-why-doesnt-5-1-return-5-4-3-2-1/107676)

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 18, 2024, 2:00pm UTC](https://discourse.julialang.org/t/two-questions-about-julia/123957/5 "2024-12-18T14:00:01Z")

</div>

It would obviously be way too breaking, but I wonder if `10:1` should be `10:-1:1` and there should be a different way to ask for the empty range starting at 10. Our reasoning is principled but I don’t think anyone new to the language understands it.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 18, 2024, 3:04pm UTC](https://discourse.julialang.org/t/two-questions-about-julia/123957/6 "2024-12-18T15:04:14Z")

</div>

Wouldn’t this be inherently type unstable? And that would be unfortunate for something so commonly used as ranges.

Also, what languages actually returns a reverse unit range for `10:1`? Not Matlab, and not numpy.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 18, 2024, 3:53pm UTC](https://discourse.julialang.org/t/two-questions-about-julia/123957/7 "2024-12-18T15:53:55Z")

</div>

> [@DNF](#):
>
> Wouldn’t this be inherently type unstable?

Right, that was discussed in the thread I linked above.

(There’s also the issue of representing empty ranges like `5:4`, which are useful for searches etc.)

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 21, 2024, 7:50am UTC](https://discourse.julialang.org/t/two-questions-about-julia/123957/8 "2024-12-21T07:50:23Z")

</div>

Hello,  
Thank you so much for your reply.  
What is the use of empty range?

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 21, 2024, 7:51am UTC](https://discourse.julialang.org/t/two-questions-about-julia/123957/9 "2024-12-21T07:51:48Z")

</div>

Hello,  
Please take a look at this code in C++ programming language:

```julia
#include <iostream>

using namespace std;

int main() {
        for (int i = 10; i >= 1; i--) {
        cout << i << " ";
    }
    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [December 21, 2024, 8:42am UTC](https://discourse.julialang.org/t/two-questions-about-julia/123957/10 "2024-12-21T08:42:28Z")

</div>

> [@hack3rcon](#):
>
> What is the use of empty range?

The same as the use of empty strings, empty tuples, or empty vectors. They come up often when programmatically building containers in generic code.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [December 21, 2024, 10:14am UTC](https://discourse.julialang.org/t/two-questions-about-julia/123957/11 "2024-12-21T10:14:35Z")

</div>

> [@hack3rcon](#):
>
> Please take a look at this code in C++ programming language:

> [@hack3rcon](#):
>
> `for (int i = 10; i >= 1; i--) {`

That’s a semicolon loop, essentially syntactic sugar for a while loop. The syntax does not exist in many languages, which often opt instead for foreach loops. That applies to Julia. A macro can replicate the C-style loop, but I’d probably just stick to `let` and `while`.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [December 21, 2024, 10:47am UTC](https://discourse.julialang.org/t/two-questions-about-julia/123957/12 "2024-12-21T10:47:24Z")

</div>

@DNF: R reverses ranges in this fashion and I have been bitten by that several times.  
@hack3rcon: Another nice thing about empty ranges is that they are valid for indexing, i.e., the following works as expected (at least that’s what I would expected, see reversed ranges above):

```julia
dropthree(x) = x[4:end]
dropthree.([[1,2], [1,2,3], [1,2,3,4]])

```

The loop `for (int i = 10; i >= 1; i--)` in C is always explicit about the step, via `i++`, `i--` or `i+=...`, except that its hidden under a bit of boilerplate. I.e., the closest translations to Julia would be `for i in 1:1:10` for counting up or `for i in 10:-1:1` for counting down.
