Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if, else, else if and end Lua

Any idea why this is wrong in Lua?

       if Pieza == 1 then
            if Rotacion == 1 then
                Piezas = Cuadrado1
            else if Rotacion == 2 then
                Piezas = Cuadrado2
            else if Rotacion == 3 then --this is Line 273
                Piezas = Cuadrado3
            else if Rotacion == 4 then
                Piezas = Cuadrado4
            else
                io.write("Me Envio una rotacion que no existe? \n");
            end 
--this end closes the if inside the first if the one using to compare Rotacion
        else if Pieza == 2 then
            if Rotacion == 1 then
                Piezas = I1
            else if Rotacion == 2 then
                Piezas = I2
            else if Rotacion == 3 then
                Piezas = I3
            else if Rotacion == 4 then
                Piezas = I4
            else
                io.write("Me Envio una rotacion que no existe? \n");
            end
--this end closes the if inside the first if the one using to compare Rotacion
        else  --this else is in case Pieza != 1 || 2
            io.write("Me Envio una pieza que no existe? \n");
        end --this close the whole if (the one comparing "Pieza")

The error I'm getting is this 'end' expected (to close 'if' at line 273) near 'else'

Also, adding end after each if (this is not what should be done but tried it anyway) is not working...

If you need me to translate to english I will gladly do it commentaries and everything but I thought it wasn't neccesary for this question.

like image 242
Makenshi Avatar asked May 02 '12 06:05

Makenshi


People also ask

How do you end an if statement in Lua?

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.

Does Lua have else if?

The usage of else if statement is optional is Lua programming language. The if.. else if.. else statement ends with the keyword end in Lua programming language. There can be any number of else if statements in a single if statement ending with else and end keyword.

What does else if mean in Lua?

1 – if then else. An if statement tests its condition and executes its then-part or its else-part accordingly. The else-part is optional.

Can you put an if statement inside an if statement Lua?

It is always legal in Lua programming to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).


1 Answers

It's elseif, not else if (notice the space). The error is because the interpreter expects an end to each else block.

See the manual for more information.

like image 131
Markus Jarderot Avatar answered Oct 17 '22 05:10

Markus Jarderot