Lolcode - Statements and Flow Control


Advertisements

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
Advertisements