How to Get Started with Lua

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/* 
  1. How to Get Started with Lua Picture 1
    Variables can be whatever you want, like these examples.
Troll123='i like pie' */This will store the string 'i like pie' in variable 'Troll123'/* 
  1. How to Get Started with Lua Picture 2
    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.
  2. How to Get Started with Lua Picture 3
    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./* 
  1. How to Get Started with Lua Picture 4
    This is the main command for talking to the user in the program. you can print variables or strings.
  2. How to Get Started with Lua Picture 5
    It will type the text or number you want onto the screen.
  3. How to Get Started with Lua Picture 6
    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!" 
  1. How to Get Started with Lua Picture 7
    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) 
  1. How to Get Started with Lua Picture 8
    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./* 
  1. How to Get Started with Lua Picture 9
    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./* 
  1. How to Get Started with Lua Picture 10
    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}}}

  1. How to Get Started with Lua Picture 11
    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./* 
  1. How to Get Started with Lua Picture 12
    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) 
    1. What does this program do and how does it work?
      How to Get Started with Lua Picture 13
  1. How to Get Started with Lua Picture 14
    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.
  2. How to Get Started with Lua Picture 15
    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

  1. How to Get Started with Lua Picture 16
    The computer would ignore this line, as long as it starts with the two dashes.
  2. How to Get Started with Lua Picture 17
    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.
  3. How to Get Started with Lua Picture 18
    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 
  1. How to Get Started with Lua Picture 19
    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 
  1. How to Get Started with Lua Picture 20
    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 
  1. How to Get Started with Lua Picture 21
    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 
  1. How to Get Started with Lua Picture 22
    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.
  2. How to Get Started with Lua Picture 23
    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

May be interested

  • Apple Store started to order iPhone 6Apple Store started to order iPhone 6
    in the evening of september 12 (in vietnamese time), apple's app store online store has started offering pre-orders to iphone 6 duo.
  • Among Us started introducing the feature of reporting violationsAmong Us started introducing the feature of reporting violations
    after a period of rapid development, innersloth has introduced a function for players to 'report' you play in among us.
  • People know how to lie since they were 2 and the truth is unexpectedPeople know how to lie since they were 2 and the truth is unexpected
    according to experts, lying is a natural state of human beings, even when we started talking about when we were 2 years old.
  • Da Vinci Eye - Paint portraits like a professional artistDa Vinci Eye - Paint portraits like a professional artist
    today, tipsmake.com.com will cover how to use the da vinci eye, what devices it's available on, and other useful information to help you get started.
  • How to Use a ComputerHow to Use a Computer
    there's a lot you can do with a computer, and if you're just getting started it can seem pretty daunting. luckily, computers have gotten simpler over the years, and you can be up and running in just a few minutes. from setting up your new...
  • HP began selling ultra-thin Pavilion dv2 and dv3HP began selling ultra-thin Pavilion dv2 and dv3
    hp has started bringing two new laptops to the market at an attractive price, slim design and running on amd processors
  • How to Set Up an Arduino UnoHow to Set Up an Arduino Uno
    so, you've just bought, or are considering buying, an arduino uno kit. your mind is probably full of questions about how you can get started right away maximizing your experience. nothing is stopping you from coding the next big thing on...
  • Apple is testing iOS 13Apple is testing iOS 13
    according to information from macrumors, in recent months, some apple devices have started to update ios 13.
  • How to Learn Web DesignHow to Learn Web Design
    with the development of so many programming, style, and markup languages, learning web design is becoming more complicated than ever. fortunately, there are tons of tools available to help you get started. look for a few basic resources,...
  • How to Use the InternetHow to Use the Internet
    using the internet is a vital thing for this century. however, some people don't know how to use the web. to learn many of the ways that you can use the web, just get started with step 1 below. use email. email is a lot like regular mail...