I Language: Tutorial 1 (includes I2!)

Posted July 21, 2010 by User 1


First question to ask: what version of "I" should I use?

I 1.1 is the current stable release. It should be for those who want very few to no bugs and can deal with having to translate a bit of code to get up to date with I2 when it becomes stable. I2 is the trunk branch meaning it's in active development. This means that there may be some noticeable bugs. On the other hand, there may not. I2 is in a temporary, very weak alpha state right now meaning there is SOME testing going on but not much. Because of this, you should only use I2 if a few minor bugs won't do something disastrous (like crash a plane or something).

The Basics

The basics are mostly consistent between both 1.1 and v2. No semi-colons needed. (or allowed for that matter)

Variables are still the same without the declarations:
foo = "hello world"


You can do multiple variables at a time if you want:
foo, bar = "hello", "world"


You can also assign a bunch of variables the same value in v2:
foo, bar => "hello world"


To print out a value, use the out() function:

out(foo)
out("hello")


In v2, it's possible to print out multiple values in the out() function:

out(foo, " ", bar)
out("hello", " ", "world")


You can do the same thing, at the cost of speed in 1.1 using string concatenation -- also known as appending (valid for v2 too):

out(foo .. " " .. bar)
outln("hello" .. " " .. "world")


To print each value on a separate line, use outln():

outln(foo)
outln("hello")


To take input from a user, you need to use the io library. Input is returned by the function. This is again, universal for I. Use the appropriate one depending on the type of into you are trying to get:
foo = io.readln()


foo = io.readnum()


foo = io.readchar()


Conditional Statements

Conditional statements do different things depending on the condition you give.

if foo == "hello":
outln("`foo' is hello")
elif foo == "world":
outln("`foo' is world")
else
outln("`foo' is not hello or world")
;

Note how two equal signs are used for comparison rather than just one. Using just one will only assign "hello" to `foo' rather than check whether they are the same. If they are, the first block will be executed. If it's "world", it will skip over the first block, enter the second `elif' block (because "hello" is not the same as "world") and execute it. Otherwise, both blocks will be skipped and the third `else' block will be executed.

Note that you can have more `elif' blocks if you want. Or you can have none. It's also possible to omit the `else' block but there can be no more than one.

An alternative in I2 is to use the case/of statement (equivalent of switch/case):

case foo
of "hello"
outln("`foo' is hello")
of "world"
outln("`foo' is world")
else
outln("`foo' is not hello or world")
;

To remind myself of how unstable this is, the `else' block didn't even work until I fixed it just now after writing this.

If you are familiar with other languages, you may notice that there is no fallthrough and no break statement required. To do the equivalent of fallthrough, you need to do the following:

case foo
of "hello", "world"
outln("`foo' is either hello or world")
else
outln(`foo' is not hello or world")
;

Note the boolean operators. The `|' symbol is the equivalent of the operator OR. The other two are `&' and `!' (or for v1.1, `~') which stand for AND and NOT respectively.

Loops

In I, there are 3 types of loops. Only 2 will be covered here.

The while loop is the easiest to understand:

i = 1
while i

Go to I Language project page
More news from the I Language project.

Go to all projects' news index

Copyrighted © 2007-2023, The Caglow Project.
Material is available under AL.