How to Get Started with Lua
Lua is a text-based programming language, which is a lot of fun. The possibilities of this program are virtually endless. It takes a bit of time to get the hang of, but with a bit of practice making and running programs becomes addictive!...
Method 1 of 2:
Example
aBc=42 (This will make a variable called aBc and will store the number 42 i it. You can also modify the content of a variable.) A=1 A=(A+32) */This will first set A as 1, and then add 32 to it. You can even do multiplication *, division /, and subtraction -. You can also add another variable to a variable./* A=1 D=3 A=(D-A) */This will subtract A from D, and storing it in A/*
- Variables can be whatever you want, like these examples.
Troll123='i like pie' */This will store the string 'i like pie' in variable 'Troll123'/*
- There are things called strings in lua. These can be stored in variables like numbers, but they are text. All strings must have quotes 'around them.
- See these examples:
A='Some cheese' */This will store the string 'Some cheese' in the variable A. These are mainly used to tell the user things./*
- This is the main command for talking to the user in the program. you can print variables or strings.
- It will type the text or number you want onto the screen.
- For strings, all you need to do is type 'print' then a space, then the string in quotes, as in this example.
print "Hello world!"
- For variables, you need to type 'print' and then the name of the variable in brackets, as shown.
A=42 abc='Hello World!' print (abc) print (A)
- This will print whatever is in the variable. You don't have to have a set variable, either. You can print an equation, as shown.
A=3 print (A+42) */This will print the number 42+3, which is 45./*
- Halfway through a string you want to print, if you type n then it will print the rest of the text on a new line. For example:
print "Hello everyone!nHow was your day?" */will print 'Hello everyone!' On one line then 'How was your day?' On the next./*
- You need a way of letting the user enter information for the program to use. When this is in the program and the computer gets up to it, the computer will stop and not go onto anything else in the code until the user has entered in some information. This is where the user input command comes in. Here is what it looks like:
{{{1}}}
- It looks a bit complicated, but don't worry, it's not. all you have to worry about is the 'A' at the start. This is the variable in which the information is stored. It can be anything, and once it's entered you can do whatever you want with it.
D=io.stdin:read'*l' print (D) */This will let you type in whatever you want, and then it will print it out again. It stores whatever the user types in into the variable 'D', and when told to print out the variable 'D' it prints out whatever the user typed in./*
- Try to find out what this program will do, just as some practice.
print "Type in a number." n=io.stdin:read'*l' print "Now type in a second number." g=io.stdin:read'*l' print "Those two numbers added together are:" print (n+g)
-
- What does this program do and how does it work?
- What does this program do and how does it work?
- Comments are lines of code put into the program to help you know what sections of the code do what. As long as one of your lines of code starts with two dashes -- then the program will ignore whatever comes after it on that line. They are purely there to help you keep track of your program.
- For example, if you wanted to mark a section of code which is used to add two numbers together you would add this comment.
Method 2 of 2:
Calculating section
- The computer would ignore this line, as long as it starts with the two dashes.
- These are sections of code which allows you to only run that section of code if certain variables are certain things. Whenever you want in your code you can add one of these, and then enter some code into it. It will only do that code if the variables you say are equal to a certain thing. Once you have finished the code in the statement you must type a line of code which simply says 'end' without the quotes and it will tell the computer that that is the end if the code in the statement.
- on the line of code that starts with 'if' you then have a space, then the name of a variable, then what operation (== means equal to, ~= means not equal to, => means bigger than etc) then the line must end with 'then'.
A=1 if A==1 then print "Hello world!" A=0 end
- You can even have statements inside other statements, but just be sure you have the right amount of 'end's at the end. You can also have more than one variable, by saying either 'and' or 'or'.
A=1 B='cheese' if A==1 and B=='cheese' then print "Hello world!" B=1 A=737 if A=>2 or B~='cheese' then print "Hello again!" end end
- This can be useful for a variety of things, for example this. It will ask you what your favourite animal is, then says it likes that animal too. If you type in something completely different then it will say it doesn't understand you. Try to see how it works.
print "What do you prefer:ndogsncats" a=io.stdin:read'*l' if a=='dogs' then print "I like dogs too!" end if a=='cats' then print "I like cats too!" end if a~='dogs' and a~='cats' then print "I do not understand." end
- This is very similar to the 'if' statement, but instead it will keep looping through the code inside it until it is told to stop. This is how you use it.
B=0 while B<=21 do B=(B+1) print (B) end
- This program will print the numbers from 1 to 20 and then it will stop, because it got above 20. A lot of Lua programs are just massive 'while' loops with a whole lot of 'if' statements in them.
- You use this in your 'if' statement, at the end. If the variables don't match to anything on the 'if' line, then it goes onto the 'else' section.
A=42 if A==12 then print "This text shouldn't come up." else print "But this text should." end
4 ★ | 1 Vote
You should read it
- Variable type in C / C ++
- How to Code an Alert with a Variable Using Javascript
- Variable in C programming
- How to Create a Print Preview Control in Visual Basic
- Reference in C ++
- Declare variables in SQL Server
- Relax with 3 ways to print in this simple reverse order
- Safe code in C #
- Steps to disable Print Spooler on Windows 10
- How to print web pages without ads
- Variable in PHP
- How to print Excel, print Excel spreadsheets fast, standard
Maybe you are interested
What is Digital Footprint?
Excel 2016 - Lesson 12: Formatting pages and printing spreadsheets in Excel
How to print multiple Word files at once on your computer
Instructions for managing print lists on iPhone
How to print Excel on 1 A4 page - Display full content on 1 page
Instructions on how to open when Print title in Excel is locked