LOLCODE ~ The Basics...
LOLCODE is an esoteric programming language inspired by the funny things on the Internet. It is designed to test the boundaries of programming language design. This tutorial provides a basic level understanding of the LOLCODE programming language.
This tutorial is meant for people who want to explore beyond general boring programming syntax. Readers of this tutorial can learn the programming language in simple and easy ways.
Programming experience recommended.
Before we get started...
To start your program, write
HAI 1.2. This means that your program is running LOLCODE verison 1.2, the newest version. End your program with
KTHXBYE. Anything written after the ending of your program will return an error upon execution.
HAI 1.2 BTW Write your code here! KTHXBYE
*IMPORTANT: Every syntax in LOLCODE is in all caps. Make sure that you write it like that, otherwise your LOLCODE program will not run.
1) Printing Statements
To print a string, you use the function
VISIBLE. Make sure that you make it ALL CAPS! (Every syntax in LOLCODE is in all caps). Put your string in double quotes.
VISIBLE "Hello World!"
Unlike other languages, LOLCODE comments start with the keyword
BTW (Slang for 'By the way'). Anything after the
BTW is ignored.
BTW The following code will not execute BTW VISIBLE "Hello World!"
Multi-line comments in LOLCODE start with the keyword
OBTW(Short for 'Oh, by the way') and end with
OBTW This is a comment Split between Multiple lines! TLDR
*The indentation isn't needed, I just use it because it makes it easier to read.
To declare a variable, use the
I HAVE A statement.
I HAVE A food BTW my variable is named 'food' VISIBLE "Yummy!"
To assign a variable a value, use the keyword
ITZ after the declaration of the variable.
I HAVE A food ITZ "Hamburgers" I HAVE A number ITZ 100 VISIBLE food VISIBLE number
*When we are giving the variable
food a value of
"Hamburgers", we use double quotes around the string, and when we are giving
number a value of
100 (an integer), we do not.
You can also assign a variable a value with the
R function ('R' stands for 'are').
I HAVE A apple apple R "Yummy" BTW apple has the value of "Yummy"
Alternatively, you can also change the value of a variable with the
R statement too.
I HAVE A number ITZ 34 BTW 'number' equals 34 number R 57 BTW 'number' now equals 57 VISIBLE number BTW Prints out 57, not 34
LOLCODE does not allow use of spaces, dashes, or other symbols while naming a variable.
I HAVE A number$ BTW Returns a error because our variable 'number$' contains '$'
Here are some of the rules for valid and invalid names for variables in LOLCODE:
- The name should always begin with an alphabet. For example, name, Name are valid.
- The name of a variable cannot begin with a digit. For example, 2var is invalid.
- The name of a variable cannot begin with a special character.
- A variable can contain _ or a digit anywhere inside its name, except at the starting index. For example, name2_m is a valid name.
Currently, the variable types in LOLCODE are:
- Strings (YARN) - Integers (NUMBR) - Floats (NUMBAR) - Booleans (TROOF) - Arrays (BUKKIT)
In LOLCODE the variable type is handled dynamically by the compiler. If a variable does not have an initial value, it is called untyped (known as NOOB in LOLCODE).
I HAVE A string ITZ "Hello World!" BTW 'string' as a value type of 'YARN'
*Boolean values in LOLCODE are
FAIL instead of
False, like in many other languages.
5) NOOB values
When a variable is declared, but does not have a value, it's value type is untyped, or NOOB.
I HAVE A variable BTW 'variable' does not have a value, so it's value type is NOOB
A variable is not a NOOB type as soon as a value is assigned to it.
I HAVE A variable BTW 'variable' has a value type of NOOB variable R 100 BTW 'variable' value type is now NUMBR
I HAVE A decimal ITZ 3.4 BTW 'decimal' immediately has a value type of NUMBAR
Following are the basic mathematical operations in LOLCODE:
SUM OF <a> AN <b> BTW This is a plus + operator
DIFF OF <a> AN <n> BTW This is a minus - operator
PRODUKT OF <a> AN <n> BTW This is a multiply * operator
QUOSHUNT OF <a> AN <n> BTW This is a divide / operator
MOD OF <a> AN <n> BTW This is a modulo % operator
BIGGR OF <a> AN <n> BTW This is a > operator
SMALLR OF <a> AN <n> BTW This is a < operator
I HAS A m ITZ 4 I HAS A n ITZ 2 VISIBLE SUM OF m AN n BTW + VISIBLE DIFF OF m AN n BTW - VISIBLE PRODUKT OF m AN n BTW * VISIBLE QUOSHUNT OF m AN n BTW / VISIBLE MOD OF m AN n BTW modulo % VISIBLE BIGGR OF m AN n BTW max VISIBLE SMALLR OF m AN n BTW min
Will produce this upon execution:
6 2 8 2 0 4 2
Here are the comparisons in LOLCODE:
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
I HAVE A number_1 ITZ 5 I HAVE A number_2 ITZ 5 OBTW BOTH SAEM number_1 AN number_2 will return WIN DIFFRINT number_1 AN number_2 will return FAIL TLDR
8) User Input
To get user input, use the
GIMMEH (Stands for 'Give me') function.
I HAVE A variable GIMMEH variable VISIBLE variable BTW Gets user input, then prints it
You must declare the variable that you are going to use for user input. If you don't, you'll get an error when you execute it.
GIMMEH number VISIBLE number BTW Returns an error because the variable 'number' is not declared
I HAVE A number GIMMEH number VISIBLE number BTW All good! :)
9) If/Else loops
The if-then statement is a very simple operation working on the IT variable. It is similar to if–else statements in other programming languages like C and Java.
There are four keywords to apply the if–then statements.
- O RLY? - YA RLY - NO WAI - OIC
The general form is:
<expression> O RLY? YA RLY <code to execute if above condition is true> NO WAI <code to execute in this block> OIC
YA RLY (Yeah Really) functions like the if block itself, and the
NO WAI (No Wait) is like the else statments. Optional
MEBBE (Maybe) blocks can be added, which functions like another
YA RLY consitional.
<expression> O RLY? YA RLY <code to execute if above condition is true> MEBBE <code to execute if above condition is true> MEBBE <code to execute if above condition is true> NO WAI <code to execute in this block> OIC
OIC statements end the conditionals. Do NOT forget them.
These conditionals only run on Booleans (TROOFS). To create a conditional using other variable types is a little more complicated. Basically,
OMG <value> is the same as checking if the expression is equal to
OMGWTF is an else. To end a statement, you must put
<expression> WTF? OMG 10 BTW This code will execute if the result of <expression> is 10 GTFO OMG 23 BTW This code will execute if the result of <expression>is 23 GTFO OMGWTF BTW This code will execute if the result of <expression> is 10 or 23 OIC
Welp, that's it! You've completely mastered LOLCODE! Be sure to leave an upvote if you've enjoyed it! :)