Lua Crash Course part 2

# Lua crash course part 2

This part will cover operators, control flow, and functions.

Other parts of this crash course:

Let the learning begin.

# Operators

Tables were made by taking screenshots of the previews from table generators

Here are the arithmetic operators:

Unlike most languages, you cannot have += and stuff for arithmetic assignment
Here are the comparing operators:

Here are the Boolean logic operators:

# If statements

Sometimes, you want code to run only if a condition or boolean is true.

We do that in Lua (and many other programming languages) with if statements.

They end with the keyword `end`.

``````if <condition/boolean> then
-- insert code here
end``````

There's more.

You also have `elseif` for the other conditions after the original `if`.

``````if <condition/boolean> then
-- indentation is not required in Lua but makes the code more readable.
elseif <condition/boolean> then
-- try not to think about the fact that there's no end after the first if statement
end``````

You can have as many `elseif`s as you need.

But what if none of the `if` and `elseif`s are run?

We have `else` for that.

``````if <condition/boolean> then
-- code
elseif <condition/boolean> then
-- code
else
-- this code is only run if the other if and elseifs are not run
end``````

# Loops

Running code if a certain condition or boolean is true, but sometimes, you want to repeat code.

You can do that with loops.

They are blocks of code that can be repeated as many times as you need or while a condition or boolean is true.

The most basic type of loop is a `while` loop.

They execute a piece of code while a condition is true.

``````while <condition/boolean> do
-- this code is repeated while the condition or boolean is true
end``````

You can easily do an infinite loop since you have conditions or booleans for the loop.

Example for infinite loop:

``````while true do
print("INFINITE LOOP OH NOOOOOOOOOOOOOOOOOOOOOOO!!!!!!!!!!!!!!") -- this code is executed forever
end``````

However, if a condition or boolean is false when the loop starts, then the whole loop will be skipped over and none of the code in the loop will be run.

So we have a `repeat...until` loop for that. It will iterate at least once.

Here is the basic idea:

Basically the loop will iterate, then check the condition. If the condition is false, then the loop will iterate again. Otherwise, the loop is terminated.

``````repeat
-- code
until <condition/boolean>``````

Both of those are cool and all. But what if we want a more concise version of a `while` loop?

Well we have a `for` loop.

It takes a maximum of three statements.

``````for <statement executed before loop starts iterating>, <condition that is checked after each iteration>, <statement executed after each iteration> do
--code
end``````

You can also nest loops.

`break` terminates the current loop.

Have fun looping!

# Functions

Functions are an important part of Lua.

They are basically code that can be called upon multiple times.

`print` is a function. `type` is a function. You name it, a ton of thingsa are functions.

Example:

``````function sayhello()
print("hello world.")
end``````

You also have parameters, which are like local variables for a function.

``function paramExample(parameter1, parameter2)``

You can have as many as you need.

When you call a function and give values to the parameters, those are called arguments.

There is also the `return` keyword, which will allow it to be used for variables.

Example:

``````function basicAddition()
sum = 1 + 1
return sum
end``````

This returns a `number` value of 2.

And finally, you have named arguments.

I don't completely understand this, but basically, you have to put names for the arguments.

Example from Roblox Developer:

``````-- Named arguments
local function namedArguments(args)
return args.name .. "'s birthday: " .. args.dob
end

namedArguments{name="Bob", dob="4/1/2000"}``````

# AND SO ENDS PART 2 OF THIS LUA CRASH COURSE! PLEASE REMIND ME OF TYPOS AND THINGS I CAN MENTION PLEASE!!!!!!!

You are viewing a single comment. View All
DungeonMaster00 (167)

@ananab1221 lets say you call a function and you want to use it as a variable's value. well it calls the function when you assign the function call as a variable value, but when it gets to the return statement, it will assign the return value to the variable and the function stops.