Clout 9
27/12/2020
2. First Programs
Standard Output
Our next step should be to write a program that actually communicates the results of its computations. In order to do that, we will have to introduce a new piece of magic, called standard output. It turns out that standard output is a rather involved notion, so, for the purposes of this chapter, we will pretend that this standard output thing is a piece of paper. In other words, all the programs that we shall write will "communicate" their results on a piece of paper. We can write 'STDOUT:' at the top of this paper so that we know what the paper stands for.
When writing a C++ program we will be referring to standard output by virtue of a word cout (pronounced see-out). This word is an abbreviation for "character output" because everything, regardless of its true nature, is sent to standard output in the form of characters. Without further ado, we present you with the same program rewritten so that it writes out its result on a piece of paper:
int main()
{
cout
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.
Click here to claim your Sponsored Listing.
Category
Contact the business
Website
Address
Kampala
256