typestar

if y elseif en Lua

Los bloques cierran con end; and/or hacen de selector de valores.

-- Blocks close with end; elseif chains without extra nesting.
local torchlight = 3

if torchlight > 6 then
  print("The hall is bright")
elseif torchlight > 2 then
  print("Shadows crowd the corners")
else
  print("Total darkness")
end

-- and/or short-circuit; the a and b or c idiom picks a value.
local hp = 4
local status = hp > 10 and "steady" or "limping"
print("Status: " .. status)
if hp < 5 then print("Find a campfire soon") end

Cómo funciona

  1. if/elseif/else encadenan sin anidar de más.
  2. and/or cortocircuitan; solo nil y false son falsos.
  3. cond and a or b es el ternario de Lua — seguro mientras a sea veraz.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
16
Caracteres a escribir
433
Tokens
74
Ritmo de tres estrellas
70 tpm

Al ritmo de tres estrellas de 70 tokens por minuto, este intento toma unos 63 segundos.

Escribe este fragmento

Paso 1 de 3 en Control de flujo; paso 4 de 27 en Fundamentos del lenguaje.

← Anterior Siguiente →