File tree 1 file changed +45
-15
lines changed
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!"
17
17
local table = {1 , 2 , 3 }
18
18
local nilValue = nil
19
19
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" )
25
29
else
26
- print (" a is equal to 10 " )
30
+ print (" x is equal to y " )
27
31
end
28
32
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 )
38
36
39
37
-- Functions
40
38
function add (x , b )
93
91
if not success then
94
92
print (" Error:" , result )
95
93
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
You can’t perform that action at this time.
0 commit comments