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.

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

Variables

example5.cpp
#include "tutorial.h"
 
start()
{
        integer i = 5;
 
        // prints the value of 'i' to the screen
        print(i);
}
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.1349639253.txt.gz · Last modified: 2023/08/18 18:15 (external edit)