A Crash Course in LOLCODE 🐱
A Crash Course in LOLCODE
OHAI! This is a crash course in the beautiful programming language known as LOLCODE! LOLCODE is a great language, and while it is similar to many other programming languages, trying to learn it may confuse some people because of the syntax. For this tutorial, any time I say
[variable], as long as I'm not talking about initializing a variable, you can put a value instead.
Creating a new program
To begin a program, you need to have the line
HAI 1.2. This will declare that it's a LOLCODE program that's written in LOLCODE 1.2. The last line in the program must be
There are two different ways of doing commenting -
OBTW. The differences are shown below:
The indentation is not necessary, but it makes it easier to read.
Variables are dynamically typed in LOLCODE, so you don't have to give them a type when declared. To declare a variable, use
I HAS A [variable]. However, if you do want to give it a type, you can use
I HAS A [variable] ITZ A [type]. There are 4 usable types of variables:
- TROOF (a boolean -
FAIL, corresponding to true or false.)
- NUMBR (an integer - whole numbers)
- NUMBAR (a float - decimal places)
- YARN (a string - text, defined by "")
Variable names are case-sensitive, so INT is different from Int. You can use capital and lowercase letters, underscores, and numbers - as long as neither underscores nor numbers begin the variable name. To assign one variable to another, use
[variable 1] R [variable 2].
It's very simple to concatenate YARNS in LOLCODE - you use
SMOOSH [variables to concatenate, seperated by AN] MKAY. It will cast any input given to it to a YARN before concatenating. See below for an example.
There are a couple different ways of casting a variable from 1 type to another. The first is
MAEK [variable] A [type]. This will attempt to cast from whatever type the variable is to the desired type. However, this will not work if it's illogical - for instance, trying to cast letters into a NUMBR or NUMBAR. To cast a variable to a different type and save the output in a different variable, use
[variable 1] R MAEK [variable 2] A [type].
There are 3 different types of expressions in LOLCODE - Math, Boolean, and Comparison. The basic form for all expressions is either
[expression] [variable] or
[expression] [variable1] AN [variable2].
In LOLCODE, you have all the typical expression types - addition, subtraction, multiplication, division, and modulus (remainder), as well as some less-common ones - min (returns the value of the smaller of 2 variables) and max (returns the value of the larger of 2 variables). If either variable is a YARN and has a decimal, it is cast to a NUMBAR for the calculation. If it doesn't have a decimal, it's cast to a NUMBR. If both variables used are NUMBRs, then integer math is performed. If one or both are NUMBARS, floating floating point math is invoked. See below for a list of math expressions:
The boolean expressions work pretty much as you would expect, comparing WINs and FAILs. You can use and, or, xor, and not. Any value passed to this is cast to a TROOF. See below for the complete list:
This is very similar to boolean expressions - it takes in 2 variables and checks if they're either the same or different. However, it doesn't have to be 2 TROOFS, but they do have to be the same type. "9" and 9 will NOT be recognized as the same. See below for examples
To output text, you have to use the
VISIBLE [output] command. This can also be used with variables by using
VISIBLE [variable]. See below for examples:
To get input from the user, you can use
GIMMEH [variable]. For this, you MUST specify a variable because that is where the output is stored. GIMMEH stores input as a YARN, so if you want to get a NUMBR or NUMBAR you have to cast it as such.
Creating conditionals is fairly straightforward in LOLCODE. There are 2 basic formats - one utilizing TROOFs, and one utilizing other types of variables. To create a conditional using TROOFs, use the following:
To create a conditional using other variable types is a little more involved. Basically,
OMG [value] is the same as checking if the expression is equal to [value], and
OMGWTF is an else. To end a statement, you must put GTFO.
Loops are a somewhat confusing beast at first, but actually aren't that hard. First, you need
IM IN YR [label for the loop - I would recommend just calling it LOOP]. Then, if you want to increase the iterator variable have
UPPIN YR [variable], and if you want to decrease the iterator variable have
NERFIN YR [variable]. Finally, if you want to go until a certain value, use
TIL [expression], and if you want to go while a certain expression is true, use
WILE [expression]. To end the loop, use
IM OUTTA YR [label]. See below for an example:
Aaaand that's pretty much everything I could possibly find on the internet about LOLCODE... There is documentation for functions and BUKKITs (arrays), but I couldn't get them to work and so I decided against detailing them. If you still want MOAR LOLCODE documentation, go here, here, or here. If you want a fairly simple random python with turtle spiral generator, go check out my tutorial for that here. If you want a super long tutorial about how to make your own game using only python with turtle graphics, go here. If you liked this tutorial, feel free to leave an upvote. Thanks! :)
ITS DA TWOOF
DIFFRINT [variable 1] AN BIGGR OF [variable 1] AN [variable 2] BTW variable 1 > variable 2
DIFFRINT [variable 1] AN SMALLR OF [variable 1] AN [variable 2] BTW variable 1 < variable 2
I think it's the other way around:
(a != max(a, b) --> a < b)
(a != min(a, b) --> a > b)