Table of Contents

Introduction

This tutorial is a basic introduction to programming. It is written with the assumption that the student has never written a computer program before beginning with the basic program structure and the ever popular "Hello, world!" program.

The language is based on the C++ programming language with some simplifications. By the end of the tutorial the student should have a good understanding of general programming to be able to write useful and interesting programs.

Program Structure

The example programs in this tutorial use a special header file tutorial.h that is intended to keep the examples extremely simple. This file can be downloaded from here.

[include section]

[declarations]

[functions]

[start]

The programs begin with the include section. Don't worry about this section for now. Just remember that this section will always contain the line #include "tutorial.h". This just sets up some stuff that our programs will use.

Following the include section are the declarations and functions sections. These sections will contain information that our program might need later. We'll look more at these sections later.

Finally, every program needs a start section. This tells the computer where to start executing our program. The start section is identified by the keyword start. Following this keyword are the instructions that our program is to execute. The end of the program is identified by the end keyword. This tells the computer that the program is finished and it can stop running.

Let's take a look at our first program.

Hello, World!

example1.cpp
#include "tutorial.h"
 
start
    print("Hello, world!");
end

This program begins with the include section that includes our tutorial library. Don't worry too much about this section for now. Just remember that for our programs, the #include "tutorial.h" line must always be there.

This program does not have any declarations or functions defined and we go straight to the start keyword.

Our start section includes only one statement, the print command. This command takes whatever value we give it and print it to the screen.

Finally, we have the end keyword to tell the computer that we are done.

INSERT IDE INSTRUCTIONS HERE

Once you run this program you should see the following output on the screen:

Hello, world!

You can call print more than once to print multiple items. For example:

example2.cpp
#include "tutorial.h"
 
start
    print("Hello, ");
    print("world!");
end

This example produces the same output as the previous example. The second print command begins its output where the previous left off.

Practice

  1. Write a program that prints your name.
  2. Write a program that prints your family's names.
  3. Write a program to print a verse from your favorite poem or a paragraph from your favorite book.

Syntax Errors

While you are programming it is inevitable that you will type something wrong. When you try to run your program and there is a typo the compiler will complain with some pretty cryptic error messages.

Take a look at the following example. In this example we forgot to include the closing quotation mark on our output sentence.

example3.cpp
#include "tutorial.h"
 
start
        print("Hello, world!);
end

This produces the following error message:

example2.cpp:4:8: warning: missing terminating " character
example2.cpp:4:2: error: missing terminating " character
example2.cpp: In function ‘void t_main()’:
example2.cpp:5:1: error: expected primary-expression before ‘}’ token
example2.cpp:5:1: error: expected ‘;’ before ‘}’ token

Pretty cryptic huh? Don't be daunted by these messages. Although they look intimidating they do provide some clues to what might be wrong. In our case the line example2.cpp:4:2: error: missing terminating " character tells us that on line 4 we are missing a closing quotation mark.

Note also, there are other errors following that initial error. These errors are a result of the compiler getting confused and are not true errors. Once you fix the first error the others will go away.

In this next example, we misspell the print command.

example4.cpp
#include "tutorial.h"
 
start
        prnt("Hello, world!");
end

This produces the following error message:

example3.cpp: In function ‘void t_main()’:
example3.cpp:4:22: error: ‘prnt’ was not declared in this scope

Again cryptic but it tells us that on line 4 it doesn't know what prnt is.

Practice

Try to make other errors in the code and see what kinds of error messages you get. This will help you become more familiar with the errors as you make mistakes working through the rest of the exercises.

Comments

Comments are not actually part of the program. They are blocks of free text that you can insert into your code to remind yourself what a piece of code is doing.

Comments begin with the two characters //. Everything following those characters to the end of the line are considered a comment.

example5.cpp
#include "tutorial.h"
 
start
        // This prints a message to the screen
        print("Hello, world!");
end

Variables

Variables are locations in memory that are used to remember things. You can think of them as a box on a shelf. Each box can only hold one thing at a time. When putting a new thing into the box, the old thing is removed and either thrown away or put into a different box.

Each variable has a name. You can think of the name as the label on the box. The name just specifies which box you want to work with.

When using a thing stored in a variable in your program, you refer to it using the variable name. Have a look at this example:

example6.cpp
#include "tutorial.h"
 
start
        integer i = 5;
 
        // prints the value of 'i' to the screen
        print(i);
end

This example creates a variable named i. A variable is defined by first giving it a type, in this case it is an integer. (An integer is a positive or negative whole number or zero. e.g …, -2, -1, 0, 1, 2, …)

Following the type is the variable name itself. The variable can be named anything you want. After the name you can optionally give it a value or leave it empty. In this example we give it the value 5.

