Figure 2.1 A Program that prints text
The comments are targeted at beginners. Normally we do not explain everything
in detail. You put in enough extra comments to help your colleagues (and yourself)
understand what the code is about.
I'll be happy if you put three or four lines that say who you are, what the program should do, and how far it has progressed. You can also comment on things that are not obvious.
You are allowed to have a single sheet of 8.5><11 piece of paper to aid your memory in quizzes and final. You might like to write down the outline of a small working program:
#include <iostream>
int main()
{
std::cout << "Hello, World!\n";
return 0;
}[ 03.cpp ] You need to get completely used to the C++ syntax.... including where semicolons (";") are needed and where they must be left out.A
Why do we need iostream in every program
Because a program without input and output (IO) is useless.
We put it first so that the compiler knows the meaning of many special
words and symbols that it defines before it meets them. And
every where in the program. For example <iostream> defines
the
stream extraction
operator ">>" is defined to extract data from a stream. An example being
cin >> number;
Putting these definitions isdie a program is a syntax error!
return 0;
I read this as
terminate with zero errors.In UNIX the number you return from the main function is the the number of the error things go wrong.
Later we will find another use for the return statement in C++.
Figure 2.2 -- Escape Sequences
This is a good place for a sticky note. You might like to add Newline and
Horizontal tab to your Cheat Sheet.
2.3 Modifying our first program
All programs are modified, many times.
Hence we make our code clear and amendable --
even if this takes extra effort!
We change code because
Destruction of data in variables
Many commands replace the value of a variable by a new value. The old
value is "destroyed". You can not get it again. Suppose we have
two int variables a and b with values 1 and 2 respectively. We
may want to exchange the two values, and we might write:
a=b;
b=a;(which is good math!). But what happens? [ bad.cpp ] Here is a trace
| Command | a | b |
|---|---|---|
| Initially | 1 | 2 |
| a=b; | 2 | 2 |
| b=a; | 2 | 2 |
We destroyed the value of a with the command "a=b;".
To exchange or swap two values, the standard technique is to add a variable to save the old value of one variable, like this:
int old_a=a;
a=b;
b=old_a;Here is how it works
| Command | a | b | |
|---|---|---|---|
| Initially | 1 | 2 | old_a |
| int old_a=a; | 1 | 2 | 1 |
| a=b; | 2 | 2 | 1 |
| b=old_a; | 2 | 1 | 1 |
Good Programming Practice 2.12
I've used both styles: declare all variables first vs declare just before use.
I will except either in your code.... but not BOTH.
Declare one variable per line
because you will make fewer mistakes!
Finding errors without compiling
2.5 Memory Concepts
Memory is like a gigantic piece of paper with lots of small boxes
(locations) where you can put numbers and other data. These boxes are
numbered and the computer actually uses these numbers (addresses) to
determine which data to use and which to change. In C++, and most
languages, we don't have to use numbers to address data but declare variables
instead.
A variable is the name of a box(location). A ">>" puts data into a box,
"+" gets data from two boxes, into the CPU and a "<<" copies it out
to the user.
Self Review Exercises -- If you have time
Pages 75-77, Exercises 2.1-2.3.
. . . . . . . . . ( end of section Prepare) <<Contents | End>>
Deliver -- a question on one of the above sections
Demo -- walkthrough or trace of figure 2.5 Page 50
Exercises -- probably from 2.7 to 2.13
TBA
Next -- C++ Elements etc
[ 04.html ]
[ lab02/ ]
. . . . . . . . . ( end of section 03 First C++ Program) <<Contents | End>>
Abbreviations