Defining Variables
Now that we are beyond our first program, we are going to want to manipulate some meaningful information and get some answers. An essential element in this process is having a piece of memory that we can call our own, that we can refer to using a meaningful name and where we can store an item of data. Each individual piece of memory so specified is called a variable.
Each variable will store a particular kind of data, which is fixed when we define the variable in our program. One variable might store whole numbers (that is, integers), in which case it couldn't be used to store numbers with fractional values. The value that each variable contains at any point is determined by the instructions in our program and, of course, its value will usually change many times as the program calculation progresses.
Let's look first at the rules for naming a variable when we introduce it into a program.
Naming Variables
The name we give to a variable is called an identifier, or more conveniently, a variable name. Variable names can include the letters A-z (upper or lower case), the digits 0-9 and the underscore character. All other characters are illegal. Variable names must also begin with either a letter or an underscore. Names are usually chosen to indicate the kind of information to be stored.
In Visual C++, variable names can be up to 247 characters long, which gives you a reasonable amount of flexibility. In fact, as well as variables, there are quite a few other things that have names in C++. We shall see they too can have names of up to 247 characters, with the same definition rules as a variable name. Using names of the maximum length can make your programs a little difficult to read and, unless you have amazing keyboard skills, they are the very devil to type in. A more serious consideration is that not all compilers support such long names. If you anticipate compiling your code in other environments, names with up to 31 characters are usually adequate and will not cause problems in most instances.
Although you can use variable names that begin with an underscore, for example _this and _that, this is best avoided, because there are potential clashes with standard system variables which have the same form. You should also avoid using names starting with a double underscore for the same reason.
Examples of good variable names are:
Price
discount
pShape
Value_
COUNT
8_Ball, 7Up, and 6_pack are not legal. Neither is Hash! or Mary-Ann. This last example is a common mistake, although Mary_Ann would be quite acceptable. Of course, Mary Ann would not be, because blanks are not allowed in variable names. Note that the variable names republican and Republican are quite different, as upper- and lower-case letters are differentiated.
Keywords in C++
There are reserved words in C++, also called keywords, which have special significance within the language. They will be highlighted with a particular color by the Visual C++ editor as you enter your program. If the keywords you type do not appear highlighted, then the keyword has been entered incorrectly.
Remember that keywords, like the rest of the C++ language, are case-sensitive.
For example, the program that you entered earlier in the chapter contained the keywords int and return. You will see many more as you progress through the book. You must ensure that the names you choose for entities in your program, such as variables, are not the same as any of the keywords in C++. You can find a complete list of the C++ keywords in the online help, if you look under Visual C++ Documentation \ Reference \ C/C++ Language and C++ Libraries \ C++ Language Reference \ Lexical Conventions \ C++ Keywords.
Declaring Variables
A variable declaration is a program statement which specifies the name of a variable and the sort of data that it can store. For example, the statement,int value;
declares a variable with the name value that can store integers. The type of data that can be stored in the variable value is specified by the keyword int. Because int is a keyword, you can't use int as a name for one of your variables.
Note that a declaration always ends with a semicolon.
A single declaration can specify the names of several variables but, as we have said, it is generally better to declare variables in individual statements, one per line. We will deviate from this from time to time, but only in the interests of keeping the code reasonably compact.
A variable name alone can't store anything, so it's not much use on its own. In order to store data (for example, the value of an integer), we need to assign a piece of the computer's memory to the variable. This process is called variable definition.
In C++, a variable declaration is also a definition (except in a few special cases, which we shall come across during the book). In the course of a single statement, we introduce the variable name, and also tie it to an appropriately-sized piece of memory. So, the statement int value;
is both a declaration and a definition. We use the variable name value that we declared, to access the piece of the computer's memory that we defined.
We use the term declaration when we introduce a name to our program, with information on what the name will be used for. The term definition refers to the allotment of computer memory to the name. In the case of variables, we can declare and define in a single statement, as in the line above.
You must declare a variable at some point between the beginning of your program and when the variable is used for the first time. In C++, it is good practice to declare variables close to their first point of use.
Initial Values for Variables
When you declare a variable, you can also assign an initial value to it. A variable declaration that assigns an initial value to a variable is called an initialization. To initialize a variable when you declare it, you just need to write an equals sign followed by the initializing value after the variable name. We can write the following statements to give each of the variables an initial value:int value = 0;
int count = 10;
int number = 5;
In this case, value will have the value 0, count will have the value 10 and number will have the value 5. These three statements are each declarations, definitions and initializations.
There is another way of writing the initial value for a variable in C++ called functional notation. Instead of an equals sign and the value, you can simply write the value in parentheses following the variable name. So we could rewrite the previous declarations as:int value(0);
int count(10);
int number(5);
If you don't supply an initial value for a variable, then it will usually contain whatever garbage was left in the memory location it occupies by the previous program you ran (there is an exception to this which we shall see later). Wherever possible, you should initialize your variables when you declare them. If your variables start out with known values, it makes it easier to work out what is happening when things go wrong. And if there's one thing you can be sure of, it's that things will go wrong.
Thursday, September 27, 2007
The Structure of a C++ Program
Programs which will run as console applications under Visual C++ are text-based MS-DOS programs. All the examples that we'll write to understand how C++ works will be MS-DOS programs, so let's look at how such programs are structured.
A program in C++ consists of one or more functions. Every C++ program in the DOS environment contains the function main() and all C++ programs of any size consist of several functions. A function is simply a self-contained block of code with a unique name which is invoked by using the name of the function.
A typical DOS program might be structured as shown in the figure:
The figure above illustrates that execution of the program shown starts at the beginning of the function main(). From main(), execution transfers to a function input_names(), which returns execution to the position immediately following the point where it was called in main(). The function sort_names() is then called from main() and, once control returns to main(), the final function output_names() is called. Eventually, once output has been completed, execution returns once again to main() and the program ends.
Of course, different programs under DOS may have radically different functional structures, but they all start execution at the beginning of main(). The principal advantage of having a program broken up into functions is that you can write and test each piece separately. There is a further advantage in that functions written to perform a particular task can be re-used in other programs. The libraries that come with C++ provide a lot of standard functions that you can use in your programs. They can save you a great deal of work.
We'll see more about creating and using functions in Chapter 4.
Try It Out - A Simple Program
Let's look at a simple example to understand the elements of a program a little better. Start by creating a new project from the range of alternatives offered on the Projects tab when you click the New... item in the File menu. When the dialog appears, select Win 32 Console Application and name the project Ex1_01; then click on OK.
Next, the Console Application dialog appears. We'll start from the very basic project structure, so choose An empty project, and click on the Finish button.
The New Project Information tells you about the project you're about to create; it should look something like this:
The project directory at the foot of the screen may be slightly different on your machine; everything else should be identical. Once your happy with it, click on OK.
We'll start by entering the following program as a new source file, so click the leftmost icon on the standard toolbar, , to create a new text file into which you can type the code.
Note that this code is available for download from the Wrox Press website: http://www.wrox.com, as are all of the code examples in this tutorial.// EX1_01.CPP
// A Simple Example of a Program
#include
using namespace std;
int main()
{
int apples, oranges; // Declare two integer variables
int fruit; // ...then another one
apples = 5; oranges = 6; // Set initial values
fruit = apples + oranges; // Get the total fruit
cout << endl; // Start output on a new line
cout << "Oranges are not the only fruit... " << endl
<< "- and we have " << fruit << " fruits in all.";
cout << endl; // Start output on a new line
return 0; // Exit the program
}
The above example is intended to illustrate some of the ways in which you can write C++ statements, rather than to be a model of good programming style.
Note that in the word endl ends with the letter l, not with the numeral 1.
Once you have keyed it in, save it as Ex1_01.cpp using the File menu Save As... option. The Save As dialog will offer to save it in the project directory that you just created and, since this file is part of that project, that's the best place for it. Since you have now identified the file by its extension as a file containing C++ code, the keywords in the code will gain their colors. You will be able to see if you have entered Int where you should have entered int, since the two will be different colors.
If you look at the FileView tab for your new project, you'll notice that the source file doesn't appear as part of the project's files. We need to add the file to the project using the Add to Project... item from the Project menu, or by right-clicking in the window containing the source and selecting Insert File into Project. Once this is done, you'll see the newly created source file in the FileView, and the main() function will appear under the Globals section of the ClassView. We'll consider the meaning of this later.
If you now build this program by using the Build button on the Project toolbar, , and execute it using the Execute Ex1_01.exe item in the Build menu, or by pressing the Execute Program button, , you should get the following output:
Program Comments
The first two lines in the C++ code are comments. Comments are an important part of any program, but they're not executable code - they are there simply to help the human reader. All comments are ignored by the compiler. On any line of code, two successive slashes // that are not contained within a text string (we shall see what text strings are later) indicate that the rest of the line is a comment.
You can see that several lines of the program contain comments as well as program statements. You can also use an alternative form of comment bounded by /* and */. For example, the first line of the program could have been written:/* EX1_01.CPP */
The comment using // only covers the portion of the line following the two successive slashes, whereas the /*...*/ form defines whatever is enclosed as a comment and can span several lines. For example, we could write:/*
EX1_01.CPP
A Simple Program Example
*/
All four lines are comments. If you want to highlight some particular comment lines, you can always embellish them with a frame of some description:/*****************************
* EX1-01.CPP *
* A Simple Program Example *
*****************************/
As a rule, you should always comment your programs comprehensively. The comments should be sufficient for another programmer, or you at a later date, to understand the purpose of any particular piece of code, and to understand how it works.
The #include Directive - Header Files
Following the comments, we have the #include directive,#include
which makes the compiler insert the contents of the file iostream into the program before compilation. This file is called a header file because it's usually brought in at the beginning of a program file. This particular header file contains definitions that are necessary for you to be able to use input and output statements in C++. If we didn't include iostream in our program, it wouldn't compile because we use output statements which depend on some of the definitions in this file. There are many different header files provided by Visual C++ and they cover a wide range of capabilities. We shall be seeing more of them as we progress through the language facilities.
A #include statement is one of several preprocessor directives. The Visual C++ editor recognizes these and highlights them in blue in your edit window (as it does with all the C++ keywords). Preprocessor directives are commands executed by the compiler that generally act on your source code in some way before it is compiled. They all start with the # character. We'll be introducing other preprocessor directives as we need them.
The using Directive - Namespaces
The standard library is an extensive set of routines which have been written to do many common tasks: for example, dealing with input and output, performing basic mathematical calculations, etc. Instead of writing these routines for yourself, you can simply pluck them out of the standard library and use them in your code. The file iostream is just one of a number of header files that contain the routines of the standard library. (You can see a full list of the standard library files in the online MSDN help files in Visual C++ Documentation \ Reference \ C/C++ Language and C++ Libraries \ Standard C++ Library Reference.)
The code for all of these standard library routines is contained within the namespace std. So, any standard library routine belongs to the namespace std. Each of the standard library header files contributes a few routines to the namespace std.
The code in our program does not belong to the namespace std. Therefore, to use the output routines in the iostream header file, we need to tell the compiler that the routines belong to the namespace std. In order to do this, we use the using directive:using namespace std;
With this line in our program, the compiler knows that we will be using routines that belong to the standard library. There are other ways of dealing with this, and we'll see those later, when we'll go into namespaces in more detail in Chapter 5, when we will be able to create our own namespaces.
The Function main()
The function main() in our example consists of the function header defining it as main() plus everything from the first opening curly brace, {, to the corresponding closing curly brace, },. The curly braces enclose the executable statements in the function, which are referred to collectively as the body of the function.
As we shall see, all functions consist of a header which defines (amongst other things) the function name, followed by the function body which consists of a number of program statements enclosed between a pair of curly braces. The body of a function may contain no statements at all, in which case it doesn't do anything.
A function that doesn't do anything may seem somewhat superfluous, but when you're writing a large program, you may map out the complete program structure in functions but, initially, leave the code for many of them with empty bodies. Doing this means that you can compile and execute the whole program with all its functions at any time, but add detailed coding for the functions incrementally.
Program Statements
The program statements making up the function body of main() are each terminated with a semicolon. The program statement is the basic unit in defining what a program does. This is a bit like a sentence in a paragraph of text, where each sentence stands by itself in expressing an action or an idea, but relates to and combines with the other sentences in the paragraph in expressing a more general idea. A statement is a self-contained definition of an action that the computer is to carry out, but which can be combined with other statements to define a more complex action or calculation.
The action of a function is always expressed by a number of statements, each ending with a semicolon. Let's take a quick look at each of the statements in the example that we have just written, just to get a general feel for how it works. We will discuss each type of statement more fully later in this chapter.
The first statement in the program,int apples, oranges; // Declare two integer variables
declares two variables, apples and oranges. A variable is a named bit of computer memory that you can use to store data. A statement introducing the names of variables is called a variable declaration. The keyword int indicates that the variables are to store values that are whole numbers, or integers. The next statement declares another integer variable, fruit. While you can declare several variables in the same statement, as we did for apples and oranges, it is generally a good idea to declare them separately. This enables you to comment them individually.
In the example, the line,apples = 5; oranges = 6; // Set initial values
contains two statements, each terminated by a semicolon. While it isn't obligatory, it's good programming practice to write only one statement on a line. The two statements store the values 5 and 6 in the variables apples and oranges respectively. These statements are called assignment statements, because they assign a new value to a variable.
The next statement,fruit = apples + oranges; // Get the total fruit
is also an assignment statement. This one adds the values stored in the variables apples and oranges and stores the result in the variable fruit.
The next three statements are:cout << endl; // Start output on a new line
cout << "Oranges are not the only fruit... " << endl
<< "- and we have " << fruit << " fruits in all.";
cout << endl; // Start output on a new line
These are all output statements. The first sends a newline character, denoted by the word endl, to the screen. In C++, a source of input or a destination for output is referred to as a stream. The word cout specifies the 'standard' output stream, and the operator << indicates that what appears to the right of the operator is to be sent to the output stream, cout. The operator << 'points' in the direction that the data flows - from the variable or string on the right to the output destination on the left.
The meaning of the word cout and the operator << are defined by the contents of the header file iostream, which you'll remember we added to our program code by placing the #include directive at the beginning of the program. Because cout has been defined to send the standard output stream to your display screen, you shouldn't use the word cout for other purposes - for example, as a variable in your program.
The second statement sends a text string (defined between quotes) to the screen, followed by another newline character (endl), then another text string, followed by the value stored in the variable fruit, then finally another text string. There is no problem stringing together a sequence of things that you want to output in this way. The statement executes from left to right, with each item being sent to cout in turn. Note that each item is preceded by its own << operator.
The third statement sends another newline character to the screen. These statements produce the output from the program that you see. Note that the second statement runs over two lines. The successive lines are combined into a single statement until the compiler finds the semicolon that defines the end of the statement. This means that if you forget a semicolon for a statement, the compiler will assume the next line is part of the same statement and join them together. This usually results in something the compiler cannot understand, so you'll get an error when you try to compile the code.
The last statement in our program,return 0; // Exit the program
stops execution of the program and returns control to the operating system. We will be discussing all of these statements in more detail later on.
The statements in a program are executed in the sequence in which they are written, unless a statement specifically causes the natural sequence to be altered. In Chapter 3, we will look at statements which alter the sequence of execution.
Whitespace
Whitespace is the term used in C++ to describe blanks, tabs, newline characters and comments. Whitespace separates one part of a statement from another and enables the compiler to identify where one element in a statement, such as int, ends and the next element begins. Therefore, in the statement,int fruit; // ...then another one
there must be at least one whitespace character (usually a space) between int and fruit for the compiler to be able to distinguish them. On the other hand, in the statementfruit = apples + oranges; // Get the total fruit
no whitespace characters are necessary between fruit and =, or between = and apples, although you are free to include some if you wish. This is because the = is not alphabetic or numeric, so the compiler can separate it from its surroundings. Similarly, no whitespace characters are necessary either side of the + sign.
Apart from its use as a separator between elements in a statement that might otherwise be confused, whitespace is ignored by the compiler (except, of course, in a string of characters between quotes). You can, therefore, include as much whitespace as you like to make your program more readable, as we did when we spread our output statement in the last example over several lines. In some programming languages, the end of a statement is at the end of the line, but in C++ the end of a statement is wherever the semicolon occurs.
Since variable names must be made up of single words, you must not put whitespace characters in the middle. If you do, the single variable name won't be seen by the compiler as such, and it won't be interpreted correctly.
Statement Blocks
We can enclose several statements between a pair of curly braces, in which case they become a block, or a compound statement. The body of a function is an example of a block. Such a compound statement can be thought of as a single statement (as we shall see when we look at the decision making possibilities in C++ in the next chapter. In fact, wherever you can put a single statement in C++, you could equally well put a block of statements between braces. As a consequence, blocks can be placed inside other blocks. In fact, blocks can be nested, one within another, to any depth.
A statement block also has important effects on variables, but we will defer discussion of this until later in this chapter when we discuss something called variable scope
A program in C++ consists of one or more functions. Every C++ program in the DOS environment contains the function main() and all C++ programs of any size consist of several functions. A function is simply a self-contained block of code with a unique name which is invoked by using the name of the function.
A typical DOS program might be structured as shown in the figure:
The figure above illustrates that execution of the program shown starts at the beginning of the function main(). From main(), execution transfers to a function input_names(), which returns execution to the position immediately following the point where it was called in main(). The function sort_names() is then called from main() and, once control returns to main(), the final function output_names() is called. Eventually, once output has been completed, execution returns once again to main() and the program ends.
Of course, different programs under DOS may have radically different functional structures, but they all start execution at the beginning of main(). The principal advantage of having a program broken up into functions is that you can write and test each piece separately. There is a further advantage in that functions written to perform a particular task can be re-used in other programs. The libraries that come with C++ provide a lot of standard functions that you can use in your programs. They can save you a great deal of work.
We'll see more about creating and using functions in Chapter 4.
Try It Out - A Simple Program
Let's look at a simple example to understand the elements of a program a little better. Start by creating a new project from the range of alternatives offered on the Projects tab when you click the New... item in the File menu. When the dialog appears, select Win 32 Console Application and name the project Ex1_01; then click on OK.
Next, the Console Application dialog appears. We'll start from the very basic project structure, so choose An empty project, and click on the Finish button.
The New Project Information tells you about the project you're about to create; it should look something like this:
The project directory at the foot of the screen may be slightly different on your machine; everything else should be identical. Once your happy with it, click on OK.
We'll start by entering the following program as a new source file, so click the leftmost icon on the standard toolbar, , to create a new text file into which you can type the code.
Note that this code is available for download from the Wrox Press website: http://www.wrox.com, as are all of the code examples in this tutorial.// EX1_01.CPP
// A Simple Example of a Program
#include
using namespace std;
int main()
{
int apples, oranges; // Declare two integer variables
int fruit; // ...then another one
apples = 5; oranges = 6; // Set initial values
fruit = apples + oranges; // Get the total fruit
cout << endl; // Start output on a new line
cout << "Oranges are not the only fruit... " << endl
<< "- and we have " << fruit << " fruits in all.";
cout << endl; // Start output on a new line
return 0; // Exit the program
}
The above example is intended to illustrate some of the ways in which you can write C++ statements, rather than to be a model of good programming style.
Note that in the word endl ends with the letter l, not with the numeral 1.
Once you have keyed it in, save it as Ex1_01.cpp using the File menu Save As... option. The Save As dialog will offer to save it in the project directory that you just created and, since this file is part of that project, that's the best place for it. Since you have now identified the file by its extension as a file containing C++ code, the keywords in the code will gain their colors. You will be able to see if you have entered Int where you should have entered int, since the two will be different colors.
If you look at the FileView tab for your new project, you'll notice that the source file doesn't appear as part of the project's files. We need to add the file to the project using the Add to Project... item from the Project menu, or by right-clicking in the window containing the source and selecting Insert File into Project. Once this is done, you'll see the newly created source file in the FileView, and the main() function will appear under the Globals section of the ClassView. We'll consider the meaning of this later.
If you now build this program by using the Build button on the Project toolbar, , and execute it using the Execute Ex1_01.exe item in the Build menu, or by pressing the Execute Program button, , you should get the following output:
Program Comments
The first two lines in the C++ code are comments. Comments are an important part of any program, but they're not executable code - they are there simply to help the human reader. All comments are ignored by the compiler. On any line of code, two successive slashes // that are not contained within a text string (we shall see what text strings are later) indicate that the rest of the line is a comment.
You can see that several lines of the program contain comments as well as program statements. You can also use an alternative form of comment bounded by /* and */. For example, the first line of the program could have been written:/* EX1_01.CPP */
The comment using // only covers the portion of the line following the two successive slashes, whereas the /*...*/ form defines whatever is enclosed as a comment and can span several lines. For example, we could write:/*
EX1_01.CPP
A Simple Program Example
*/
All four lines are comments. If you want to highlight some particular comment lines, you can always embellish them with a frame of some description:/*****************************
* EX1-01.CPP *
* A Simple Program Example *
*****************************/
As a rule, you should always comment your programs comprehensively. The comments should be sufficient for another programmer, or you at a later date, to understand the purpose of any particular piece of code, and to understand how it works.
The #include Directive - Header Files
Following the comments, we have the #include directive,#include
which makes the compiler insert the contents of the file iostream into the program before compilation. This file is called a header file because it's usually brought in at the beginning of a program file. This particular header file contains definitions that are necessary for you to be able to use input and output statements in C++. If we didn't include iostream in our program, it wouldn't compile because we use output statements which depend on some of the definitions in this file. There are many different header files provided by Visual C++ and they cover a wide range of capabilities. We shall be seeing more of them as we progress through the language facilities.
A #include statement is one of several preprocessor directives. The Visual C++ editor recognizes these and highlights them in blue in your edit window (as it does with all the C++ keywords). Preprocessor directives are commands executed by the compiler that generally act on your source code in some way before it is compiled. They all start with the # character. We'll be introducing other preprocessor directives as we need them.
The using Directive - Namespaces
The standard library is an extensive set of routines which have been written to do many common tasks: for example, dealing with input and output, performing basic mathematical calculations, etc. Instead of writing these routines for yourself, you can simply pluck them out of the standard library and use them in your code. The file iostream is just one of a number of header files that contain the routines of the standard library. (You can see a full list of the standard library files in the online MSDN help files in Visual C++ Documentation \ Reference \ C/C++ Language and C++ Libraries \ Standard C++ Library Reference.)
The code for all of these standard library routines is contained within the namespace std. So, any standard library routine belongs to the namespace std. Each of the standard library header files contributes a few routines to the namespace std.
The code in our program does not belong to the namespace std. Therefore, to use the output routines in the iostream header file, we need to tell the compiler that the routines belong to the namespace std. In order to do this, we use the using directive:using namespace std;
With this line in our program, the compiler knows that we will be using routines that belong to the standard library. There are other ways of dealing with this, and we'll see those later, when we'll go into namespaces in more detail in Chapter 5, when we will be able to create our own namespaces.
The Function main()
The function main() in our example consists of the function header defining it as main() plus everything from the first opening curly brace, {, to the corresponding closing curly brace, },. The curly braces enclose the executable statements in the function, which are referred to collectively as the body of the function.
As we shall see, all functions consist of a header which defines (amongst other things) the function name, followed by the function body which consists of a number of program statements enclosed between a pair of curly braces. The body of a function may contain no statements at all, in which case it doesn't do anything.
A function that doesn't do anything may seem somewhat superfluous, but when you're writing a large program, you may map out the complete program structure in functions but, initially, leave the code for many of them with empty bodies. Doing this means that you can compile and execute the whole program with all its functions at any time, but add detailed coding for the functions incrementally.
Program Statements
The program statements making up the function body of main() are each terminated with a semicolon. The program statement is the basic unit in defining what a program does. This is a bit like a sentence in a paragraph of text, where each sentence stands by itself in expressing an action or an idea, but relates to and combines with the other sentences in the paragraph in expressing a more general idea. A statement is a self-contained definition of an action that the computer is to carry out, but which can be combined with other statements to define a more complex action or calculation.
The action of a function is always expressed by a number of statements, each ending with a semicolon. Let's take a quick look at each of the statements in the example that we have just written, just to get a general feel for how it works. We will discuss each type of statement more fully later in this chapter.
The first statement in the program,int apples, oranges; // Declare two integer variables
declares two variables, apples and oranges. A variable is a named bit of computer memory that you can use to store data. A statement introducing the names of variables is called a variable declaration. The keyword int indicates that the variables are to store values that are whole numbers, or integers. The next statement declares another integer variable, fruit. While you can declare several variables in the same statement, as we did for apples and oranges, it is generally a good idea to declare them separately. This enables you to comment them individually.
In the example, the line,apples = 5; oranges = 6; // Set initial values
contains two statements, each terminated by a semicolon. While it isn't obligatory, it's good programming practice to write only one statement on a line. The two statements store the values 5 and 6 in the variables apples and oranges respectively. These statements are called assignment statements, because they assign a new value to a variable.
The next statement,fruit = apples + oranges; // Get the total fruit
is also an assignment statement. This one adds the values stored in the variables apples and oranges and stores the result in the variable fruit.
The next three statements are:cout << endl; // Start output on a new line
cout << "Oranges are not the only fruit... " << endl
<< "- and we have " << fruit << " fruits in all.";
cout << endl; // Start output on a new line
These are all output statements. The first sends a newline character, denoted by the word endl, to the screen. In C++, a source of input or a destination for output is referred to as a stream. The word cout specifies the 'standard' output stream, and the operator << indicates that what appears to the right of the operator is to be sent to the output stream, cout. The operator << 'points' in the direction that the data flows - from the variable or string on the right to the output destination on the left.
The meaning of the word cout and the operator << are defined by the contents of the header file iostream, which you'll remember we added to our program code by placing the #include directive at the beginning of the program. Because cout has been defined to send the standard output stream to your display screen, you shouldn't use the word cout for other purposes - for example, as a variable in your program.
The second statement sends a text string (defined between quotes) to the screen, followed by another newline character (endl), then another text string, followed by the value stored in the variable fruit, then finally another text string. There is no problem stringing together a sequence of things that you want to output in this way. The statement executes from left to right, with each item being sent to cout in turn. Note that each item is preceded by its own << operator.
The third statement sends another newline character to the screen. These statements produce the output from the program that you see. Note that the second statement runs over two lines. The successive lines are combined into a single statement until the compiler finds the semicolon that defines the end of the statement. This means that if you forget a semicolon for a statement, the compiler will assume the next line is part of the same statement and join them together. This usually results in something the compiler cannot understand, so you'll get an error when you try to compile the code.
The last statement in our program,return 0; // Exit the program
stops execution of the program and returns control to the operating system. We will be discussing all of these statements in more detail later on.
The statements in a program are executed in the sequence in which they are written, unless a statement specifically causes the natural sequence to be altered. In Chapter 3, we will look at statements which alter the sequence of execution.
Whitespace
Whitespace is the term used in C++ to describe blanks, tabs, newline characters and comments. Whitespace separates one part of a statement from another and enables the compiler to identify where one element in a statement, such as int, ends and the next element begins. Therefore, in the statement,int fruit; // ...then another one
there must be at least one whitespace character (usually a space) between int and fruit for the compiler to be able to distinguish them. On the other hand, in the statementfruit = apples + oranges; // Get the total fruit
no whitespace characters are necessary between fruit and =, or between = and apples, although you are free to include some if you wish. This is because the = is not alphabetic or numeric, so the compiler can separate it from its surroundings. Similarly, no whitespace characters are necessary either side of the + sign.
Apart from its use as a separator between elements in a statement that might otherwise be confused, whitespace is ignored by the compiler (except, of course, in a string of characters between quotes). You can, therefore, include as much whitespace as you like to make your program more readable, as we did when we spread our output statement in the last example over several lines. In some programming languages, the end of a statement is at the end of the line, but in C++ the end of a statement is wherever the semicolon occurs.
Since variable names must be made up of single words, you must not put whitespace characters in the middle. If you do, the single variable name won't be seen by the compiler as such, and it won't be interpreted correctly.
Statement Blocks
We can enclose several statements between a pair of curly braces, in which case they become a block, or a compound statement. The body of a function is an example of a block. Such a compound statement can be thought of as a single statement (as we shall see when we look at the decision making possibilities in C++ in the next chapter. In fact, wherever you can put a single statement in C++, you could equally well put a block of statements between braces. As a consequence, blocks can be placed inside other blocks. In fact, blocks can be nested, one within another, to any depth.
A statement block also has important effects on variables, but we will defer discussion of this until later in this chapter when we discuss something called variable scope
Welcome to the Wrox Press C++ tutorial
Welcome to the Wrox Press C++ tutorial
"I hope you'll enjoy reading this tutorial with your portable, your work, or your home PC. It's a perfect companion to the Introduction to Visual C++ 6.0 Standard Edition manual and is a proven aid to understanding the C++ language. The material in this tutorial is adapted from my book Beginning Visual C++ 6.0, to provide you with a thorough grounding in 'pure' C++. I've been careful to address the new standards in C++ laid out by the ANSI and ISO committees and I encourage you to adopt these conventions so that your programs are maintainable for years to come. There's plenty of code in this tutorial, which you can cut and paste from the help viewer and is also available for download from the Wrox Press web site. We've tested this code extensively, but if you do encounter a problem, try visiting the web site, which will list any errata that may have been spotted since publication of this document. Also, remember that your suggestions for improving the text are very welcome - use the web site to give us your feedback. We try to keep all Wrox tutorials error free and appreciate your help in sustaining our high standards."
Ivor Horton
"I hope you'll enjoy reading this tutorial with your portable, your work, or your home PC. It's a perfect companion to the Introduction to Visual C++ 6.0 Standard Edition manual and is a proven aid to understanding the C++ language. The material in this tutorial is adapted from my book Beginning Visual C++ 6.0, to provide you with a thorough grounding in 'pure' C++. I've been careful to address the new standards in C++ laid out by the ANSI and ISO committees and I encourage you to adopt these conventions so that your programs are maintainable for years to come. There's plenty of code in this tutorial, which you can cut and paste from the help viewer and is also available for download from the Wrox Press web site. We've tested this code extensively, but if you do encounter a problem, try visiting the web site, which will list any errata that may have been spotted since publication of this document. Also, remember that your suggestions for improving the text are very welcome - use the web site to give us your feedback. We try to keep all Wrox tutorials error free and appreciate your help in sustaining our high standards."
Ivor Horton
Subscribe to:
Posts (Atom)