How to correctly set a WHILE loop in Julia?

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:

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])

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

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

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

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

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.

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

# 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)

# 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__

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:

while true
	# do stuff

	# decide if we should `stop_now`

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

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


Thank you both! @ mrufsvold works like a charm; @mikmoore is also a valid alternative I need to tweak with. Case solved.

1 Like