A short introduction to computer programming, using C++

Roger Mitton, Birkbeck, University of London

Chapter 1

To get a computer to do something, you have to tell it what you want it to do. You give it a series of instructions in the form of a program. You write a program in a programming language. Many programming languages have been devised; well known ones include Fortran, BASIC, Cobol, Algol, Lisp, Ada, C++ and Java.

Programming languages vary a lot, but an instruction in a typical programming language might contain some English words (such as while or return), perhaps a mathematical expression (such as x + 2) and some punctuation marks used in a special way. The following three lines are in BASIC, Cobol and C++ respectively:

FOR I% = 1 TO 100 STEP 5 DO
PERFORM RECORD-TRANSACTION UNTIL ALL-DONE = "T"
if (x > 100) cout << "Big"; else cout << "Small";

Programs can vary in length from a few lines to thousands of lines. Here is a complete, short program written in C++:

// Given a series of words separated by spaces,
// this program finds the length of the longest word.

#include <iostream>
#include <string>

using namespace std;

int main()
{ string       s;
  int          max = 0;
  while (cin >> s)
    if (s.length() > max)
       max = s.length();
  if (max == 0)
    cout << "There were no words." << endl;
  else
  { cout << "The longest word was ";
    cout << max << " characters long." << endl;
  }
}

When you write a program, you have to be very careful to keep to the syntax, ie to the rules of the language. For example, the above program would behave incorrectly if we wrote if (max = 0) instead of if (max == 0) or if we put a semi-colon immediately after while (cin >> s). The computer would refuse to accept it if any of the semi-colons were missing or if any of the parentheses or curly braces were missing, or if we wrote min instead of main or even Main instead of main.

Input and output

To get the computer to take some input and to store it in its memory, we write, for example:

cin >> num;
cin (pronounced "see-in"), is a word which has a special meaning to the computer. The combination of cin with the symbol >> means "Take some input and put it into the memory." num, by contrast, is a word that I (the programmer) have chosen. I could have used number or widget or foodle or just n or almost any word I wanted. (There are some restrictions which I will deal with in the next chapter.) The semi-colon signals the end of this instruction.

Computers can take in all sorts of things as input — numbers, letters, words, records and so on — but, to begin with, we will write programs that only handle numbers (whole numbers like 0, 2, 15, 10025, –1, –2465). We'll also assume that the computer is taking its input from the keyboard, ie when the program is executed, you key in one or more numbers at the keyboard and these are the numbers that it puts into its memory.

You can imagine the memory of the computer as consisting of lots of little boxes. Programmers can reserve some of these boxes for use by their programs and they refer to these boxes by giving them names. cin >> num; means "Take a number and put it in the box called num." When the program runs and this instruction gets executed, the computer will take a number which you type at the keyboard (8 or –25 or 9999 or whatever you like) and will put it into the box which has the (temporary) name of num.

Each of these boxes can hold only one number at a time. If a box is holding, say, 26, and you put a 44 into it, the 44 replaces the 26. In computer parlance these boxes are called variables because the number inside the box can vary; you can put a 10 in it to start with and later change it to a 20 and change it again to 555 and so on as often as you want.

In C++ you have to tell the computer that you want to use a variable with a certain name before you use it. You can't just pitch in with cin >> num; without telling it what num is. You also have to tell it what type of variable it is, ie what sort of thing you are going to put into it. In this case we are going to put whole numbers into it. Whole numbers, or integers as they are called in programming, are known in C++ as int. To tell the computer that we want to use a variable of type int called num, we write

int num;
If we wanted more than one variable, we could use two lines:
int num;
int total;
or we could declare them both on one line, separated by a comma
int num, total;
Your program can have as many variables as you want. In C++ you don't have to declare all your variables at the start of the program, as is the case in some other languages. You can declare them in the middle of a program, but you mustn't try to use a variable before you've declared it.

If you want the computer to display (on the screen) the contents of one of its boxes, you use cout << (pronounced "see-out") followed by the name of the box. For example

