If statements are useful for executing different actions depending on certain conditions. It allows doing something like "if the given value is too big, warn the user; if not, execute some code" or "if the value is zero, send an error and stop; if not, do something useful" (or some less dramatic alternatives).
Lets see an example of a common case. As you may know, we cannot divide by Zero (it does not make any sense or is infinite).
-- if.lua-- ===========-- Tell Emp that we do not intend to solve anythingauto_solve =falsea =2b =1if a ==0then-- This will fail because we want to divide by a.error("value of 'a' must be greater than zero")else-- Now that we know that a is not zero, lets divideprint(b/a) --> 0.5end
If structures can also have several conditions. In such cases, only the code after the first true condition will be executed. If none is true, the 'else' block will be executed. For instance:
-- if.lua-- ===========-- Tell Emp that we do not intend to solve anythingauto_solve =falsea =2b =1if a ==0then-- This will fail because we want to divide by a.error("value of 'a' must be greater than zero")elseif a ==1then-- Trivial result... result is bprint(b) --> 1 or whatever is the value of belseif a >99999999999999then-- Lets assume a value of Zeroprint(0) --> 0else-- Now that we know that a is not zero, lets divideprint(b/a)end
The conditions may be more complex than the ones shown above. For exampleif a ~= 1 and a ~= 2 then ...
While
The while is the first loop we will evaluate. This loop is used to run repetitive tasks until a certain condition is false.
-- while.lua-- ===========-- Tell Emp that we do not intend to solve anythingauto_solve =falsea =1while a <4doprint(a)-- increse the value of a... otherwise, -- we fall in an into an infinite loop a = a +1end-- this prints--[[123]]
The While loop is particularly useful for when you do not know how many iterations will it take for something to happen (or that is, exactly, what you want to find out). For instance
-- while.lua-- ===========-- Tell Emp that we do not intend to solve anythingauto_solve =false-- Pyramid Scams-- ==============--[[ A friend of yours wants to recruit you for a "business". The deal is that you need to pay $20 for entering, and then recruit three new people to the business. Since they have to pay, you will receive your money back, along with some profit. How many "generations" do we need for having 100,000,000 people involved?]]sum =0-- The amount of people in the scamgenerations =0population =100000000while sum < population do-- Calculate the number of people in this generation this_generation =3^generations-- add it to the the total sum = sum + this_generation-- Increase generation generations = generations +1endprint(generations) --> 18
As you can see, with a reasonable number of new people to recruit (i.e. 3), the pyramid will have included more than 100 million people in 18 generations! It actually takes 22 to grow larger than the world's population expected stable population of 9 billion. Try printing the this_generation variable to see how fast these things scale, and remember that most of the people are at the latest generations and only the few people at the first ones actually make money.