The if and break commands in Lua

1. Introduction

In previous pages in this sequence you have seen how to

This page provides you with two other Lua programming constructs to help you develop your skills further. These are the "if" statement and the "break" statement for controlling loops further.

As before, all of the programming in Lua described on this web page has analogues in just about any other standard "imperative" programming language such as C++, Java, C# etc. It should take very little time to convert what you learn about Lua to some other language.

2. The "if" statement

We have already seen expressions that evaluate to "true" or "false" when we were looking at the "while" statement. If you only want the body of the statement to execute at most once then the "if" statement is more convenient.

a = 1
b = 2
if a ~= b then
  print ("they are not equal")
end

A related version of this has an extra keyword, "else", and two blocks, one to be executed if the condition is true and the other when it is false.

a = 1
b = 2
if a == b then
  print ("they are equal")
else
  print ("they are not equal")
end

When using "if" and conditions, or even the "while" loops, you may find it helpful to be able to use "and" and "or". These words combine conditions in the way you might expect.

a = 1
b = 1
c = 2
if a == b and b == c then
  print ("they are all equal")
else
  print ("they are not all equal")
end

Exercise: try out the above. When you understand what you get, try modifying parts of it to get something different, or try the effect of replacing "and" with "or".

Exercise: What happens if you write "a == b == c"? Try to find values for a,b,c so that a==b==c comes out to be true.

One word of warning about "and" and "or": these operations work from left to right and only calculate what is actually required. So the second (rightmost) input to "and" or "or" may not even be looked at!

Consider the following.

a = 1
b = 1
c = 2
d = "string"
e = c + d -- generates an error message!
f = a == b or c + d
g = a ~= c or c + d
h = a == b and c + d
i = a == c and c + d

Exercise: As indicated by the comment, the expression "c+d" gives an error message because you cannot add a number to a string. Which of the following four lines also give(s) an error message and why? Try them out to see if you were right!

3. Controlling loops with "if" and "break"

Lua, like most lanuages of this kind, has a "break" command that jumps out of the smallest enclosing loop. Normally you use "break" with "if" to decide when to exit the loop. Here is an example that searches for an integer root of "x*x==3*x+88" between 1 and 99. It should be fairly easy to understand and a useful example to modify for your own code.

x = 1
while x < 100 do
  if (x*x == 3*x+88) then break end
  x = x+1
end
print(x)

The idiom "while true do ... end" is a loop that goes on for ever. This can be particularly helpful when there are several "if ... then break end" lines inside the loop that break the loop in a number of different places.

Exercise: Define a sequence a n starting with a 0 by the rule a n + 1 = 1 if a n = 1 , a n + 1 = a n / 2 if a n is even, and a n + 1 = 3 a n + 1 otherwise. Using a computer program, verify that for any integer value a 0 from 1 to 99 the sequence a n is eventually constant with value 1 .

The Collatz problem (an unsolved problem in mathematics) is whether or not this is true for all starting values a 0 .