2. First Programs


Here is a program that computes the number of hours in one week:

int main()
{ cout << "A week has " << 24*7 << " hours." << endl; }

You might find it unusual that we have put braces (that is, curly brackets) on the same line as the first statement, but that has no impact on the outcome of this program.

The entire previous program can be written in a single line:

int main() { cout << "A week has " << 24*7 << " hours." << endl; }

or in several lines:

    ){ cout << 
"A week has " << 24
    *7 <<             " hours." 

When it comes to whitespaces and rows, C++ is a free-form language, which means that we are allowed to subdivide our programs into any number of rows and in any way we prefer (the only thing not allowed is splitting a word of the language or a literal expression across multiple lines). However, the Guild of Wizards has issued a proclamation that all programs should be written in an orderly fashion so that they are easier to read, which should ultimately lead to a smaller number of potentially catastrophic errors. The last example is in opposition to this proclamation, so we will not write such disorderly programs anymore, and neither should you.

Execution of this program results in the following output:


A week has 168 hours.