## 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 later 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 an equal sign (=) and an initial value.[/!] [!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. You can name a variable `stepAmount` if you want, but `step amount` is not a valid name.

[!c|java]

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

    int x;

If you want, you can specify the initial value of the variable by adding an equal sign (=) followed by the value after the declaration.

    int x=5;

[/!] [!python]

So, if you want that the variable contains 5 as initial value, you should type:

    x = 5

[/!] [!scala]

So, to create a variable named x intended to contain integers with 42 as initial value, one should write:

    var x:Int = 42

In most cases, the compiler is able to guess the type of the variable from the initialization value, and you can omit it:

    var x = 42

You still have to specify if you use some generic values as an initialization, such as the very particular value `null`, which type happens to be ambiguous. Even when the type is clear, you can still specify it if you prefer. So you want that the variable contains 5 as initial value, you should type:

    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|c]

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! The [!thelang] language is said to be statically typed.

Other languages (such as Python) are less picky and allow you to store any kind of data in any variable, without restriction (those languages are said to be dynamically typed). 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 existing types in [!thelang]:

- [!java|c]int[/!][!scala]Int[/!], for integers;
- [!java|c]double[/!][!scala]Double[/!], for dot numbers;
- [!c]int[/!][!java]boolean[/!][!scala]Boolean[/!], for booleans that are values being either [!c]1[/!][!java|scala]true[/!] or [!c]0[/!][!java|scala]false[/!];
- [!c]char*[/!][!java|scala]String[/!], for char strings.

[/!] [!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 later on store a number in the same variable. The values themselves are still typed, but not the variable. Python is said to be dynamically typed.

Other languages (such as Java, Scala or C) are much more picky and prevent you to mix data types in a given variable (they are said to be statically typed). 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 check your actions and catch your error 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.

    char* name = "Martin Quinson";
    double height=1.77; // in meters
    int married=1;// 1 means "true"; "false" would be written 0

    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|c];[/!]

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

    x = 3 + 2[!java|c];[/!]
    x = 3 * x[!java|c];[/!]
    [!java|scala|python]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|c] 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 as long as `doneSteps` is not equal to `stepAmount`, incrementing `doneSteps` each time. The `!=` operator should be used to test the inequality (whether some values are NOT equal).

It's your turn now!