We want to print the value we have stored in our variable. To do that we simply give it to the print command.

The output from running this example should look like this:

5

Changing the value in a variable is done simply by supplying the new value. In the following example, we change the value from 5 to 6 and print the result.

example7.cpp
#include "tutorial.h"
 
start
        integer i = 5;
        print("i = ");
        print(i); // prints the value of 'i' to the screen
        i = 6;
        print(", now i = ");
        print(i);
end

The output of this should look like the following:

i = 5, now i = 6

Variables can be copied from one to another. We can take the thing stored in one box, and store a copy in another. For example, here we make a copy of what is in i and put it into the variable j and then print out j:

example8.cpp
#include "tutorial.h"
 
start
        integer i = 5;
        integer j = i;
        print(j);
end

Output of this program should look like this:

5

Practice

  1. Write a program that prints your age.
  2. Write a program that prints today's date.

Arithmetic Operations

Arithmetic operations consist of the usual add (+), subtract (-), multiply (*) and divide (/). Notice that the multiplication operator is an astrisk (*) symbol and not an x.

In the following example, perform some calculations:

example9.cpp
#include "tutorial.h"
 
start
        integer i = 7 + 1;
        print("i = ");
        print(i);
 
        integer j = 8 - 2;
        print(", j = ");
        print(j);
 
        integer k = 6 * 5;
        print(", k = ");
        print(k);
 
        integer l = 30 / 5;
        print(", l = ");
        print(l);
end

The output from this program looks like:

i = 8, j = 6, k = 30, l = 6

We can also perform calculations using variables as terms to the calculation:

example10.cpp
#include "tutorial.h"
 
start
        integer i = 7 + 1;
        print("i = ");
        print(i);
 
        integer j = i - 2;
        print(", j = ");
        print(j);
 
        integer k = j * 5;
        print(", k = ");
        print(k);
 
        integer l = k / 5;
        print(", l = ");
        print(l);
end

This program's looks like before only we used variables in the calculations:

i = 8, j = 6, k = 30, l = 6

A variable can also be used in a calculation that updates itself. In this case, the value in the variable is used in the calculation, the result of the calculation then replaces the old value that was stored in the variable:

example11.cpp
#include "tutorial.h"
 
start
        integer i = 1;
        print(i);
        i = i + 1; // add 1 to 'i'
        print(", ");
        print(i);
end

The output from this program looks like this:

1, 2

In the next example, we'll write a program that counts from 1 to 10. Notice that this program uses only one variable i.

example12.cpp
#include "tutorial.h"
 
start
        integer i = 1;
        print(i);
        i = i + 1; // add 1 to 'i'
        print(i);
        i = i + 1;
        print(i);
        i = i + 1;
        print(i);
        i = i + 1;
        print(i);
        i = i + 1;
        print(i);
        i = i + 1;
        print(i);
        i = i + 1;
        print(i);
        i = i + 1;
        print(i);
        i = i + 1;
        print(i);
end

The output from this program looks like this:

12345678910

println

Notice that all the numbers in the last example are up against each other. That's because we didn't print any formatting like we did before. What if we wanted to print each number on its own line? We can use a new command println to do this. println works just like the previous print command except that it forces the next print to start on the next line.

example13.cpp
#include "tutorial.h"
 
start
        integer i = 1;
        println(i);
        i = i + 1; // add 1 to 'i'
        println(i);
        i = i + 1;
        println(i);
        i = i + 1;
        println(i);
        i = i + 1;
        println(i);
        i = i + 1;
        println(i);
        i = i + 1;
        println(i);
        i = i + 1;
        println(i);
        i = i + 1;
        println(i);
        i = i + 1;
        println(i);
end

The output of this code now looks like this:

1
2
3
4
5
6
7
8
9
10

Practice

  1. Write a program that prints the powers of 10.
    1. Start with a variable set to 1.
    2. End when the variable reaches 1,000,000.
  2. Write a program that calculates how many days are in two weeks.
    1. Use a variable called daysInAWeek.
    2. Use a variable called numberOfWeeks.
    3. Use a variable called totalDays.
    4. Print the value of totalDays.

Strings

example14.cpp
#include "tutorial.h"
 
start
        string s = "Hello, world!";
        print(s);
end

Getting Input

example15.cpp
#include "tutorial.h"
 
start
        string name;
        print("Hello, what is your name? ");
        read(name);
        print("Hello ");
        print(name);
        print("!");
end
example16.cpp
#include "tutorial.h"
 
start
        integer number;
        print("Enter a number: ");
        read(number);
        print("You entered the number ");
        print(number);
        print("!");