File tree Expand file tree Collapse file tree 1 file changed +45
-15
lines changed Expand file tree Collapse file tree 1 file changed +45
-15
lines changed Original file line number Diff line number Diff line change @@ -17,24 +17,22 @@ local string = "Lua is awesome!"
1717local table = {1 , 2 , 3 }
1818local 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" )
2529else
26- print (" a is equal to 10 " )
30+ print (" x is equal to y " )
2731end
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
4038function add (x , b )
9391if not success then
9492 print (" Error:" , result )
9593end
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
You can’t perform that action at this time.
0 commit comments