Chapter 1: Data Types, Variables, Input and Output

In this chapter we will talk a bit about algorithms and write our first simple programs with MuLE.

Contents

  1. Algorithms
  2. A "Hello, World!" Program
  3. Data and Data Types
  4. Variables
  5. User Input
  6. Arithmetic Operators
  7. String Concatenation
  8. Type Conversion
  9. Turtle


  1. Algorithms
  2. Before we start programming, we have to talk about algorithms, after all they represent the core of any program.
    However, algorithms are specifically restricted to programming, in fact, we also can follow specific algorithms as human beings.
    Let us take a look at a simple example.
    ATM. Source: https://pixabay.com/

    When withdrawing money from an ATM, we have to follow a specific set of steps, for example:

    1. Insert your debit card.
    2. Choose or enter the amount of money to withdraw.
    3. Choose the denomination.
    4. Enter your PIN.
    5. Take your card and money.

    Sounds about right, as a human being we will have no trouble following these steps.
    However, a machine will have difficulties executing this algorithm, since there is at least one ambiduous step.
    Step 2 tells us to choose a pre-set amount or enter the amount of cash, leaving this decision to the user.
    A machine does not know which option to take, so we have to specify it further:

    1. Insert your debit card.
    2. Choose the withdraw option.
    3. If desired, enter a specific value, go to 5.
    4. Select one of the pre-set values.
    5. If desired, choose the denomination.
    6. Enter your PIN, if correct go to 8.
    7. Go to 6.
    8. Take your card and money.

    As we can see, we have added a couple of weird sounding steps, especially when it comes to go to instructions.
    The go to instructions allow us to skip some steps or repeat previous steps depending on a specific condition.
    This way we have removed the ambiguity from the previous algorithm.
    Of course, these steps are still heavily abstracted and we may need to specify them further, but we won't go there yet.
    What we need to know now, machines cannot think the way humans do, unlike machines we are capable of abstract thinking.
    When programming a machine however, we have to define every step at lower levels of abstractions.
    Thankfully, modern programming languages work at a relatively high level, so in most cases we don't have to address the hardware directly.

    So what is an algorithm in the context of Computer Science?
    An algorithms is a finite, unambiguous sequence of strict, executable instructions which solve a specific problem.
    Meaning, that when we encounter a problem, e.g. How do I withdraw money from an ATM, we have an abstract definition of said problem.
    We then need to formulate an algorithm to solve that problem similarly how we did it before.
    And then we can start translating the algorithm into a program using a programming language.
    There is a lot more to algorithms and their properties, but we are here to program and not talk about dry mathematical theory, aren't we?

    Back to top



  3. A "Hello, world!" program
  4. If you have followed the getting started guide, you have propably already written and executed you first "Hello, World!" program.
    If you have not, we strongly suggest that you do it now.

    There is quite a lot of information in this example, so let us try to summarize it:

    1. A program is written in a file, its name must be equal to the file name.
    2. We can import libraries, which are contained in their own files, using their names in an import instruction.
    3. Named elements, e.g. a program or a variable, can be referred to by theit names. In this example we first refer to the IO library to import it, then again to access the writeString operation, which we also reference by its own name.
    4. The main procedure is the entry point in our program, i.e. the first statement here is the first one to be executed.
    5. A statement is a single executable instruction with a specific task, e.g. the IO.writeString("Hello, world!") invokes the imported operation to print a string on the console.
      Other statements can be used to declare variables, change their values or manipulate to flow of our program depending on specific conditions.
    6. Statements can be complemented by expressions, which in case of MuLE cannot be used outside of statements. An expression is evaluated and yields a value.
      In our case "Hello, World!" is an expression which is used to pass a string value to the writeString operation. Another example of an expression is 2 + 3 which yields 5.
    7. Values represent data. They also have a data type, which is used to validate the correctness of our program.
      In our example we invoke an operation which expects a string value, meaning that we are not allowed to pass a value with a different data type, e.g. an integer.
      So if we want to print a 5 using writeString, we have to pass it as a string value, i.e. "5".
      Otherwise our program will not compile.
    8. We can define our own operations and types outside of the main procedure, which we will do it in subsequent chapters.

    Comments are a very useful feature, as the name suggests, they are meant to be used to write comments for your code.
    Even if you understand now, what you code is doing, if you look at it in a couple of weeks or even months, you will have to invest time to understand you own program.
    If you write meaningful comments for you programs, both your future self as well as your colleagues will be very grateful.

    Back to top



  5. Data and Data Types
  6. The word informatics is an amalgam of two words: infortmaion and mathematics. Or in other words, informatics is built around data and what we can do with it.
    Thus the core idea of any program is usually to handle chunks of data, and in order to know what to do, each piece of information has a data type.
    Let's take a look at the following program, feel free to copy and paste the code into your Eclipse and run it.

    As we can see, we print different values on the console and we need different operations for different types of values.
    We can not print an integer using the writeString operation since it expects a string value as a parameter.

    MuLE supports four basic data types:

    Back to top



  7. Variables
  8. The concept of variables should sound familiar, pretty sure most of us have heard it in the mathematics class.
    In the mathematics variables represent placeholders for values, e.g. in f(x) = mx + t we have three variables, with x also acting as a parameter.
    In most programming languages, variables act as a named container for values, i.e. we can store our data there.
    The image below gives an extremely simplified explanation of variables in the memory of a program, i.e. we have a container with the name x where we can store integer values. The currently stored value is 42.

    The following code demonstrates the basic usage of variables, go ahead and try it out in your Eclipse.

    Back to top



  9. User Input
  10. When talking about our ATM algorithm at the beginning of this chapter, we have mentioned steps in which user had to enter her PIN or the amount of money.
    This values are specific to each user, meaning that there should be a possibility for the user to enter this information and pass it to the machine.
    To achieve this we use the IO library again, which we have already used to print values on the console.
    This time we have to use one of the read operations, e.g. readString if we want to read a string value, which we will have to enter from the console view.
    Please notice, that the read operation does not accept any parameters, unlike the write operations.
    However it yields us a value, which we promptly store in a variable. We can now use this variable to access the data which we have entered earlier, for example to print it on the console.

    Back to top



  11. Arithmetic Operators
  12. Arithmetic operators allow us to parforms basic arithmetic operations on numeric values, i.e. values of types integer and rational.

    Back to top



  13. String Concatenation
  14. Similar to our arithmetic operations, we can apply a specific concatenation operator & on two strings to create a single string from them.

    Back to top



  15. Type Conversion
  16. Sometimes we have the right data but with the wrong data type.
    For example, we want to concatenate a number to a string value.
    We can't simply do that using the concatenation operator since it expects two string values.
    So first, we need to transform our number into a string.
    We can do this by importing the Strings library and using some of its operations, for example integerToString.

    Back to top



  17. Turtle
  18. And as the final topic of this chapter, we will take a look at the Turtle library.
    This library allows us to draw pictures programmatically, which, as we all know, is much more fun than computing arithmetic operations, isn't it.
    To use the Turtle library, we have to import it first.
    The library is very simple, we can draw lines using the forward(LENGTH_PIXELS) opeation and change our bearing using the left(ANGLE) or right(ANGLE) operations.
    There are other operations of course, but we don't need them right now.


    Back to top