|
|||
Department of Engineering | |
University of Cambridge > Engineering Department > computing help > Languages > C++ |
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.
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
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.
Whenever 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?
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
This time you're going to write a program. It will print out the 7 times table something like
etc. Use a for loop - you can adapt the code from example 2.
If you have problems, mail Tim Love (tl136)
We're now going to write a program that will tell us whether numbers are even or odd. The program's output will be
The main function is going to be
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.
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.
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.
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.
Local information about functions includes
| C++ | Languages | computing help | |