How to Use C++ Expressions

What is a C++ Expression or a Statement?

© Mark Alexander Bain

Feb 21, 2009
Creating C++ Statements, Mark Alexander Bain
Any C++ program consists of a series of expressions and statements, but what exactly are they?

The starting point for any C++ program is simply a sequence of statements typed into a text file by a programmer. These statements are (mainly) free-form, so that they can be laid out in ways that the programmer feels may highlight the code. The only real restriction is that a semi-colon (;) must be used to end any statement.

Laying out C++ Code

The actual layout of the code that makes up a program is ignored by the compiler, but not by the preprocessor. This means that any statements to be used by the preprocessor must be laid out on separate lines:

#include

However, no there are no such restrictions for the compiler, for example:

int
main
()
{
cout
<<
"Code all on separate lines"
;
}

The above is handled in exactly the same way as:

int main(){cout<<"Clump it all together";}

The only real consideration, therefore, is how easy the code is to read by humans and not by computers:

int main () {
cout <<"Make the code easy to read";
}

The only real rule is to choose a style that is easy to read and then stick to it.

Writing C++ Statements: Operators and Operands

A typical C++ statement contains operators and operands. Operators are the key words and symbols used to carry out operations, for example:

  • + (addition)
  • - (subtractions)
  • * (multiplication)
  • == (equals)
  • != (doesn't equal)

These are used in conjunction with operands. Operands are the variables that the operators operate on. Combined they produce something like:

int choice;
const int target = 7;
cout << "Please enter 1 - 10:";
cin >> choice;
if (choice == target) cout << "Correct";

In this case the equals operator has been used with two operands (choice and target) in a simple if statement. Of course most programs require something a little more complex than that.

C++ Compound and Nested Statements

Each individual statement ends in a semi-colon. However, statements can be grouped together into a single (compound) statement by making use of braces (or curly brackets - {}). This also allows one statement to be nested inside another:

while (choice != target) {
cout << "Please enter 1 - 10:";
cin >> choice;
if (choice > target) {
cout << "Too big\n";
} else if (choice < target) {
cout << "Too small\n";
} else {
cout << "Correct";
}
}

In this example series of if..then..else statements have been nested in a compound statement (within the when statement). This idea of nesting can be seen more clearly by extending the if statements further:

const int max = 10;
const int way_out = 100;
if (choice > target) {
cout << "Too big\n";
if (choice > max ) {
cout << "Much too big\n";
if (choice >= way_out ) {
cout << "Really, really far out\n";
}
}
}

With just these few statements the programmer can create a very simple yet effective program.

Summary

Each C++ program consists of a series of statements. Each statement:

  • ends in a semi-colon
  • usually consist of a number of operators and operands
  • can be grouped into a compound statement
  • can be nested
  • can be laid out according the human readability rather than computer readability

Even the most complex program can be created by just these simple building blocks.


The copyright of the article How to Use C++ Expressions in C Programming is owned by Mark Alexander Bain. Permission to republish How to Use C++ Expressions in print or online must be granted by the author in writing.


Creating C++ Statements, Mark Alexander Bain
C++ Code, Mark Alexander Bain
Compiling and Running C++ Code, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo