# Terminating a while loop on an empty-line input

**URL:** https://discourse.julialang.org/t/terminating-a-while-loop-on-an-empty-line-input/122715
**Category:** New to Julia
**Created:** [November 16, 2024, 12:26pm UTC](https://discourse.julialang.org/t/terminating-a-while-loop-on-an-empty-line-input/122715 "2024-11-16T12:26:14Z")
**Posts on this page:** 6
**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: [November 16, 2024, 12:26pm UTC](https://discourse.julialang.org/t/terminating-a-while-loop-on-an-empty-line-input/122715/1 "2024-11-16T12:26:14Z")

</div>

Hello,  
I want this program to take some numbers from the input and print their sum. This happens until the Enter key is pressed:

```julia
function numbers()
    flag = 0
    sum = 0
    while flag != ""
        print("Enter the number: ")
        flag = parse(Int,readline())
        sum += flag
    end
    println("The sum is $sum")
end
numbers()

```

Because the flag becomes the code of the Enter key, the program does not work properly. Should I use the ASCII code of the Enter key?

Thank you.

---

<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: [November 16, 2024, 1:06pm UTC](https://discourse.julialang.org/t/terminating-a-while-loop-on-an-empty-line-input/122715/2 "2024-11-16T13:06:22Z")

</div>

I just asked Perplexity to write that code for me. Here it is:

```julia
function sum_of_integers()
    total_sum = 0
    
    while true
        print("Enter a number: ")
        input = readline(stdin)
        
        if isempty(input)
            println("Terminating the loop.")
            break
        elseif isdigit(input[1])
            num = parse(Int, input)
            total_sum += num
            println("Current sum: $total_sum")
        else
            println("Please enter only a number.")
        end
    end
    
    println("The total sum of entered numbers is: $total_sum")
end

```

---

<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: [November 16, 2024, 2:27pm UTC](https://discourse.julialang.org/t/terminating-a-while-loop-on-an-empty-line-input/122715/3 "2024-11-16T14:27:39Z")

</div>

> [@hack3rcon](#):
>
> `flag = parse(Int,readline())`

One answer: just store the `readline()` result in a variable, and then check it before trying to parse it. i.e. change your loop body after the `println` to:

```julia
line = readline()
line == "" && break # halt loop on an empty input
sum += parse(Int, line)

```

Of course, in this case you can replace the `while flag != ""` condition with `while true`, since that check is always `true` anyway — the exit condition is instead in the body of the loop. And you no longer need the `flag` variable.

This doesn’t have any error checking (e.g. it doesn’t check for an input that is not a number — it will throw an exception in that case), but you could make the parsing more robust in any number of ways, e.g. by using `tryparse` instead of `parse`.

> [@hack3rcon](#):
>
> Should I use the ASCII code of the Enter key?

The ASCII code of `\n` is 10 (0x0a). Do you really want to stop whenever the user enters “10”?

Or if you are asking how to access the ASCII codes that were entered, these are all accessible, but are in the string returned by `readline()`, but you have no way to access this if you pass it directly to `parse` rather than storing it in a variable.

(This is a classic example of an [XY problem](https://en.wikipedia.org/wiki/XY_problem) — you have a basic logic bug in your program in which you were trying to parse a string before checking its contents, but you confused yourself by instead focusing on ASCII codes.)

> [@hendri54](#):
>
> I just asked Perplexity to write that code for me. Here it is:

In general, AI-generated content is discouraged. People are on this forum to talk to humans.

The AI-generated answer doesn’t contain any explanations, contains lots of extraneous changes that distract from understanding what was wrong with the original code, and is not particularly good code anyway (e.g. checking whether the first character of the line is a digit is not a robust way to check for numeric input — it has both false negatives and false positives).

---

<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: [November 17, 2024, 7:29am UTC](https://discourse.julialang.org/t/terminating-a-while-loop-on-an-empty-line-input/122715/4 "2024-11-17T07:29:37Z")

</div>

Hello,  
I changed my code as follow:

```julia
function numbers()
    flag = 0
    sum = 0
    print("Enter the number: ")
    while ((flag = readline()) != "")
        print("Enter the number: ")
        sum += parse(Int,flag)
    end
    println("The summer is $sum")
end
numbers()

```

The only problem is that I have used two `print` commands to print the message.

---

<div class="post-metadata">

### Author: ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)
#### Post date: [November 17, 2024, 8:40am UTC](https://discourse.julialang.org/t/terminating-a-while-loop-on-an-empty-line-input/122715/5 "2024-11-17T08:40:53Z")

</div>

You can just break out of a `while true` loop, as already suggested by @stevengj above.

```julia
function numbers()
    sum = 0
    while true
        print("Enter the number: ")
        line = readline()
        line == "" && break
        sum += parse(Int, line)
    end
    println("The sum is $sum")
end

```

Or if you don’t like `break` statements:

```julia
function numbers()
    sum = 0
    stop = false
    while !stop
        print("Enter the number: ")
        line = readline()
        stop = isempty(line)
        stop || (sum += parse(Int, line)) # (i.e. if !stop ... end)
    end
    println("The sum is $sum")
end

```

---

<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: [November 17, 2024, 9:39am UTC](https://discourse.julialang.org/t/terminating-a-while-loop-on-an-empty-line-input/122715/6 "2024-11-17T09:39:37Z")

</div>

Hi,  
Thank you so much.
