Storing and manipulating data

The programs we wrote so far are missing a fundamental point in computing. Actually, it is all about processing data through specific instructions. In the buggle world, the main data are a bit hidden behind the graphical representation, but that's no reason to never manipulate some data explicitly.

Data in [!thelang]

In a program, you can use several types of data, such as integers or strings of chars. If you want to use a data several times, you need to store it within a variable, which is a memory cell containing a value: you put your data (say the value '5') in the variable (say 'length'), and you can retrieve it latter when you need it. That's very similar to a box of label 'gift' in which you would put some stuff, like a bottle of perfume "Channel N°5".

Variable declarations

Declaring (ie, creating) a variable in [!thelang], is very simple. You just need to write [!java]its type, a space, and the variable name.[/!] [!scala]the var keyword, the variable name, a column (:) and the variable type.[/!] [!python]the variable name, an equal sign (=) and an initial value.[/!] The variable name is the label to retrieve it afterward[!python].[/!] [!java|scala] while the type is the kind of data that this variable accepts to store.[/!] It is forbidden to use spaces in variable names. So you can name a variable stepAmount if you want, but step amount is not a valid name.

[!java|scala]

So, to create a variable named x intended to contain integers, one should write:

[!java]int x;[/!][!scala]var x: Int[/!]
[/!]

[!java|scala]If you want, you can specify the initial value of the variable by adding a equal sign (=) followed by the value after the declaration.[/!] [!scala]If you specify a value, you don't have to specify the type of the variable, since Scala manages to guess it alone in most cases. You are still free to specify the type if you prefer (or if the compiler fail to determine the type for some reason), but it's optional.[/!] So you want that the variable contains 5 as initial value, you should type:

[!java]int x=5;[/!][!python]x = 5[/!][!scala]var x: Int =  5 // I can define the type if I want to
var y =  10      // or I can omit the type if I prefer[/!]
[!java|scala]

As you can see, the variables are typed in [!thelang], which means that they are somehow specialized: A given variable can only store data of a given type; Don't even think of storing numbers in a variable that is tailored for letters! Other languages (such as Python) are less picky and allow you to store any kind of data in any variable without restriction. This seems easier at the first glance, but this kind of restriction allows the compiler to catch more logic errors for you, which is also good. In some sense, Python is easier to write but errors can sneak in more easily than in [!thelang]. Here are some of the existing types:

[/!] [!python]

As you can see, the variables are not typed in Python, which means that they are not specialized in any type of data. A given variable store any type of data of a given type: you can store a number in a variable and latter on store a number in the same variable. Other languages (such as Java or Scala) are much more picky and prevent you to mix data types in a given variable. This seems annoying at the first glance, but this kind of restriction allows the compiler to catch more logic errors for you, which is also good. In some sense, Python is easier to write but errors can sneak in more easily.

[/!]

If you know that the value of your "variable" will never change (eg because it contains the screen size or some other constant value), then you should make it a value instead of a variable. Simply change the var keyword with the val one. The compiler can then perform more verifications and catch when you inadvertently modify the value. More interestingly, the compiler can produce faster code in some cases.

Variables work very similarly for strings, floating point numbers and boolean values.

String name = "Martin Quinson";
double height=1.77; // in meters
boolean married=true;// the contrary would be written "false"
val name:String = "Martin Quinson"; // this cannot be modified (it's a value)
var height: Double = 1.77; // in meters
var married = true; // the contrary would be written "false"
// Scala knows that 'true' is a Boolean value, no need to repeat it here
firstName = "Martin"
lastName = 'Quinson' # both single and double quote work here
motto = "I never finish anyth' (but I keep trying)" # having single quote within double quote is fine 
height=1.77 # in meters
married=True # the contrary would be written "False"

Affectations

Once your variable is declared, you can affect a new value to it later in the program. That's really easy:

x = 3[!java];[/!]

To the right of the equal symbol, you can put an arithmetic expression containing constants, variables and operations.

x = 3 + 2[!java];[/!]
x = 3 * x[!java];[/!]
greeting = "Hello "+name[!java];[/!] [!python]#[/!][!scala|java]//[/!] + is (also) the operation to concatenate (ie, to join) strings

Exercise goal

It is now time to do more challenging exercises, don't you think? The objective is now to move forward until you find a baggle, pick it up, and then move back to your initial location before dropping the baggle.

How to do this?

To solve this problem, you have to decompose it in easier sub-parts. For example, you may want to do the following steps:

  1. Move forward until located over a baggle
  2. Pickup the baggle
  3. Move backward of the same amount of steps than done in first step
  4. Drop back the baggle

Naturally, it is impossible to do the right amount of steps backward at step 3 if you didn't count the amount of steps done in the first phase. You can use a variable for that, which can be named stepAmount.

Create an integer variable before phase 1, initialize it to 0, and each time you move one step forward, increment its value by one (stepAmount = stepAmount + 1;[!java] or stepAmount++;, both syntaxes being equivalent[/!]). Such variable which takes every values of a given range is often called a stepper.

[!python|scala]

If you know Java or other languages, you will probably try to use the ++ operator to increment the variable, but it's not allowed in [!thelang]. This is because it would be difficult to define this operator for every data types. For example, what should ++ do when applied to a Complex value or to a String? The problem does not occur in Java as int is not an object but a primitive type. (if you don't know the ++, just ignore this paragraph: it does not exist in [!thelang])

[/!]

Then, phase 3 consists in simply creating a new integer variable doneSteps initialized to 0, and do one step backward until doneSteps equals stepAmount, incrementing doneSteps each time.

It's your turn now!