Control Structures in Lua

  Control Structures in Lua


Lua has several control structures, including:


if-then-else

while loops

for loops

repeat-until loops

Here's an example of an if-then-else statement:



local x = 10


if x > 5 then

  print("x is greater than 5")

elseif x < 5 then

  print("x is less than 5")

else

  print("x is equal to 5")

end

Here's an example of a while loop:



local x = 1


while x <= 10 do

  print(x)

  x = x + 1

end

Here's an example of a for loop:



for i = 1, 10 do

  print(i)

end

Here's an example of a repeat-until loop:



local x = 1


repeat

  print(x)

  x = x + 1

until x > 10

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...