Statements, Null Statements, and Compound Statements

 < Day Day Up > 



Before talking about if statements, for statements, do statements, and the like, it will be a big help to you to understand statements, null statements, and compound statements. If all this talk about statements is making you dizzy just hang on, it will soon make perfect sense.

Statements

You have seen various statement forms used in the preceding chapters. For instance, expressions, discussed in chapter 5, are a form of statement known as expression-statements. Statements are terminated by a semicolon “;”. A statement will usually result in something happening, as in the case of expression statements that perform a calculation, or assignment expressions where a value is being assigned to some memory location. (remember lvalues and rvalues?) Here are a few examples of statements:

int a; a = 1;  for(int i=0; i<some_val; i++)     cout<<i<<endl; 

The first statement is a declaration statement; the variable name a is being introduced. The second statement is an assignment expression, a.k.a., expression-statement. The third example is a for statement, which will be discussed below. For now, however, see where the semicolon occurs in the for statement. It could have been written like so...

for(int i=0; i<some_val; i++) cout<<i<<endl; 

...and you will no doubt see short for statements written in this format. The semicolon also serves as a sequence point meaning the statement it terminates and all side effects associated with the statement will be fully executed before the next statement executes.

Statements are executed sequentially, that is, one right after the other, unless the flow of program control is changed with a selection statement, iteration statement, or goto statement.

Null Statements

Sometimes you need a statement that does nothing. A semicolon appearing by itself serves as a null statement. The following is a null statement:

;

The null statement doesn’t have to be on a line by itself but it often appears that way; it could immediately follow another statement. The following code shows an expression statement followed by a null statement:

int a = 25; ;

It does look weird doesn’t it? Regardless, keep the null statement in mind. You will see it again soon being put to good use.

Compound Statements

Throughout the rest of this chapter you will see blocks like this:

Where you can use a statement, you can also use a compound statement, otherwise known as a block statement. Compound statements are formed using the opening and closing braces { }. The following code is an example of a compound or block statement:

{  int i = 3;  cout<<i<<endl; }

You will rarely see compound statements used in the middle of nowhere. They are more often employed in if statements, for statements, while statements, etc. If the body of a selection statement or iteration statement requires more than one statement a compound statement can be used. You will see compound statements used heavily throughout the rest of this chapter.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net