Rob Cook


Home | GitHub | Dev.to | Donate | Contact

[4] A little bit of Lua


2026-01-06

Recall from the last lesson that our temperature converter program would fail if we didn't type in a valid number when prompted. It would be nice if, instead of failing with an error, we just recognized the input wasn't valid and prompt the user again. Well with a while loop we can do just that.

-- Celsius to Fahrenheit converter #2.

local celsius = nil

while celsius == nil do
  io.write("Enter temperature in Celsius: ")
  local raw_input = io.read()
  celsius = tonumber(raw_input)
end

local fahrenheit = (celsius * 9 / 5) + 32
io.write("The temperature in Fahrenheit is " .. fahrenheit .. "\n")

Line by line walkthrough

From now on I'll ignore comments and blank lines from these walkthroughs.

nil is special

The value nil is special in Lua. It indicates the concept of nothing, the absence of a value. Setting a variable to nil indicates to Lua that it can delete that variable if it wants to. (When this deletion happens is an internal detail of Lua, and isn't something you need to worry about).

We can explicitly set a variable to nil to remove it if we need to. The following example illustrates this.

local name = "bob"
io.write(name) -- Outputs: "bob".
name = nil
io.write(name) -- Error: expected string but found nil.

Variables that haven't been defined yet are also assumed by Lua to be nil. Lets rewrite the above snippet without defining the name variable.

io.write(name) -- Error: name has not been assigned a value yet!

while loops

As we saw in the revised temperature converter program, a while loop allows us to run (in programming terms execute) a block of lines of code repeatedly. The loop will run until the condition associated with it is no longer true. The lines contained between the do and end keywords are said to be the body of the loop.

while some_condition do
  io.write("Inside the loop.\n")
end

io.write("Outside the loop.\n")

true and false

On the subject of true and false, these are actually first class values in Lua. We can assign true and false to a variable. Such variables are called boolean variables.

local flag = true

if flag then
  io.write("Flag was true.\n")
else
  io.write("Flag was false.\n")
end

Conditionals evaluate to true or false. For example:

10 == 10          -- 10 is equal to 10:         true
"bob" ~= "alice"  -- bob is not equal to alice: true
5 > 10            -- 5 is greater than 10:      false

local age = 90
age < 50          -- age is less than 50:       false

The value nil also evaluates to false. We will look at comparison operators (==, ~=, <, >) in a future lesson.

Understanding our code

Now are programs are getting a little more complex, they can be hard to follow just by reading and running the code. Next up we will explore a feature of your IDE known as the debugger. This helps us to step through a program a line at a time, and see what values are held in our variables at any given moment.

Exercises

  1. Add a line to the very end of the temperature converter listing, that prints out the value of the variable raw_input. What output do you get? Do you know why?

Answers

  1. The variable raw_input is declared with the local keyword. The key thing to note is it is done inside the body of the while loop. Variables that are local and inside another block of code (such as a loop) are said to be scoped to that block. When the while loop ends, the variable raw_input no longer exists. If you remove the keyword local from the declaration of raw_input it becomes a global variable. It can then be accessed from anywhere in your code. As we mentioned before, global variables are generally considered to lead to bugs in larger programs. Use them with care! Prefer local variables wherever you can.
end