Lolcode Tutorialf
Lolcode Tutorialf
i
LOLCODE
This tutorial provides a basic level understanding of the LOLCODE programming language.
Audience
This tutorial is meant for people who want to explore beyond general boring programmi ng
syntax. Readers of this tutorial can learn the programming language in simple and easy
ways.
This tutorial will also be helpful for all those developers who want to learn the basics of
LOLCODE.
Prerequisites
The tutorial assumes that the readers have a knowhow about programming languages. If
you have worked on any other programming language, it will be easier for you to learn
LOLCODE.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected]
i
LOLCODE
Table of Contents
About the Tutorial .............................................................................................................................................................i
Audience..............................................................................................................................................................................i
Prerequisites .......................................................................................................................................................................i
Constructs ..........................................................................................................................................................................2
Whitespace ........................................................................................................................................................................3
Comments ..........................................................................................................................................................................4
File Creation.......................................................................................................................................................................4
Types ...................................................................................................................................................................................9
Booleans (TROOFS)........................................................................................................................................................ 10
Strings (YARN)................................................................................................................................................................. 11
BUKKIT ............................................................................................................................................................................. 12
5. LOLCODE – OPERATORS............................................................................................................................................... 13
ii
LOLCODE
Operators ........................................................................................................................................................................ 13
Comparison..................................................................................................................................................................... 15
Concatenation of Values............................................................................................................................................... 16
Type Casting.................................................................................................................................................................... 17
Expression Statements.................................................................................................................................................. 20
Case Statements............................................................................................................................................................. 22
iii
LOLCODE
1. LOLCODE – INTRODUCTION AND ENVIRONMENT SETUP
This chapter will make you familiar with setting up the local environment for LOLCODE,
installing it on Windows, and executing its script online at TutorialsPoint – codingground.
Please note that LOLCODE officially supports direct installation of interpreter for MAC
operating Systems only. To install LOLCODE in your operating system, you need to follow
the steps given below:
Installation on Windows
If you need to install LOLCODE on Windows operating system, please take the se steps:
First add MinGW and Python to your environment variables path. To do this, right
click on My Computer, choose Properties, then select Advanced system
settings. Select Environment Variables. In this box, select the PATH variable
and then click Edit.
Next, open the Command Prompt and navigate to the project directory using the
"cd" command, for example.
1
LOLCODE
2. LOLCODE – SYNTAX
Constructs
The LOLCODE constructs are slang words. The following table shows the alphabetical list
of constructs implemented so far:
NUMBAH (int)
DECINUMBAH (double)
WORDZ (std::string)
IM IN YR LOOP This starts an infinite loop. The only way to exit the
loop is using GTFO. Corresponds to for(;;) in other
languages
2
LOLCODE
VISIBLE <expr> This prints the argument on screen. Note that this does
not print a newline.
HAI is hi
KTHXBYE is okay, thanks, bye
BTW is by the way
OBTW is oh, by the way
TLDR is too long; didn't read
Whitespace
In most programming languages, keywords or tokens may not have spaces between them.
However, in some languages, spaces are used in tokens to differentiate them.
Comma
The comma behaves like a newline keyword in most languages, for example, \n in Java
and C. You can write many commands in a single line in LOLCODE, provided that you
separate them using a comma (,).
A comment is terminated by a newline. Please note that the line continuation (...) and (,)
after the comment (BTW) are ignored by the lci.
3
LOLCODE
Comments
Single line comments are written followed by the BTW keyword. They may occur anywhere
inside a program body: it can be at the first line of program, in between the program, in
between some line, or at the end of a program.
In LOLCODE, multiple line comments are written followed by OBTW and they are ended
with TLDR.
File Creation
A LOLCODE program begins with HAI keyword and it should end with KTHXBYE. As
LOLCODE uses shorthand language HAI basically stands for Hi and KTHXBYE can be
remembered as “Ok, thanks, bye ”.
Example
HAI 1.2
I HAS A NAME
VISIBLE "NAME::"!
GIMMEH NAME
VISIBLE "tutorialsPoint " NAME "!"
KTHXBYE
4
LOLCODE
3. LOLCODE – VARIABLES
As in any other programming language, LOLCODE allows you to define variables of various
types. This chapter will make you familiar with working with variables in LOLCODE.
Scope of Variables
The scope of a variable is local to the function or to the program block, i.e. a variable
defined in one scope cannot be called in any other scope of the same program. Variables
are accessible only after they are declared.
Naming Conventions
Variable names are usually called identifiers. Here are some of the conventions for naming
variables in LOLCODE:
Variable identifiers may be in all CAPITAL or lowercase letters (or a mixture of the
two).
They can only begin with a letter and then may be followed by other letters,
numbers, and underscores.
LOLCODE does not allow use of spaces, dashes, or other symbols while naming a
variable.
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.
A variable can contain _ or a digit anywhere inside its name, except at the
starting index. For example, name2_m is a valid name.
HAI 1.2
I HAS A food ITZ "111.00033"
I HAS A food2 ITZ "111"
I HAS A fo_od ITZ "1"
VISIBLE food
5
LOLCODE
VISIBLE food2
VISIBLE fo_od
KTHXBYE
All the declaration statement s in the above code are valid and will produce the following
output when executed:
sh-
4.3$ lci main.lo
111.00033
111
1
Some examples of invalid statement s and their output are given below:
Example 1
HAI 1.2
I HAS A 2food ITZ "111.00033"
KTHXBYE
The above code will give the following output when you execute it:
sh-
4.3$ lci main.lo
Line 2: Expected: identifier; Got: int(2).
Example 2
HAI 1.2
I HAS A _food ITZ "111.00033"
KTHXBYE
The above code will give the following output when you execute it:
sh-
4.3$ lci main.lo
Line 2: Unrecognized sequence at: _food ITZ "111.00033".
6
LOLCODE
Example 3
HAI 1.2
I HAS A f$ood ITZ "111.00033"
KTHXBYE
The above code will give the following output when you execute it:
sh-
4.3$ lci main.lo
Line 2: Unrecognized sequence at: $ood ITZ "111.00033".
I HAS A VAR BTW VAR is empty now, You can use any name instead of var
To assign the variable a value in the same statement, you can then follow the variable
name with “ITZ” and then give the value you want to assign. Use the following syntax to
assign a value to a variable:
<variable> R <expression>
Example
VAR R "Green" BTW VAR is now a YARN and equals "Green"
VAR R 30 BTW VAR is now a NUMBR and equals 30
You can also declare and assign variables at the same time using the following syntax:
Example
I HAS A NAME ITS “TUTORIALS POINT”
Example
HAI 1.2
BTW this is how we declare variables
I HAS A food
I HAS A bird
7
LOLCODE
The above program shows the declaration of variables and print s them. The output is:
sh-
4.3$ lci main.lo
1
OMG!
5
Type Casting
To convert a value of one type to another type, we use type casting. Casting a NUMBAR
to a NUMBR truncates the decimal portion of the floating point number. Casting a NUMBAR
to a YARN (by printing it, for example), truncates the output to a default 2 decimal places.
Example
HAI 1.2
I HAS A food ITZ "111.00033"
VISIBLE food
BTW this is how we do type casting
MAEK food A NUMBAR
VISIBLE food
KTHXBYE
All the variables declared in a LOLCODE program are local variables and there is no global
scope in this language for any variable.
8
LOLCODE
4. LOLCODE – TYPES
Types
Currently, the variable types in LOLCODE are:
strings (YARN)
integers (NUMBR)
floats (NUMBAR)
and 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).
The syntax for declaring and using different types in LOLCODE is shown below:
<VARIABLE> R <EXPRESSION>
Untyped (NOOB)
The untyped data type (known as NOOB), cannot be converted into any other type except
into a TROOF data type. The implicit casting of a NOOB into TROOF makes the variable
FAIL. After that any operation on a NOOB results in an error.
Explicit casts of a NOOB data type (i.e. the types that are uninitialized and do not have
any initial value) variable result s to zero values for all other types.
9
LOLCODE
To define an untyped variable, just declare a variable and assign a value as shown in this
example:
HAI 1.2
I HAS A VAR3
VAR3 R "ANYVALUE"
VISIBLE VAR3
BTW Or declare in same line
I HAS A VAR4 ITZ 44
VISIBLE VAR4
KTHXBYE
When you run the above program, you will find the following result:
sh-
4.3$ lci main.lo
ANYVALUE
44
Booleans (TROOFS)
In LOLCODE, the Boolean values are of two types. BOOLEAN generally have two values -
true and false. But, in LOLCODE, the Boolean is known as TROOF, and the true/ false
values are known as WIN/FAIL respectively. All the uninitialized values like an empty string
(""), or an empty array will all cast to FAIL. All other initialized values evaluate to WIN.
Example
HAI 1.2
I HAS A VAR3 ITZ A TROOF
VAR3 R "FAIL"
VISIBLE VAR3
KTHXBYE
You can see the following output when you execute the above code:
10
LOLCODE
Example
HAI 1.2
I HAS A VAR3 ITZ A NUMBR
VISIBLE VAR3
KTHXBYE
The above code shows you the following result when you run it:
sh-
4.3$ lci main.lo
0
Similar to NUMBR, LOLCODE has another data type, which represents a decimal or a float
in many programming languages. In LOLCODE, a NUMBAR is a float containing one decimal
point. Casting a NUMBAR to a NUMBR truncates the decimal portion of the floating point
number and returns it as a NUMBR, without any decimal.
Strings (YARN)
In LOLCODE, value containing strings, i.e. string literals (YARN) should start and end with
double quotation marks ("”).
Anything may be written inside the string, like space, comma, full stop, exc lamation or
any other symbol. A string where any single quote is missing may cause an error. Colons
are used as escape characters in LOLCODE, and any value following a colon gets a special
meaning.
Example
HAI 1.2
I HAS A VAR3 ITZ A YARN
VAR3 R "XYZ"
11
LOLCODE
VISIBLE VAR3
KTHXBYE
The code given above produces the following output upon execution:
sh-
4.3$ lci main.lo
XYZ
BUKKIT
This type represents an array. It has named slots, which can contain either variables or
functions. A BUKKIT can be declared in the following way:
A function inside a BUKKIT may also access variables and other functions of the BUKKIT
by using ME'Z [var] or ME IZ [function name] (YR [argument1] (AN YR [argument2] (AN
YR [argument3] ...))) MKAY .
Example
HAI 1.2
I HAS A VAR6 ITZ A BUKKIT
BTW DECLARING AN ARRAY
VAR6 HAS A VAR7 ITZ "DOGE"
BTW VAR7 IS A STRING VARIABLE THAT IS INSERTED INTO ARRAY VAR6
VISIBLE VAR6'Z VAR7
BTW GET THE ELEMENT OF ARRAY
KTHXBYE
This is the output you will find when you run the code given above:
sh-
4.3$ lci main.lo
DOGE
12
LOLCODE
5. LOLCODE – OPERATORS
Operators play an important role to perform various operations on variables. This chapter
brings you various operators in LOLCODE and their usage.
Operators
Mathematical operators depend on a prefix notation i.e. the notation that comes before
the operand. When all the operators have known number of arguments or operands, then
no grouping markers are necessary. In cases where operators don’t have fixed argument s
or operands, the operation is closed with MKAY.
An MKAY may not be used if it coincides with the end of the statement. In such cases, the
EOL keyword should be used. To use unary mathematical operators , use the following
syntax:
<operator> <expression>
The AN keyword can optionally be used to separate arguments, and apply a single
operation on more than one operand, so a binary operator expression has the following
syntax:
Any expression containing an operator with infinite number of arguments can be expressed
with the following syntax:
Math
Following are the basic mathematical operations in LOLCODE:
<a> and <b> can each be unique expressions in the above, so mathematical operators
can be nested and grouped indefinitely.
13
LOLCODE
Math is performed c onsidering arguments as integer math in the presence of two NUMBRs,
but if either of the expressions is NUMBAR, then operations are considered as floating
point operations.
Example
HAI 1.2
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
KTHXBYE
The above code will produce the following output when you run it:
sh-
4.3$ lci main.lo
Important Points:
Consider the following important points related to working with mathematical operators in
LOLCODE:
If one or both arguments in an expression are YARN, they are treated as NUMBARs.
14
LOLCODE
If any of the arguments cannot be safely casted internally to a numerical type, then
it fails with an error.
Boolean
Boolean operators are applied on those values that may be true or false. Boolean operators
working on TROOFs are as following:
BOTH OF <m> AN <n> BTW its and operation: WIN if m=WIN and n=WIN
EITHER OF <m> AN <n> BTW its or operation: FAIL iff m=FAIL, n=FAIL
WON OF <m> AN <n> BTW its xor operation: FAIL if m=n
NOT <m> BTW its an unary negation: WIN if m=FAIL
ALL OF <m> AN <n> ... MKAY BTW it will take infinite arguments and apply AND
ANY OF <m> AN <n> ... MKAY BTW it will take infinite arguments and apply OR.
Please note that <m> and <n> in the expression syntax above are automatically cast as
TROOF values if they are not already TROOF Values.
Comparison
When you want to compare two or more operands in LOLCODE, you can do so in any of
the following methods:
Method 1
You can c ompare two binary operands using equality operators. The syntax is shown
below:
BOTH SAEM <m> AN <n> BTW this will return WIN if m is equal to n
DIFFRINT <m> AN <n> BTW this will return WIN if m is not equal to n
Method 2
You can c ompare if both the values are of NUMBRs type. Remember that if either of the
values are NUMBARs, then they are compared as floating point values.
Method 3
You can also perform comparison using the minimum and maximum operators. The syntax
is shown below:
15
LOLCODE
Example
HAI 1.2
I HAS A VAR11 ITZ 7
BOTH SAEM VAR11 SMALLR OF VAR11 AN 8, O RLY?
YA RLY
VISIBLE "TRUE"
NO WAI
VISIBLE "FALSE"
OIC
KTHXBYE
You can see the following output when you execute the given code:
sh-
4.3$ lci main.lo
TRUE
Concatenation of Values
LOLCODE allows you to explicitly concatenate infinite number of YARNs using the
SMOOSH…MKAY operator. For concatenation, multiple arguments can be separated
with the AN operator.
Example
HAI 1.2
I HAS A VAR1 ITZ A YARN
VAR1 R "TRUE"
I HAS A VAR2 ITZ A YARN
VAR2 R "ANOTHER TRUE"
I HAS A VAR3 ITZ A YARN
VAR3 R "ONE MORE TRUE"
VISIBLE SMOOSH VAR1 " " VAR3 " " VAR2 MKAY
KTHXBYE
The above given code will produce the following result upon execution:
sh-
4.3$ lci main.lo
16
LOLCODE
Type Casting
Operators that work on specific types implicitly cast or convert the values of one type to
other type safely. If the value cannot be safely converted t o other type, then it results in
an error.
An expression's value may be explicitly casted or converted to some other type with the
binary MAEK operator. The syntax of MAEK Operator is:
To explicitly cast a variable to some other type, a normal assignment statement with
the MAEK operator can be used, or a casting assignment statement may be used as
follows:
Example
HAI 1.2
I HAS A food ITZ "111.00033"
VISIBLE food
BTW this is how we do type casting
MAEK food A NUMBAR
VISIBLE food
KTHXBYE
17
LOLCODE
6. LOLCODE – INPUT/OUTPUT
This chapter will explain you how to input a value through LOLCODE terminal and how to
output it onto the terminal.
The VISIBLE function ends or terminates by a delimiter, which is either a line end or a
comma.
The output is automatically terminated by the compiler with a carriage return. If the final
token is terminated with an exclamation symbol (!), then the carriage returned is over-
ridden by this symbol.
Please note that in LOLCODE, currently there is no defined standard for printing some data
to a file.
To take some input from the user, the keyword used is GIMMEH. It is a function which can
take any number of variables as input. It takes YARN as the input and stores the value in
any given variable.
GIMMEH <any_variable>
Example
HAI 1.2
I HAS A VAR ITZ A YARN BTW DECLARE A VARIABLE FOR LATER USE
VISIBLE "TYPE SOMETHING AND ENTER"
GIMMEH VAR BTW GET INPUT (STRING) INTO VARIABLE
VISIBLE VAR
KTHXBYE
When this code is run, it will ask you to enter a number and then prints the number back in
the next line automatically. When you run this code, it will print the following output:
sh-
4.3$ lci main.lo
18
LOLCODE
67
67
19
LOLCODE
7. LOLCODE – STATEMENTS AND FLOW CONTROL
LOLCODE allows you to control the flow of program through various statements. This
chapter explains different types of statements available in LOLCODE.
Expression Statements
An expression without any assignment, i.e. simply calling a mathematical operation or any
function, is a legal statement in LOLCODE. Once the expression is evaluated, its final value
is placed in the temporary variable IT. The value of IT remains in the local scope, and
exists until the next time it is replaced with an expression.
Assignment Statements
Assignment statements are used to assign the output of any expression to a given variable .
They are generally of the form:
Please note that, you can use a variable in the expression, even before it is being assigned.
Conditional Statements
If-Then Statements
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.
O RLY?
YA RLY
NO WAI
OIC
<any_ expression>
O RLY?
YA RLY
<code to execute if above condition is true>
NO WAI
<code to execute in this block>
20
LOLCODE
OIC
All of the above statements can be written in the same line separated by commas like:
While using the if-then statements, an optional MEBBE <any expression> may be used
between the YA RLY and NO WAI blocks.
If the <any expression> following MEBBE is True (WIN), then that block is executed.
Otherwise, if that expression is false, the block is skipped until the next MEBBE, NO WAI,
or OIC statements.
Example
<any expression>
O RLY?
YA RLY
<code to be executed if true>
MEBBE <expression>
<code to be executed mebbe is true>
MEBBE <expression>
<code to be executed mebbe is true>
NO WAI
<code to be executed if above are false>
OIC
Example
BOTH SAEM NAMES AN "NAME"
O RLY?
YA RLY, VISIBLE "YOUR NAME IS ABCD"
MEBBE BOTH SAEM ANIMAL AN "OUR NAME IS ABCD"
VISIBLE "NO ABCD"
OIC
21
LOLCODE
Case Statements
In LOLCODE, the keyword ‘WTF?’ is similar to switch in many ot her languages. The
keyword WTF? takes IT as the expression value for comparison. To use WTF, a comparison
block is opened by OMG which should be a literal, and not an expression.
Please remember that each literal must be unique, similar to the case in other languages.
The OMG block must be terminated by a GTFO statement. If an OMG block is not
terminated by a GTFO, then the next OMG block is executed till GTFO is reached.
If none of the literals evaluate as true, then default case is signified by OMGWTF.
WTF?
OMG <any value to compare>
<code block to execute if expression is satisfied>
OMG <any value to compare>
<code block to execute if expression is satisfied>
OMGWTF
<code block to execute as a default case>
OIC
NAME, WTF?
OMG "A"
VISIBLE "ABCD"
GTFO
OMG "E"
VISIBLE "EFGH"
GTFO
OMGWTF
VISIBLE "ZYXW"
OIC
"E":
EFGH
22
LOLCODE
8. LOLCODE ─ LOOPS
Loops are used in programming languages to execute a set of statements multiple times.
For example, if you want to print the digit 5 for five times, then instead of writing the
VISIBLE “5” statement five times, you can run a loop with single VISIBLE “5” statement
for five times.
Simple loops are represented with IM IN YR <label> and IM OUTTA YR <label>. Loops
defined in this way are infinite loops and they should be terminated with a GTFO break
statement.
Please note that inside the function body, UPPIN (increment by one), NERFIN (decrement
by one), or any unary function can be used.
The TIL keyword calculates the expression as a TROOF: if it evaluates as FAIL, the loop
continues once more, if it evaluates as WIN, then the loop execution stops, and continues
after the matching IM OUTTA YR statement.
The WILE keyword is the opposite of TIL keyword, if the expression is WIN, execution
continues, otherwise the loop exits.
Example
HAI 1.2
I HAS A VAR ITZ 0
IM IN YR LOOPY UPPIN YR VAR TIL BOTH SAEM VAR AN 10
VISIBLE SUM OF VAR AN 1
IM OUTTA YR LOOPY
KTHXBYE
When the above code is compiled on any LOLCODE compiler, or on our online
codingground, this will produce the following output.
sh-
4.3$ lci main.lo
23
LOLCODE
10
24
LOLCODE
9. LOLCODE – Functions
Functions are useful in programming because they reduce time and effort for writing the
code again and again. A well written function code offers high reusability. This chapter
explains you how to write and work with functions in LOLCODE.
Definition of a Function
A function is a set of statements that are executed all at once by calling that function. In
LOLCODE, a function’s definition starts with the keyword “HOW IZ I” and the closing
keyword is “IF U SAY SO”.
Important Points:
Consider the following important points when you are defining a LOLCODE function:
In LOLCODE, the function can accept only a certain fixed number of arguments as
an input.
The arguments or parameters, are the identifiers that become a variable to the
function.
Functions in LOLCODE can’t access any other values other than the values passed
to them as arguments.
If no other return statement is found, then IF U SAY SO is executed and the value
in the IT variable is returned.
25
LOLCODE
Calling Functions
A function is defined in the body of program and is later called for execution. A function
which accepts a given number of arguments is called as shown below:
While calling a function, the expression is formed by the function name , followed by the
number of arguments that the function will accept. These arguments can be simple
variables or any expressions. If a function accepts any expression instead of a simple
value, then the expressions' values are calculated before the function is called.
Please remember that the number of arguments a function will accept , should be defined
in the definition of the function.
Example
HAI
VISIBLE MAINUMBA
KTHXBYE
When you run the above code, it will ask for an input, and then when you submit the input,
you’ll see the same as the result. For example, if we enter 55, it will print 55.
Example
HAI 1.2
HOW IZ I MULTIPLY YR FIRSTOPERANT AN YR SECONDOPERANT
FOUND YR PRODUKT OF FIRSTOPERANT AN SECONDOPERANT
IF U SAY SO
VISIBLE I IZ MULTIPLY YR 2 AN YR 3
KTHXBYE
26
LOLCODE
The above function that performs multiplication of input operands will print the following
output when you run it:
sh-
4.3$ lci main.lo
Example
HAI 1.2
I HAS A STRINGARRAY ITZ A BUKKIT
STRINGARRAY HAS A VAR17 ITZ "OBJECT1"
STRINGARRAY HAS A VAR18 ITZ "OBJECT2"
HOW IZ STRINGARRAY ACCESS YR VARIABLE
FOUND YR STRINGARRAY'Z SRS VARIABLE
IF U SAY SO
I HAS A STRING ITZ "VAR17"
VISIBLE STRINGARRAY IZ ACCESS YR STRING MKAY
KTHXBYE
sh-
4.3$ lci main.lo
OBJECT1
27
LOLCODE
10. LOLCODE - EXCEPTION HANDLING
Exception handling is one of the powerful mechanisms to handle the runtime errors so that
the normal flow of the application can be maintained. LOLCODE does not have a lot of
support for exception handling like other programming Languages. Similar to the Try-
Catch block in other languages, LOLCODE has the PLZ-block.
For example, if you want to open a file that may or may not exist, use:
The code that may cause an exception is written in the PLZ block, and the exception is
handled in the O NOES block. Here, the INVISIBLE keyword sends an inner message to
the debugger.
Please note that as LOLCODE is not maintained regularly, there are no more updates
available for LOLCODE exception handling and many other features.
28
LOLCODE
11. LOLCODE – SOME MORE EXAMPLES
The previous chapters explained you the programming in LOLCODE. In this chapter, you
will learn some examples that lets you code at an advanced level in LOLCODE.
HAI 1.2
HOW IZ I POWERTWO YR NUM
BTW RETURN 1 IF 2 TO POWER OF 0
BOTH SAEM NUM AN 0, O RLY?
YA RLY, FOUND YR 1
OIC
FOUND YR TOTAL
IF U SAY SO
BTW OUTPUT: 8
VISIBLE I IZ POWERTWO YR 4 MKAY
KTHXBYE
The above code will print the following output once it runs succesfully:
sh-
4.3$ lci main.lo
16
29
LOLCODE
HAI 1.3
OBTW
CREATES A ONE DIMENSIONAL ARRAY WITH N ELEMENTS, EACH IS A 0
TLDR
HOW IZ I MAKEMATRIX YR N
I HAS A MATRIX ITZ A BUKKIT
IM IN YR LOOP UPPIN YR INDEX TIL BOTH SAEM INDEX N
MATRIX HAS A SRS INDEX ITZ 10
IM OUTTA YR LOOP
FOUND YR MATRIX
IF U SAY SO
I HAS A N ITZ 5
I HAS A MATRIX ITZ A BUKKIT
MATRIX R I IZ MAKEMATRIX YR N MKAY
KTHXBYE
You can see the following output when you execute the above code:
30
LOLCODE
HAI 1.3
HOW IZ I FACTORIAL YR N
BOTH SAEM N AN 0
O RLY?
YA RLY, FOUND YR 1
NO WAI
FOUND YR PRODUKT OF N AN I IZ FACTORIAL YR DIFF OF N AN 1
MKAY
OIC
IF U SAY SO
The above program prints the factorial of the number 6 and you can see the output as
shown below:
sh-
4.3$ lci main.lo
720
HAI 1.2
I HAS A V1
I HAS A V2
I HAS A CHOICE
VISIBLE "VALUE1"
GIMMEH V1
VISIBLE "VALUE2"
GIMMEH V2VISIBLE "Choose Operation? + - * /"
GIMMEH CHOICE CHOICE, WTF?
OMG "+"
31
LOLCODE
VISIBLE SUM OF V1 AN V2
GTFO
OMG "-"
VISIBLE DIFF OF V1 AN V2
GTFO
OMG "*"
VISIBLE PRODUKT OF V1 AN V2
GTFO
OMG "/"
VISIBLE QUOSHUNT OF V1 AN V2
GTFO
OMGWTF
VISIBLE "CHOOSE SOME OPERATION"
OIC
KTHXBYE
3
4
+
Upon execution, the above program will generate the following output:
VALUE1
VALUE2
Choose Operation? + - * /
7
32