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")From now on I'll ignore comments and blank lines from these walkthroughs.
Introduce a local variable called celsius and set it to nil.
Begin a while loop. The lines between do and end will be run repeatedly,
until the conditional (celsius == nil) is no longer true.
Prompt for the temperature.
Read a line of input from the keyboard.
Convert the contents of the variable raw_input to a number, assign the result
to the variable celsius.
End the while loop.
Calculate the temperature in Fahrenheit and assign it to the variable
fahrenheit.
Output the temperature in Fahrenheit.
nil is specialThe 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 loopsAs 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 falseOn 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.
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.
raw_input. What output do you get? Do you know
why?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.