Lolcode – Discussion

Discuss Lolcode ”; Previous Next LOLCODE is an esoteric programming language inspired by the funny things on the Internet. LOLCODE is designed to test the boundaries of programming language design. This tutorial provides a basic level understanding of the LOLCODE programming language. Print Page Previous Next Advertisements ”;

Lolcode – Loops

Lolcode – Loops ”; Previous Next 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. Iteration loops have the following structure− IM IN YR <label> <any_operation> YR <any_variable> [TIL|WILE <expression>] <code block to execute inside the loop multiple times> IM OUTTA YR <label> 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 1 2 3 4 5 6 7 8 9 10 Print Page Previous Next Advertisements ”;

Lolcode – Statements & Flow Control

Lolcode – Statements and Flow Control ”; Previous Next 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 − <any_variable> <assignment operator> <any expression> 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. There are four keywords to apply the if–then statements. O RLY? YA RLY NO WAI OIC The general form is − <any_expression> O RLY? YA RLY <code to execute if above condition is true> NO WAI <code to execute in this block> OIC All of the above statements can be written in the same line separated by commas like − BOTH SAEM NAMES AN “Name”, O RLY? YA RLY, VISIBLE “My name is ABCD” NO WAI, VISIBLE “Your name is ABCD” OIC 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 Case Statements In LOLCODE, the keyword ‘WTF?’ is similar to switch in many other 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 The output results of the above code will be − “E”: EFGH Print Page Previous Next Advertisements ”;

Lolcode – Types

Lolcode – Types ”; Previous Next LOLCODE is designed to test the boundaries of the programming language design. It is an esoteric programming language inspired by the funny things on the Internet. This chapter gives you an understanding of 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 − To create a variable of any data type I HAS A <VARIABLE> ITZ A <DATA TYPE> To create a variable and assign a value to it I HAS A <VARIABLE> ITZ <EXPRESSION< To assign a value to an already created data type <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 results to zero values for all other types. 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 − sh-4.3$ lci main.lo FAIL Numerical Types (NUMBR) In LOLCODE, a NUMBR stands for an integer. Any sequence of digits is considered as a NUMBR, unless it has a decimal appearing anywhere in between the sequence. To make any number negative, it may be preceded by a hyphen (-) which signifies a negative number. 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, exclamation 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. 🙂 − A closing bracket following a colon represents a newline (n) :> − A closing angle bracket following a colon represents a tab (t) 😮 − A ‘o’ character following a colon represents a bell (beep) (g) :” − A “ following a colon represents a literal double quote (“) :: − A colon following a colon represents a single literal colon (:) Example HAI 1.2 I HAS A VAR3 ITZ A YARN VAR3 R “XYZ” 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 − BTW declaration of the BUKKIT I HAS A [object] ITZ A BUKKIT BTW creating a variable in a slots [object] HAS A [var] ITZ [value] BTW creating a function inside the BUKKIT HOW IZ [object] [function name] (YR [argument1] (AN YR [argument2] (AN YR [argument3] …))) [function code] IF U SAY SO 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 Print Page Previous Next Advertisements ”;

Lolcode – Functions

Lolcode – Functions ”; Previous Next 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”. The syntax for writing a function in LOLCODE is− HOW IZ I <function name> [YR <parameter/argument> [AN YR <other _arguments..> …]] <code block to execute / Set of statements to execute> 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. Returning Value from a Function Return in coding means something that is given back. In programming, a function can return some value to the program when its execution is completed. In LOLCODE, functions return varying values as explained below − FOUND YR <any_expression> returns the value of the expression when function block is executed completely. GTFO returns no value (NOOB), which is similar to return 0 in other programming languages like C and Java. If no other return statement is found, then IF U SAY SO is executed and the value in the IT variable is returned . 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 − I IZ <function_name> [YR <expression_One> [AN YR <expression_Two> [AN YR <expression_Three> …]]] MKAY 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 HOW DUZ I MAINUMBA I HAS A NUMBA GIMMEH NUMBA FOUND YR NUMBA IF U SAY SO 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 The above function that performs multiplication of input operands will print the following output when you run it− sh- 4.3$ lci main.lo 6 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 The output that the above code will produce is − sh- 4.3$ lci main.lo OBJECT1 Print Page Previous Next Advertisements ”;

Lolcode – Exception Handling

Lolcode – Exception Handling ”; Previous Next 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 − PLZ OPEN FILE “filename.TXT”? AWSUM THX VISIBLE FILE O NOES INVISIBLE “ERROR!” KTHX 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. Print Page Previous Next Advertisements ”;

Lolcode – Some More Examples

