# How to correctly set a WHILE loop in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-correctly-set-a-while-loop-in-julia/103379>\
**Category:** New to Julia\
**Created:** [August 30, 2023, 4:50pm UTC](https://discourse.julialang.org/t/how-to-correctly-set-a-while-loop-in-julia/103379 "2023-08-30T16:50:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [August 30, 2023, 4:50pm UTC](https://discourse.julialang.org/t/how-to-correctly-set-a-while-loop-in-julia/103379/1 "2023-08-30T16:50:35Z")

</div>

Hello,  
I would like to set a loop to get information from the user from console. The loop should run until the user says otherwise.  
I have prepared this script:

```julia
ANS = "N"

while ANS == "N"
    # get data
    println("Type a name")
    Name = readline()
    println("Type a number")
    q_num = readline()
    num = parse(Float16, q_num)
    println("Enter new values?")
    ans = readline()
    global ANS = uppercase(ans[1])
end

```

and saved in the the executable `test,jl`. But when I launch it:

```julia
$ julia test.jl
Type a name
Tony
Type a number
5
Enter new values?
yes
$

$ julia test.jl
Type a name
Jean
Type a number
8
Enter new values?
no

```

After typing `yes` (or `Y`, `y`, `YES`), I should have got into another loop.  
What am I getting wrong?  
Thank you

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [August 30, 2023, 4:54pm UTC](https://discourse.julialang.org/t/how-to-correctly-set-a-while-loop-in-julia/103379/2 "2023-08-30T16:54:08Z")

</div>

Your loop is checking if `ANS == "N"`. If you enter “Y” then that check is false and breaks the loop. You should check if it is “Y” and also start by assigning “Y” to `ANS`.

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [August 30, 2023, 6:22pm UTC](https://discourse.julialang.org/t/how-to-correctly-set-a-while-loop-in-julia/103379/3 "2023-08-30T18:22:50Z")

</div>

I’m back at my computer now, so I can write something more thorough. I rewrote your MWE and annotated my reasoning:

```julia
# best to wrap code in a function because:
# 1. It let's Julia compile it to make it fast (doesn't matter too much in this case. It's
# just a good practice).
# 2. You can reuse it in other code 
function main()
    ans = "y"
    while ans == "y"
        # get data
        println("Type a name")
        name = readline() # variables in Julia are usually snake_case

        println("Type a number")
        q_num = readline()
        num = parse(Float64, q_num) # Float16 is a pretty uncommon datatype, 
            # usually used for machine learning

        println("Enter new values? (y,n)") # let user know what their options are
        ans = lowercase(readline())

        # check to make sure user input has some characters and is something you expect
        # Right now, this will just error and end the program, you'd want to have some kind of 
        # retry mechanism in real life.
        @assert length(ans) > 0 && first(ans) in ("y", "n") "Response must be \"y\" or \"n\""

        ans = first(ans)
    end
end

# This will check if the current file is being run as a script and kick off main
# Otherwise, nothing runs. This allows you to `include()` this file in another file and not accidentally
# kick off the while loop
if abspath(PROGRAM_FILE) == @ __FILE__
    main()
end

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [August 30, 2023, 7:10pm UTC](https://discourse.julialang.org/t/how-to-correctly-set-a-while-loop-in-julia/103379/4 "2023-08-30T19:10:09Z")

</div>

Since you want this loop to run at least once, it seems that this is a better fit for a `do-while` pattern. This involves checking the exit condition at the end of the loop, rather than the beginning (which is essentially equivalent except for the first time through the loop). Julia doesn’t have a literal `do-while` keyword pair, but you can write your own like this:

```julia
while true
	
	# do stuff

	# decide if we should `stop_now`

	stop_now && break
	#=
	# the above line is equivalent to this:
	if stop_now
		break
	end
	=#
end

```

This may make it a little cleaner than needing to initialize some variable just so that it can pass the first stop check.

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [August 31, 2023, 5:52am UTC](https://discourse.julialang.org/t/how-to-correctly-set-a-while-loop-in-julia/103379/5 "2023-08-31T05:52:49Z")

</div>

Thank you both! @ [mrufsvold](https://discourse.julialang.org/u/mrufsvold) works like a charm; @mikmoore is also a valid alternative I need to tweak with. Case solved.
