Skip to content

Commit 930a0f9

Browse files
author
fidgetingbits
committed
Update playground with more loop types and ternary
1 parent c7ef9b5 commit 930a0f9

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

data/playground/lua/lua.lua

+45-15
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,22 @@ local string = "Lua is awesome!"
1717
local table = {1, 2, 3}
1818
local nilValue = nil
1919

20-
-- Conditional statements
21-
if a > 10 then
22-
print("a is greater than 10")
23-
elseif a < 10 then
24-
print("a is less than 10")
20+
-- Conditional Constructs
21+
local x = 10
22+
local y = 20
23+
24+
-- if-then-else
25+
if x < y then
26+
print("x is less than y")
27+
elseif x > y then
28+
print("x is greater than y")
2529
else
26-
print("a is equal to 10")
30+
print("x is equal to y")
2731
end
2832

29-
-- Loops
30-
for i = 1, 5 do
31-
print(i)
32-
end
33-
34-
while b == "Hello" do
35-
print("Still saying hello!")
36-
break
37-
end
33+
-- ternary conditional (short if-then-else)
34+
local max = x > y and x or y
35+
print("The maximum value is: " .. max)
3836

3937
-- Functions
4038
function add(x, b)
@@ -93,3 +91,35 @@ end)
9391
if not success then
9492
print("Error:", result)
9593
end
94+
95+
-- Loop Constructs
96+
-- while loop
97+
local i = 1
98+
i = 2
99+
while i <= 5 do
100+
print("While loop iteration: " .. i)
101+
i = i + 1
102+
end
103+
104+
-- repeat-until loop
105+
i = 1
106+
repeat
107+
print("Repeat-Until loop iteration: " .. i)
108+
i = i + 1
109+
until i > 5
110+
111+
-- for loop
112+
for j = 1, 5 do
113+
print("For loop iteration: " .. j)
114+
end
115+
116+
-- numeric for loop with step
117+
for k = 10, 1, -1 do
118+
print("Numeric for loop with step: " .. k)
119+
end
120+
121+
-- for-in loop (iterating over a table)
122+
local fruits = { "apple", "banana", "cherry" }
123+
for key, value in pairs(fruits) do
124+
print("For-In loop: " .. key .. " = " .. value)
125+
end

0 commit comments

Comments
 (0)