Lolcode – Some More Examples ”; Previous Next 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. Example 1: Program to Calculate the Power of a Number In this example, you will find the code to calculate the power of an input number. For example, 2 raised to power 4 is equal to 16. 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 BTW CALCULATE 2 TO POWER OF NUM I HAS A INDEX ITZ 0 I HAS A TOTAL ITZ 1 IM IN YR LOOP UPPIN YR INDEX TIL BOTH SAEM INDEX AN NUM TOTAL R PRODUKT OF TOTAL AN 2 IM OUTTA YR LOOP 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 Example 2: Program to Make an Array This example shows the code for making an array with five elements and each element with value 10. 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 BTW PRINTS THE CONTENTS OF THE ARRAY IM IN YR LOOP UPPIN YR INDEX TIL BOTH SAEM INDEX N VISIBLE MATRIX”Z SRS INDEX IM OUTTA YR LOOP KTHXBYE You can see the following output when you execute the above code − sh-4.3$ lci main.lo 10 10 10 10 10 Example 3: Program to Calculate the Factorial of a Number This program shows the code to calculate the factorial of an input number. 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 VISIBLE I IZ FACTORIAL YR 6 MKAY KTHXBYE 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 Example 4: Program to Design a Calculator You can design a calculator to perform basic math operations using LOLCODE programming. Observe the code given below − 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 “+” 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 When we execute the above program with following input − 3 4 + Upon execution, the above program will generate the following output − VALUE1 VALUE2 Choose Operation? + – * / 7 Print Page Previous Next Advertisements ”;

Lolcode – Useful Resources

Lolcode Useful Resources ”; Previous Next The following resources contain additional information on Lolcode. Please use them to get more in-depth knowledge on this topic. Useful Links on Lolcode Lolcode Website− Official Website of Lolcode Lolcode Wiki − Wikipedia Reference for Lolcode To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;

Lolcode – Input/Output

Lolcode – Input/Output ”; Previous Next This chapter will explain you how to input a value through LOLCODE terminal and how to output it onto the terminal. I/O from Terminal You can use the keyword VISIBLE to print something in LOLCODE. VISIBLE is a function which can take an infinite number of characters as input, and prints them all at once by internally concatenating them, and converting them to strings or YARN. 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. VISIBLE <any_expression> [<any_expression> …][!] 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 TYPE SOMETHING AND ENTER 67 67 Print Page Previous Next Advertisements ”;

Lolcode – Syntax

Lolcode – Syntax ”; Previous Next The LOLCODE constructs are slang words. The following table shows the alphabetical list of constructs implemented so far − Sr.No. Construct & Usage 1 BTW It starts a single line comment. 2 DOWN <variable>!!<times> This corresponds to variable = variable – times. Note that “times” is a wut-only language extension. 3 GIMMEH <variable> This represents the input statement. 4 GTFO This is similar to break in other languages and provides a way to break out of a loop. 5 HAI This corresponds to main () function in other languages. It is the program entry point in LOLCODE. 6 HEREZ <label> This is another wut-only language extension and declares a label for use with SHOO 7 I HAS A <type> <variable> This declares a variable of said type. There are three built-in types in LOLCODE − NUMBAH (int) DECINUMBAH (double) WORDZ (std::string) Note that types are a wut-only language extension. 8 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 9 IZ <expr1> <operator> <expr2>?: Conditional structure This is similar to if operator in other languages. Operator is one of: BIGGER THAN, SMALLER THAN, SAEM AS. Note that the ? at the end is optional. 10 KTHX It ends a block. Corresponds to } 11 KTHXBAI This ends a program 12 NOWAI This corresponds to else 13 PURR <expr> This prints argument on screen, followed by a newline. It is a wut-only language extension. 14 RELSE This corresponds to else (if) 15 SHOO This is another wut-only language extension, that corresponds to goto (the horror!) 16 UP <variable>!!<times> This corresponds to variables = variable + times. Here “times” is a wut-only language extension. 17 VISIBLE <expr> This prints the argument on screen. Note that this does not print a newline. 18 YARLY This denotes the start of the “true” conditional block Some examples of slang terms in LOLCODE are − 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 (,). Three Periods (…) The three periods (…) enables you to combine multiple lines of code into a single line or a single command by including (…) at the end of the line. This makes the compiler to treat the content of the next line as the content of previous line only. Infinite lines of code can be written together as a single command, as long as each line is ended with three periods. A comment is terminated by a newline. Please note that the line continuation (…) and (,) after the comment (BTW) are ignored by the lci. 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. All of these are valid single line comments− I HAS A VAL ITZ 19 BTW VAL = 19 I HAS A VAL ITZ 19, BTW VAL = 19 I HAS A VAL ITZ 14 BTW VAR = 14 In LOLCODE, multiple line comments are written followed by OBTW and they are ended with TLDR. This is a valid multi−line comment − I HAS A VAL ITZ 51 OBTW this is a comment No it’s a two line comment Oops no.. it has many lines here 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 Print Page Previous Next Advertisements ”;