2. First Programs

This chapter is special in one aspect. Instead of using a computer, it requires you to do a few simple tasks in the old-fashioned way, on a piece of paper. So, get ready, and pick up a pencil and a few pieces of paper now. Afterward, in Chapter 3, you will learn how to run your programs using a computer.

Learning a programming language is not completely unlike learning a foreign language. In this chapter, we will introduce the basic constructions that the programming language C++ employs.

Just like with any other language, sentences in the programming language C++ are made of words. Considering that C++ is a language focused on computation, it should not be surprising that numbers are among the most frequent words employed. Words of this kind are combined into "sentences" using symbols such as addition (+), multiplication (*), subtraction (-) and division (/). In the programming language C++, combinations of these numbers and symbols look quite like standard arithmetic expressions. For example

5 + 3.14

or:

2*6+7

In programming language terminology, these are known as expressions.

The process of finding out the value of an expression is called the evaluation. Evaluating an expression is the same as calculating or computing its result. Therefore, values of the preceding two expressions can be evaluated:

  • the expression 5 + 3.14 is evaluated to the value 8.14
  • the expression 2*6+7 is evaluated to the value 19

Expressions are not exactly valid C++ sentences. To make them into valid C++ sentences, they need to be terminated by a semicolon like this:

5 + 3.14;

2*6+7;

An expression terminated by a semicolon is one way of creating a statement.