2. First Programs

Strings

A person viewing only the result of the previous program has no idea what it was about. To remedy that, we will rewrite the program so that it prints out some useful information, which will make it clear to any potential observer what the program is doing:

int main()
{
    cout << "This is my third program!" << endl;
    cout << "5+3.14 = " << 5+3.14 << endl;
}

As you can see, there are several novelties in this program. First, the text enclosed by double quotation signs "This is my third program!" is what we call a string. Unlike expressions, strings are sent to standard output exactly as they are stated (with some exceptions). In terminology of programming languages, the symbols that the strings are made of are commonly called characters. The symbols sent to standard output are also characters, as the word cout is short for "character output".

The second novelty is a new word: endl (usually pronounced e-n-d-l, or end-l) which is short for "end of line". It is used to advance to the next line of output.

The third and final novelty is that we are faced with a more complicated statement: there are two insertion operators in the first statement, separating the word cout, a string and the word endl. We carry out a statement such as this in a left-to-right fashion.

Therefore, the first thing that this statement would write out is the string "This is my third program!". Next, we encounter the word endl. It, too, must be "written out", the effect of which is to advance to the next line of output. Having done that, we have fully executed the first statement:

STDOUT:

This is my third program!    

<<<<<<

Future output will continue on this line.

Let us execute the second statement now. Proceeding from left to right, the first thing we encounter is the string "5+3.14 = ". At first sight, it seems like there is an expression inside a string. However, that is only an illusion. In C++, strings cannot contain expressions, but plain text only. We just have to output the string literally as it is stated:

STDOUT:

This is my third program!
5+3.14 =

Do not forget that there is a space character (it is invisible) after the equals sign.

We now run into an expression. As we have already seen, expressions are first evaluated before being sent to standard output. Therefore, what next appears on our piece of paper is the number 8.14. Finally, we "write out" the word endl advancing to the next output line. Having done all of that, our piece of paper should look like this:

STDOUT:

This is my third program! 
5+3.14 = 8.14

<<<<<<

Future output would continue on this line.

As the execution of a program is complete once the final statement in the function main is executed, we have completed the execution of this program.

We will conclude with two short comments. While reading the program, you might have wondered why there were so many insertion operators. In short, insertion operators are necessary to separate items to be written out.

Secondly, have you paid attention to the order of execution of statements in this program? In a C++ program, statements are executed in order from the first statement at the top to the last statement at the bottom. This means there is an implicit notion of time in the text of our programs, and that the order of statements affects the outcome of a program.