Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute Lua Online

lua

-- Calculations
-- Solve For Y Function
function fntCalY()
    if vrbInput == "X - Z = Y" or "Y + Z = X" then
        dblY = dblX - dblZ
    else if vrbInput == "X + Z = Y" or "Y - Z = X" then
        dblY = dblX + dblZ
    else if vrbInput == "Z - X = Y" or "Y + X = Z" then
        dblY = dblZ - dblX
    else if vrbInput == "Z + X = Y" or "Y - X = Z" then
        dblY = dblZ + dblX
    else
        fntError()
end
end
end
end
end -- Solve For Y Function
function fntPrintY()
    print (dblY)
end -- Print Y Function
function fntError()
    print "Error"
end -- Print Error Function

-- Get Varibles and Constants
-- Varibles
print "X = "
dblX = io.read() -- Get X
print "Z = "
dblZ = io.read() -- Get Z
print "Equation: "
vrbInput = io.read() -- Get Equation

-- Initate Functions
fntCalY()

-- Print Y
print (dblY)

Execute Lua Online

lua

sum = 1000
res = 0
for i=1, sum - 1, 1 do
    if i%3 == 0 then
        res = res + i
    elseif i%5 == 0 then
        res = res + i
    end
end
print(res)

Execute Lua Online

lua

operations = "2p3p3m6p2"
result = 0
state = 0
nextinput = 0
nextoperation = "p"
if string.len(operations)%2 == 1 then 
--if string.match(operations, "%d([dmnp]%d)*") then
for i=1,string.len(operations) do
    nextinput = string.sub(operations,i,i)
    if state == 0 then
        if string.byte(nextinput) >=48 and string.byte(nextinput) <= 57 then
           if nextoperation == "p" then
                result = result + tonumber(nextinput)
            elseif nextoperation == "m" then
                result = result - tonumber(nextinput)
            elseif nextoperation == "n" then
                result = result * tonumber(nextinput)
            elseif nextoperation == "d" then
                result = result / tonumber(nextinput)
            else
                print("Not a valid input string! (invalid arithmetical sign)")
                return
            end
            state = 1
        else
        print("Not a valid input string! (invalid number)")
        return
    end
    elseif state == 1 then
        nextoperation = nextinput
        state = 0
    end
end
print(result)
--[[else
    print("Not a valid input string! (pattern not correct)")
    return
end--]]
else
    print("Not a valid input string! (length not correct)")
    return
end

Execute Lua Online

lua

i=1
stack=0
while (i<1000) do
    if (i%3==0) or (i%5==0) then
        stack=stack+i
    end
    i=i+1
end
print(stack)

If Else If Else Statement in Lua

lua

--[ local variable definition --]
a = 100

--[ check the boolean condition --]

if( a == 10 )
then
   --[ if condition is true then print the following --]
   print("Value of a is 10" )
elseif( a == 20 )
then   
   --[ if else if condition is true --]
   print("Value of a is 20" )
elseif( a == 30 )
then
   --[ if else if condition is true  --]
   print("Value of a is 30" )
else
   --[ if none of the conditions is true --]
   print("None of the values is matching" )
end
print("Exact value of a is: ", a )

Lua Misc Operator

lua

a = "Hello "
b = "World"

print("Concatenation of string a with b is ", a..b )

print("Length of b is ",#b )

print("Length of b is ",#"Test" )

Execute Lua Online

lua

operations = "2p3p3m6p2"
result = 0
operands = {}
numbers = {}
for i in string.gmatch(operations, "[pm]") do
    operands[#operands + 1] = i
end
for i in string.gmatch(operations, "[0-9]") do
    numbers[#numbers + 1] = i
end
result = numbers[1]
for i = 1, #operands do
    if operands[i] == "p" then
        result = result + numbers[i+1]
    else
        result = result - numbers[i+1]
    end
end
print(result)

KI-Ãœbung 1

lua

--WARNING:  !!!This does NOT support multiplication and division first,               then addtion and subtraction!!!



opt1 = "2p3p3m6p2"
length = string.len(opt1)
print("Operation: " ..opt1)
--print("Stringlength = " ..length)


-- Wrong inputlength
if length < 3 then
    print("\n-----------------------------\nWarning: string to small\n-----------------------------\n")
    return
end    



-- Correct input length

--string to table
t = {}
opt1:gsub(".",function(c) table.insert(t,c) end)


-- init number
t[1] = tonumber(t[1])  


if (t[1] == nil) then
    print("\n-----------------------------\nWarning: wrong inputstream\nat the first operation\n-----------------------------\n")
    return
end    

print(t[1])



--operations
i = 2
inputs_for_next_opt = 0
res = t[1]

while t[i] ~= nil do
    inputs_for_next_opt = inputs_for_next_opt + 1
    
    if inputs_for_next_opt == 2 then
        t[i] = tonumber(t[i])   --number to calculate with
       
        if t[i] == nil then
            print("\n-----------------------------\nWarning: wrong inputstream\nat the next operation\n-----------------------------\n")
            return
        end    
        
        if t[i-1] == "p" then   --operation
            print("+" ..t[i])
            res = res + t[i]
        elseif t[i-1] == "m" then
            print("-" ..t[i])
            res = res - t[i]
        elseif t[i-1] == "d" then
            print("/"..t[i])
            res = res / t[i]
        elseif t[i-1] == "n" then
            print("*"..t[i])
            res = res * t[i]
        else
            print("\n-----------------------------\nWarning: wrong inputstream\nafter last operation!\n-----------------------------\n")
            break
        end  
      
      print("= " ..res)
      inputs_for_next_opt = 0
    end

    i = i+1
end   

print("\n-----------------------------\nResult = " ..res .."\n-----------------------------\n")

title

lua

print("Hello World!",_VERSION)

Execute Lua Online

lua

rechnung = "2p3p3m6p2"
res=0
i= string.len(rechnung)
print(i)
a = {}

for x=1, i, 1 do
a[x] = (string.sub(rechnung, x, x))
end

for n=1, i, 1 do
    if (n==1) and (a[n+1] == "p") then
        res= a[n] + a[n+2]
            
    elseif (n==1) and (a[n+1] == "m") then
        res= a[n] - a[n+2]
            
    elseif (n>1) and (a[n+1] == "p") then
        res= res + a[n+2]
            
    elseif (n>1) and (a[n+1] == "m") then
        res= res - a[n+2]

    end
end

print(res)

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.