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:
BTW This is a one-line comment. OBTW This is a multi-line comment TLDR
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.
I HAS A VAR1 ITZ "Hi" I HAS A VAR2 ITZ 1234 I HAS A VAR3 ITZ WIN I HAS A VAR4 ITZ SMOOSH VAR1 AN VAR2 AN VAR3 MKAY VISIBLE VAR4 BTW The output will be Hi1234WIN
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:
SUM OF [variable 1] AN [variable 2] BTW This is addition DIFF OF [variable 1] AN [variable 2] BTW This is subtraction PRODUKT OF [variable 1] AN [variable 2] BTW This is multiplication QUOSHUNT OF [variable 1] AN [variable 2] BTW This is division MOD OF [variable 1] AN [variable 2] BTW This is modulus (remainder) BIGGR OF [variable 1] AN [variable 2] BTW This returns the bigger variable's value SMALLR OF [variable 1] AN [variable 2] BTW This returns the smaller variable's value
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:
BOTH OF [variable 1] AN [variable 2] BTW This is an and statement EITHER OF [variable 1] AN [variable 2] BTW This is an or statement WON OF [variable 1] AN [variable 2] BTW This is an XOR statement NOT [variable] BTW This is a not statement ALL OF [variable 1] AN ... MKAY BTW This is an infinite and statement, keep adding variable names and ANs to check more ANY OF [variable 1] AN ... MKAY BTW This is an infinite or statement, see above
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
BOTH SAEM [variable 1] AN [variable 2] BTW returns WIN if variable 1 == variable 2 DIFFRINT [variable 1] AN [variable 2] BTW returns WIN if variable 1 != variable 2 BOTH SAEM [variable 1] AN BIGGR OF [variable 1] AN [variable 2] BTW variable 1 >= variable 2 BOTH SAEM [variable 1] AN SMALLR OF [variable 1] AN [variable 2] BTW variable 1 <= variable 2 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
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:
VISIBLE "Invisible" VISIBLE INT
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:
[expression], O RLY? YA RLY BTW This code will execute if the result of [expression] is WIN NO WAI BTW This code will execute if the result of [expression] is FAIL OIC
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.
[expression], WTF? OMG 5 BTW This code will execute if the result of [expression] is 5 GTFO OMG 91 OMG 21 BTW This code will execute if the result of [expression] is 91 or 21 GTFO OMGWTF BTW This code will execute if the result of [expression] is not 5, 91, or 21 OIC
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:
I HAS A ITERATOR ITZ 0 IM IN YR LOOP UPPIN YR ITERATOR TIL BOTH SAEM ITERATOR AN 9 VISIBLE ITERATOR IM OUTTA YR LOOP BTW This will output 0 through 8, and then stop before printing 9.
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! :)