User Tools

Site Tools


programming_tutorial

This is an old revision of the document!


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:

example1.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.

example2.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.

example3.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.

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

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

example6.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);
}
example7.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);
}
example8.cpp
#include "tutorial.h"
 
start()
{
        integer i = 1;
        println(i);
        i = i * 10; // multiply 'i' by 10
        println(i);
        i = i * 10;
        println(i);
        i = i * 10;
        println(i);
        i = i * 10;
        println(i);
        i = i * 10;
        println(i);
        i = i * 10;
        println(i);
}
example9.cpp
#include "tutorial.h"
 
start()
{
        string s = "Hello, world!";
        print(s);
}
example10.cpp
#include "tutorial.h"
 
start()
{
        string name;
        print("Hello, what is your name? ");
        read(name);
        print("Hello ");
        print(name);
        print("!");
}
example11.cpp
#include "tutorial.h"
 
start()
{
        integer number;
        print("Enter a number: ");
        read(number);
        print("You entered the number ");
        print(number);
        print("!");
}
programming_tutorial.1349643074.txt.gz · Last modified: 2023/08/18 18:15 (external edit)