Home arrow Practices arrow Page 5 - Basic Ideas

C++ Statements and Statement Blocks - Practices

This article will give you a good understanding of the basic concepts and practices of the C++ language, so that you will have the foundation to eventually learn these ideas in detail as you continue working with the language. It is excerpted from Ivor Horton's Beginning ANSI C++ The Complete Language (Apress, 2004; ISBN 1590592271).

  1. Basic Ideas
  2. Interpreted vs. Compiled Program Execution
  3. A Simple C Program
  4. Names Using Extended Character Sets
  5. C Statements and Statement Blocks
  6. Creating an Executable from Your Source Files
  7. C Source Characters
  8. Whitespace in Statements
  9. Procedural and Object-Oriented Programming
By: Apress Publishing
Rating: starstarstarstarstar / 18
March 23, 2005

print this article



Statements are the basic units for specifying what your program is to do and the data elements it acts upon. Most C++ statements end with a semicolon (;). There are quite a few different sorts of statements, but perhaps the most fundamental is a statement that introduces a name into your program source file.

A statement that introduces a name into a source file is called a declaration. A declaration just introduces the name and specifies what kind of thing the name refers to, as opposed to a definition, which results in allocation of some memory to accommodate whatever the name refers to. As it happens, most declarations are also definitions.

A variable is a place in memory in which you can store an item of data. Here’s an example of a statement that declares a name for a variable, and defines and initializes the variable itself:

double result = 0.0;

This statement declares the name result will be used to refer to a variable of type double (declaration), causes memory to be allocated to accommodate the variable (definition), and sets its initial value to 0.0 (initialization).

Here’s an example of another kind of statement called a selection statement:

if (length > 25)
  boxLength = size + 2;

This statement tests the condition “Is the value of length greater than 25?” and then executes the statement on the second line if that condition is true. The statement on the second line adds 2 to the value stored in the variable size and stores the result in the variable boxLength. If the condition tested isn’t true, then the second line won’t be executed, and the program will continue on its merry way by executing whatever comes next in the program.

You can enclose several statements between a pair of curly braces, { }, in which case they’re referred to as a statement block. The body of a function is an example of a block, as you saw in the first example program where the statements in the body of the main() function appear between curly braces. A statement block is also referred to as a compound statement, because in many circumstances it can be considered as a single statement, as you’ll see when we look at C++’s decision-making capabilities in Chapter 4. In fact, wherever you can put a single statement in C++, you can equally well put a block of statements between braces. As a consequence, blocks can be placed inside other blocks—this concept is called nesting.In fact, blocks can be nested, one within another, to any depth you need.

A statement block also has important effects on the variables that you use to store data items, but I defer discussion of this until Chapter 3, where I cover variable scope.

Code Presentation Style

The way in which you arrange your code visually can have a significant effect on how easy it is to understand. There are two basic aspects to this. First, you can use tabs and/or spaces to indent program statements in a manner that provides visual cues to their logic, and you can arrange matching braces that define program blocks in a consistent way so that the relationships between the blocks are apparent. Second, you can spread a single statement over two or more lines when that will improve the readability of your program. A particular convention for arranging matching braces and indenting statements is a presentation style.

There are a number of different presentation styles in use. The following code shows three examples of how the same code might look in three commonly used styles:

namespace mine { namespace mine namespace mine {
int test() { int test()
{ int test() {
if(isGood) { { if(isGood) {
good(); if(isGood) good();
return 0; good(); return 0;
} else return 0; } else
return 1; } else return 1;
} return 1; }
} } }

In this book I have used the style shown on the right for all the examples. I chose this because I think it is clear without being too extravagant on space. It doesn’t matter much which style you use as long as you are consistent.

Program Structure

Each of your C++ programs will consist of one or more files. By convention, there are two kinds of file that you can use to hold your source code: header files and source files. You use header files to contain code that describes the data types that your program needs, as well as some other sorts of declarations. These files are referred to as header files because you usually include them at the beginning (the “head”) of your other source files. Your header files are usually distinguished by having the filename extension .h, although this is not mandatory, and other extensions, such as .hxx, are used to identify header files in some systems.

Your source files, which have the filename extension .cpp, contain function defintions—the executable code for your program. These will usually refer to declarations or definitions for data types that you have defined in your own header files. The compiler will need to know about these when it compiles your code so you specify the .h files that are needed in a .cpp file by means of #include directives at the beginning of the file. An #include directive is an instruction to the compiler to insert the contents of a particular header file into your code. You’ll also need to add #include directives for any standard library header files that your code requires.

Figure 1-3 shows a program in which the source code is contained in two .cpp files and three header files. The first .cpp file uses the information from the first two header files, and the second .cpp file requires the contents of the last two header files. You’ll learn more about the #include directives that do this in Chapter 10.

Figure 1-3.  Source files in a C++ program

A number of standard headers are supplied with your compiler and contain declarations that you need in order to use the standard library facilities. They include, for example, declarations for the available standard library functions. The first .cpp file in Figure 1-3 includes the <iostream> header, which you met in the example C++ program. As you may have noticed, in this instance the name for the header has no extension. In fact, to distinguish them from other header files that you may use, the standard header names for C++ have no extension. The standard headers are often referred to as standard header files because that’s how they’re usually implemented. However, the C++ standard doesn’t require that the headers be files, so they may not be in some implementations.

NOTE  Appendix C provides details on the ANSI/ISO standard library headers.

Your compiler system may have a whole range of other header files, providing the definitions necessary to use operating system functions, or other goodies to save you programming effort. This example shows just a few header files in use, but in most serious C++ applications many more will be involved.

Program Functions and Execution

As already noted, a C++ program consists of at least one function that will be called main(), but typically a program consists of many other functions—some that you will have written and others from the standard library. Your program functions will be stored in a number of source files that will typically have filenames with the extension .cpp, although other extensions such as .cxx and .cc are common.

Figure 1-4 shows an example of the sequence of execution in a program that consists of several functions. Execution of main() starts when it’s invoked by the operating system. All the other functions in your program are invoked by main() or by some other function in the set. You invoke a function by calling it. When you call a function, you can pass items of data to it for use while it’s executing. The data items that you want to pass to a function are placed between parentheses following the function name in the call operation. When a function finishes executing, execution control returns to the point at which it was called in the calling function.

Figure 1-4.  How program functions execute

A function can also return a value to the calling point when it finishes executing. The value returned can be stored for use later or it can participate in a calculation of some kind—in an arithmetic expression, for example. You’ll have to wait until Chapter 8 to learn how you can create your own functions, but you’ll use functions from the standard library early in the next chapter.

This article is excerpted from Beginning ANSI C++ The Complete Language by Ivor Horton (Apress, 2004; ISBN  1590592271). Check it out at your favorite bookstore today. Buy this book now.

>>> More Practices Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Calculating Development Project Costs
- More Techniques for Finding Things
- Finding Things
- Finishing the System`s Outlines
- The System in So Many Words
- Basic Data Types and Calculations
- What`s the Address? Pointers
- Design with ArgoUML
- Pragmatic Guidelines: Diagrams That Work
- Five-Step UML: OOAD for Short Attention Span...
- Five-Step UML: OOAD for Short Attention Span...
- Introducing UML: Object-Oriented Analysis an...
- Class and Object Diagrams
- Class Relationships
- Classes

Developer Shed Affiliates


Dev Shed Tutorial Topics: