Coding for Beginners #4: Flow
You can speak a language, right?
Okay, this is where things get a little bit more interesting. Let's look at a statement.
If I can code, I can call myself a coder, otherwise I'm a civilian.
So what are you going to call me? What are you going to call my dog?
See, you already understand flow. It's inherent in having a language, and in thinking.
Code is pretty much the same:
banana = "I am a coder" if banana == "I am a coder" then print("Call me a coder!") end
There are a couple of things to note here:
== is a special operator that checks if what is on the left is the same what is on the right. This is different from =, which forces the thing on the left to equal the thing on the right. It's very easy to get these confused early on, but attempting to execute the code will give an error something like:
main.lua:3: 'then' expected near '='
It's not particularly helpful, is it? A lot of error messages are kind of unhelpful, but there's still a clue there that the error is on the third line. A general rule is that when errors occur, it's best to look at the line in the error, or the line directly above it first.
So, yes, when you want to check if something equals something else, use ==. If you want to make something equal something else, use =.
There's also that word 'end'. That is there to ensure that anything following that print("Call me a coder!") line isn't included in the 'stuff to do if the conditions are met' choice.
We can make things more complex by adding an 'else' line.
banana = "I am a coder" if banana == "I am a coder" then print("Call me a coder!") else print("Call me a civilian!") end
It works exactly like the previous code, but now provides a result when the 'if' condition isn't true.
Even with the 'if' statement, the code is still flowing straight from top to bottom. Boriiiiiiing. But it doesn't have to!
Let's look at another fun example every UK schoolboy in the '80s learned to type at stores with display computers . This will make you feel big and clever... or it did then.
for kittens = 1, 10 do print(kittens .. " I am a big stupid computer from the '80s\n") end
The output here will be.
1 - I am a big stupid computer from the '80s 2 - I am a big stupid computer from the '80s 3 - I am a big stupid computer from the '80s 4 - I am a big stupid computer from the '80s 5 - I am a big stupid computer from the '80s 6 - I am a big stupid computer from the '80s 7 - I am a big stupid computer from the '80s 8 - I am a big stupid computer from the '80s 9 - I am a big stupid computer from the '80s 10 - I am a big stupid computer from the '80s
This is an example of a loop. Loops are really useful when you're working with a number of things and want to do the same stuff to each one of them. Think about bullets in a first-person shooter. You don't want to write a separate block of code for each bullet you fire, so you write a function, and then use a loop to go through them all and do stuff to each one.
The format looks a little scary to begin with, but let's break it down.
for kittens = 1, 10 do
This sets up a temporary variable 'kittens', which then goes from 1 to 10, doing whatever is between the 'do' and 'end' each time it counts up. In this case, the line between 'do' and 'end' is:
print(kittens .. " I am a big stupid computer from the '80s\n")
You already know how 'print' works.
The only thing to note is that 'kittens' is thrown away after the 'end'. If you try printing it out after that point you'll get 'nil'.
You can put more than one line those 'do' and 'end' pieces. Let's see how that works with another example.
for i = 1, 100 do if i % 2 == 0 then print(i .. " is even") else print(i .. " is odd") end end
Try this one out, and see why a combination of loops and conditions is really powerful.
Don't worry about how I've staggered the various lines. Lua doesn't care about that at all. It's there to remind me which bits of code are inside loops and conditions.
As you've probably noticed, the word 'end' pops up again. This is a 'keyword' in Lua and again means 'hey, whatever you started, I'm finishing it'!
The format for 'if' statements is:
if thing_happens then do_things end
The format for 'for' loops is:
for start_number, end_number do stuff_you_want_done_several_times end
That's about as complicated as things are going to get for a while. You've learned about data, you've learned about the flow of operation with loops and conditions. Next time I'm going to get you to write a function of your own and show how random numbers can be fun.
As ever, if you want to try any of the code out, you can use this site.
If you have suggestions or comments, please let me know. I'll try and help as much as I can!
Leave a Reply.
Fluttermind’s director, Dene Carter, is a games industry veteran of over 25 years, and co-founder of Big Blue Box Studios, creators of the Fable franchise for the XBox and XBox 360.