EECS 211
How to read Chapters 4 and 5

Chapters 4 and 5 are basically one very long chapter, covering the control structure forms in C++:

Also covered are the following operators:

The relational operators, ==, !=, <, <=, >, and >= were covered in Chapter 2 (Figure 2.12).

Hence, these are a pretty dry chapters. I recommend looking at this simple summary (PDF) to get an overview of most of what you need to know about C++ syntax (which is really C syntax). Then check out the tips in the rest of these notes.

Use blocks in control structures

In C and C++, you use braces ({...}) to combine a sequence of statements into one compound statement or block. You can also declares variables in a block. Such variables will be local to that block.

Modern programming practice is to always use blocks in

Do this even when there's just one statement in the block. This will make the code stand out better and will make it easy to add statements to the conditional or iterative form later.

Blocks are less common in do...while and switch forms.

The only for loop you need to know

Use the following as your model for all your for loops:

for (int i = 0; i < n; ++i)

There are many best practices in the above form:

Use prefix ++, not postfix

In C, there are some very nice -- well, short -- loops that can be written using i++. But most C++ loops, including the standard for, don't care which you use.

i++ requires saving the old value of i temporarily, which can be expensive if i holds some large object for which the operator ++ has been overloaded.