English 中文(简体)
Lolcode - Variables
  • 时间:2024-12-22

Lolcode - Variables


Previous Page Next Page  

As in any other programming language, LOLCODE allows you to define variables of various types. This chapter will make you famipar 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.

Please note that there is no global scope of variables in LOLCODE.

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.

    Variable identifiers are case sensitive.

Here are some of the rules for vapd and invapd names for variables in LOLCODE−

    The name should always begin with an alphabet. For example, name, Name are vapd.

    The name of a variable cannot begin with a digit. For example, 2var is invapd.

    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 vapd name.

Some examples of vapd names in LOLCODE are shown below −


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
VISIBLE food2
VISIBLE fo_od
KTHXBYE

All the declaration statements in the above code are vapd and will produce the following output when executed −


sh-4.3$ lci main.lo
111.00033
111
1

Some examples of invapd statements 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".

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".

Declaration and Assignment of Variables

To declare a variable, LOLCODE provides a keyword “I HAS A” which is followed by the variable name. You can find below the syntax for declaring a variable.


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−


I HAS A VAR ITZ VALUE

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
BTW this is how we assign variables
food R 1
bird R 5
BTW this is how initiapze variables
I HAS A biz ITZ "OMG!"
VISIBLE food
VISIBLE biz
VISIBLE bird
KTHXBYE

The above program shows the declaration of variables and prints 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

The above pne of code will produce the following output −


sh-4.3$ lci main.lo
111.00033
111.00033

All the variables declared in a LOLCODE program are local variables and there is no global scope in this language for any variable.

Advertisements