cout << num;
If the contents of the box called num happened to be 876 when the computer came to execute cout << num; then the number 876 would appear on the screen. Note that the arrows go >> with cin >> num; and << with cout << num; Think of the arrows in cin >> num; as showing you that something is going from the input into num, whereas in cout << num; something is being taken out of num and sent to the output.

Arithmetic expressions such as num + 5 can also appear in a cout line. For example, if num had the value 7, then cout << num + 5; would output 12.

To put these instructions into a valid C++ program, we need some more lines. I'll explain what they mean in a later chapter, but for now just copy them down (carefully). We have to begin the program with:

#include <iostream>
using namespace std;
int main()
{
and we finish it with a closing curly brace:
}

So we can now write a program in C++ (not a very exciting program, but it's a start):

#include <iostream>
using namespace std;
int main()
{   int num;
    cin >> num;
    cout << num;
}
This program takes a number as input from the keyboard and displays it on the screen.

It is customary to lay out programs like this, but putting each part on a line of its own is actually for the benefit of human readers, not for the computer. Except for the part beginning with a "#", which must be on its own line, this entire program could be on one line, thus:

#include <iostream>
using namespace std; int main() { int num; cin >> num; cout << num; }
I am not suggesting that you use this style. On the contrary, you should use the style of the previous version. I am just making the point that the layout of a C++ program on the page — separate lines for separate parts, blank lines, indentation of certain lines — is for the benefit of the human reader. With a few exceptions, it is of no significance to the computer.

Compiling and running a program

You can learn the rudiments of C++ from these notes just by doing the exercises with pencil and paper. It is not essential to run your programs on a computer. However, if you have a computer and are wondering how you run the programs, you will need to know the following, and, even if you don't have a computer, it will help if you have some idea of how it's done.

First of all you type your program into the computer using a text editor, but before you can run the program you have to compile it. This means that you pass it through a piece of software called a compiler. The compiler checks whether your program is acceptable according to the syntax of C++. If it isn't, the compiler issues one or more error messages telling you, in a more or less unhelpful fashion, what it objects to in your program and where the problem lies. You try to see what the problem is, correct it and try again. You keep doing this until the program compiles successfully. You now have an executable version of your program, ie your program has been translated into the internal machine instructions of the computer and the computer can run your program.

Now you issue a command (or click on an icon) and the computer executes your program. If you are lucky, your program does what it is supposed to do first time. Often it doesn't. You look at what your program is doing, look again at your program and try to see why it is not doing what you intended. You correct the program, recompile and run it again. You might have to do this many times before the program behaves in the way you wanted.

As I said earlier, you can study this introduction without running your programs on a computer. However, it's possible that you have a PC with a C++ compiler and will try to run some of the programs given in these notes. If so, do not be dismayed if your compiler objects to them. The ISO standard for C++ was only established in 1998 and there are many pre-standard compilers in circulation. The most likely problems are:

If you have a PC but you don't have a compiler, I attach a few notes telling you how you can obtain one.

Outputting words and ends of lines

Let's suppose that you managed to compile your program in one way or another and that you then ran it. Your running of the above program would produce something like this on the screen:

1234
1234
The first line is the result of you keying in a number. The system "echoes" the keystrokes to the screen, in the usual way. When you hit RETURN, the computer executes the cin line, ie it reads the number. Then it executes the cout line and the number appears on the screen again.

We can also get the computer to display words by putting them in quotes after the cout <<, for example:

cout << "Hello";
We can use this to improve the above program:
#include <iostream>
using namespace std;
int main()
{   int num;
    cout << "Please key in a number: ";
    cin >> num;
    cout << "The number was ";
    cout << num;
}
A run of this program might appear on the screen thus:
Please key in a number: 9876
The number was 9876

Note the spaces in the cout lines after number: and was. This is so that what appears on the screen is number: 9876 and was 9876 rather than number:9876 and was9876.

It's possible to output more than one item with a single cout line. For example, we could combine the last two lines of the program into one:

    cout << "The number was " << num;
and the output would be exactly the same.

Let's suppose that we now added three lines to the end of our program, thus:

#include <iostream>
using namespace std;
int main()
{   int num;
    cout << "Please key in a number: ";
    cin >> num;
    cout << "The number was " << num;
    cout << "Now please key in another: ";
    cin >> num;
    cout << "And this one was " << num;
}
The screen would look something like this:
Please key in a number: 9876
The number was 9876Now please key in another: 543
And this one was 543
Which is probably not what we wanted. If we want a new line after the output of the first number, we have to include this in the cout line. We do it by putting an endl into the line, thus:
    cout << "The number was " << num << endl;
Now we would get:
Please key in a number: 9876
The number was 9876
Now please key in another: 543
And this one was 543

We can input two or more numbers with a single cin. The numbers can be on the same line, separated by one or more spaces, or on separate lines. The following lines:

cout << "Please key in two numbers: ";
int  x, y;
cin >> x >> y;
would read 5 into x and 6 into y in any of the following ways:
Please key in two numbers: 5 6
or
Please key in two numbers: 
5
6
or
Please key in two numbers: 5
6

Exercise 1A

Now pause and see if you can write

To check your answers, click on Answers to the exercises.

Assignment and initialization

There is another way to get a number into a box apart from using cin >>. We can write, for instance:

num = 22;
This has the effect of putting the number 22 into the num box. Whatever was in num before is obliterated; the new number replaces the old one.

In programming, this is called assignment. We say that the value 22 is assigned to the variable num, or that the variable num takes the value 22. The "=" symbol is the assignment operator in C++. We are not testing whether num has the value 22 or not, nor are we stating that num has the value 22; we are giving the value 22 to num.

If we want, we can have arithmetic expressions on the right-hand side of the "=", for example:

num = count + 10;
This instruction means, "Take whatever number is in count, add 10 to it and put the result into num."

An assignment instruction such as this:

num = num + 5;
looks a little strange at first but makes perfectly good sense. Let's suppose the current value of num (the contents of the box) is 14. The instruction says, "Take the value of num (14), add 5 to it (19) and put the result into num." So the effect is to put 19 into num in place of the earlier 14.

The "=" operator is also used to initialize variables. When the computer allocates a portion of memory to store one of your variables, it does not clear it for you; the variable holds whatever value this portion of memory happened to have the last time it was used. Its value is said to be undefined.

Using undefined values is a common cause of program bugs. Suppose a program uses the variable num without giving it an initial value and suppose that, on the computer the programmer is using, the initial value in num happens to be zero and that, by happy chance, zero is just what the programmer wants it to be. The program seems to work fine. Then the program is compiled and run on a different computer. On this second computer, the initial value of num does not happen to be zero. The program, which has worked OK on the first computer, does not work on the second one.

To prevent yourself from using undefined values, you can give a variable an initial value when you declare it. If you wanted num to begin with the value zero, you should declare it thus:

int num = 0;
This is very like assignment since we are giving a value to num but this is a special case where num did not have any defined value before, so it is known as initialization.

Finally a word about terminology. I have used the word "instruction" to refer to lines such as cout << num; and num = 5; It seems a natural word to use since we are giving the computer instructions. But the correct word is actually "statement". cout << num; is an output statement, and num = 5; is an assignment statement. The lines in which we tell the computer about the variables we intend to use, such as int num; or int num = 0; are called variable definitions. They are also referred to as variable declarations. When you learn more about C++ you will find that you can have declarations which are not definitions, but the ones in these introductory notes are both definitions and declarations.

Exercise 1B

Now see if you can write a program in C++ that takes two numbers from the keyboard and outputs the sum, eg if you keyed in 6 and 8 it would reply with 14. A run of the program should look like this:

Please key in a number: 6
And now key in another: 8
The sum is 14

To check your answers, click on Answers to the exercises.