Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help >  Languages >  C++

"I don't understand anything"

Contents

When people say "I don't understand anything" about programming it's sometimes because they're trying to understand too much. This page aims to show you which parts of a program you can take for granted, and where to need to focus your attention. It assumes that you've read a little about C++ already, but that maybe you want a different approach. There are 2 exercises for you to try later, so you'll need to know how to write, compile and run programs on your machine.

Example 1

Consider this C++ program.
#include <iostream> using namespace std; int main() { int i=0; cout << "i=" << i << endl; }

The first 2 lines let us use cout which is a command we need later. Don't worry about them, just start all your programs with them.

In C++ a function is a runnable bit of code that has a name. Every C++ program has a function called main. When the program is started, the main function is run first. So it's no surprise that we have a main function here. All your programs are likely to have

int main() {
to start the main function and
}
to end it. That leaves just 2 lines in the examples to worry about. If you compile this (i.e. convert it into a form the computer can understand) then run it, it will print out "i=", then the value of i, then it would end the line, so the output would be
i=0

Example 2 - a for loop

In the first example, the lines were obeyed one after the other from beginning to end. The next example uses a for loop. It's not trivial, but for loops are important to know about. They're used when you want to do the same thing (or nearly the same thing) many times. In the following example we want the program to print out
i=0 i=1 i=2
We could change the first program to become this
#include <iostream> using namespace std; int main() { int i=0; cout << "i=" << i << endl; i=1; cout << "i=" << i << endl; i=2; cout << "i=" << i << endl; }
It would do what we want, but it's getting repetitive already. If we wanted i to go up to 10, we have to do a lot of typing. Here's another version that does what we want, and also prints The End.
#include <iostream> using namespace std; int main() { for (int i=0; i<3; i++) cout << "i=" << i << endl; cout << "The End" << }

So how does the for construction work? It doesn't run int i=0, then i<3 then i++ in a sequence - these 3 bits of information are used to set up the looping mechanism.

movie1Whenever you do things lots of times you need to think about the starting conditions, the end conditions, and what, if anything, will change after each cycle. The for construction concisely controls all these details. Note that inside the brackets of the for construction there are 3 parts separated by semi-colons. Let's look at each part in turn

Putting that all together, what happens is that i will be set to 0, its value will be printed out, then i will be set to 1 and its value printed out and so on while i is less than 3.

It may help to watch this looping animation of what happens step by step as the program runs. The green arrow in this animation shows the line of code that's being executed - the program loops around, running some lines several times. The box at the top shows the value of i. The "Execution Window" at the bottom shows the program's output. Note that the i variable ends up with the value 3, though that value isn't printed in the "Execution Window". Why is that?

More about for loops and compiling

Local information about for loops includes

The method you use to compile the programs will depend on computing set-up you have. Local users can use the method described in the 1st year handout. Alternatively, you can open a Terminal window, create a text file with the source code in it (called program1.cc, say) then on the command line you can type

    g++ -o program1 program1.cc

to compile the source code and produce a program called program1. To run the program, type

    ./program1

Example 3 - a Times Table

This time you're going to write a program. It will print out the 7 times table something like

1x7=7 2x7=14 ... 12x7=84

etc. Use a for loop - you can adapt the code from example 2.

If you have problems, mail Tim Love (tl136)

Example 4 - functions

We're now going to write a program that will tell us whether numbers are even or odd. The program's output will be

0 is even 1 is odd 2 is even 3 is odd 4 is even

The main function is going to be

int main() { for (int i=0; i<5; i++) if (is_even(i)) cout << i << " is even " << endl; else cout << i << " is odd " << endl; } }

It uses if and else which are quite easy to understand, I hope. You can read the "if" line as saying "if i is even, then do ,...". This code fragment assumes that C++ has a function called is_even. Unfortunately there's no such function so we'll have to write it ourselves. We could do it many ways. Here we'll use %, which gives us the remainder after integer division. So if we do number%2 and the answer is 0, 2 divides exactly into the number, so the number is even. We want is_even to give us a true/false answer. In C++ there's a type of variable called bool (short for Boolean) which can store such answers. Here's the routine.

bool is_even(int number) { if ( (number % 2) == 0) return true; else return false; }

The code inside the function is as described earlier. Note that in C++, functions "return" their answers back to the thing that asked for them. The first line of the routine needs an explanation.

We now have nearly all the code. Here's the complete program with the main and is_even functions we've prepared.
#include <iostream> using namespace std; bool is_even(int number); int main() { for (int i=0; i<5; i++) if (is_even(i)) cout << i << " is even " << endl; else cout << i << " is odd " << endl; } bool is_even(int number) { if ( (number % 2) == 0) return true; else return false; }
The extra line
bool is_even(int number);

is needed because otherwise the compiler will be surprised when it reaches the is_even call inside the main function, and will complain that that there isn't such a function. This line (it's called a "prototype" or "signature") is basically the first line of the function. It tells the compiler enough about the function for compilation to work.

movie2

Here's a looping animation of the program when it's running. The green arrow starts in the main routine as usual. As in earlier examples, the green arrow goes round and round the loop, but this time it jumps to and from the is_even function. Inside that routine it sometimes follows the if route and sometimes the else route, depending on whether the number is even or odd.

You may need to watch the animation a few times. The important thing to realise is that the code isn't simply run from top to bottom - some lines are run many times. It's also worth noting that the i variable exists only in the main function and the number variable exists only in the is_even function - that's why they keep appearing and disappearing at the top of the animation. They are called local variables.

Example 5 - functions

Adapt the previous example so that instead of identifying even numbers it identifies multiples of 3. Give the function a sensible name.

More about functions

Local information about functions includes

© Cambridge University Engineering Dept
Information provided by Tim Love (tpl)
Last updated: June 2009