10/02/2016
A First Program
Any C++ program we write will be composed of a sequence of statements, like the ones we have just written, enclosed inside a piece of magic called the function 'main'.
For example, to make a program out of the statement
5 + 3.14;
we would have to write the following:
int main()
{
5 + 3.14;
}
By doing this, the statement '5 + 3.14;' has been made a part of a so-called function 'main'. This so-called function makes up the main (and in this case, the only) part of our program.
To execute this statement, we have to execute the expression it contains. An expression is executed by evaluation, it is as simple as that. In this case the expression 5 + 3.14 evaluates to the number 8.14.
However, what is the meaning of a semicolon symbol? In addition to being a way to terminate expressions and mark the end of a statement, it instructs the reader or the executor of the program to discard the value of the expression preceding it.
Now, this must strike you as bizarre! Why would one make an effort to obtain the result of adding 5 to 3.14 only to throw it away in the end?
Think of it this way. Say you tell your friend: "Calculate what 5+3.14 evaluates to!" in a rather imperative voice. Normally, you would expect him to respond with the result: "8.14!" However, should he not have understood your unstated request, you could quickly clarify it by saying: "Now tell me the result!" Having done that, you would have explicitly stated your true request, leaving no room for confusion.
In computer programming we do not have such a luxury. If we are to be understood at all, we have to make the extra effort and state explicitly and upfront that we would like to know the result of a particular computation. Since we have done no such thing in this program, the result of computing the expression 5 + 3.14 will be discarded and the whole effort rendered useless.