Some doubts about programming

I’m a bit new to programming, so I have some doubts:
what are they for
if when then else elseif and how are they used?

2 Likes

So you’re not sure what the syntax and keywords are for the language? That’s fine. Check this site first and read the documentation about lua and its basic uses. Once you learned the basics of lua, I recommend reading the API Reference as you’re making more and more things. Overtime it will feel more natural and you will feel it as another language.

3 Likes

This all has to do with what we call “syntax”.

First of all, there is no when from my knowledge.

So first, there is something called an “if statement” this primarily uses if and then, and can include else, elseif, and others. I’ll explain this through a use-case scenario example below.

Let’s say you want to make something very simple, that adds two numbers together and makes numOne be equal to that, only if one of the numbers is greater than the other number. We’ll call the two numbers “numOne” and “numTwo”, To check if the “numOne” is greater than “numTwo”, we can use an if statement.

Extra Note: An if statement is what we call a “conditional statement.”

Here’s the code, and pay attention to the grey “comments” (we call them) that explain the code, but don’t affect it:

local numOne = 5 -- Making a local variable" called "numOne" that's equal to 5
local numTwo = 4 -- Making another "local variable" called "numTwo" that's equal to 5

if numOne > numTwo then -- Checks to see if numOne is greater than numTwo
    numOne = numOne + numTwo -- If, and only if the above is true, then it will make numOne be equal to itself plus numTwo
end -- This simply says that it is the end of the conditional statement

What will happen here, is that when you execute this program, because numOne is greater than numTwo, numOne will be equal to 9 after it completes the code.

But, what happens if we changed numTwo to 6, so that the conditional statement isn’t true? Well what will happen is nothing. So let’s make a “just in case” scenario. Let’s say that, if numOne is not greater than numTwo, then make numTwo equal itself minus numOne. How do we do this? With else.

local numOne = 5
local numTwo = 6

if numOne > numTwo then
    numOne = numOne + numTwo
else
    numTwo = numTwo - numOne -- If the above isn't true, it will do this.
end

In the above code, numTwo will become equal to 1.

Let’s do one last scenario. If we wanted to specifically do numTwo = numTwo - numOne only if numTwo is exactly equal to 6. That’s when elseif comes in. Let’s try it!

local numOne = 5
local numTwo = 6

if numOne > numTwo then
    numOne = numOne + numTwo
elseif numTwo == 6 then -- if the original conditional is incorrect, then see if numTwo == 6 is correct
    numTwo = numTwo - numOne
end

In the code above, numTwo will also equal 1.

Here’s a recap:

  • if (condition) then is the basis of a “conditional statement”. If the condition turns out correct, then it will do whatever code is between “then” and “end” (else and elseif aside).

  • else is to make the computer do something other than said above it, but only if the original conditional statement turns out to be incorrect.

  • Finally, elseif is used to “extend” the conditional statement by having another one. It basically says, if the original conditional statement turns out to be incorrect, then I want you to execute this conditional statement instead. It’s simple the extension of a conditional statement through use of another, in the form of “elseif”.

I hope this helped, and let me know if you have any questions!

4 Likes

It is pretty easy to deduce the function of a line of code just from how it is written. Most coding languages are made so that the programmer doesn’t need to write random abstract sentences of gibberish, and instead can write in a language they understand. When you hear, “If”, you probably think, “If this, then that”. Same for “When”, “When this is met, do this”. Gone are the days of hexadecimal editing and constantly writing random strings of letters and numbers.

2 Likes

Yes, no more binary, assembly code, or punch cards :joy: (for most purposes)

Now it’s much more straight forward like you said, so that most of it can be interpreted to some degree with common logic.

1 Like

Thank you very much for taking the time to explain me.

You’re welcome! In fact, I enjoy spending time explaining things, as it proves to myself that I know the subject at hand at the same time - we’re all learning here :smiley:

Let me know if you have any other questions, and I’d be happy to answer them!

3 Likes