|
||||||
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++ CodeThe 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 OperandsA typical C++ statement contains operators and operands. Operators are the key words and symbols used to carry out operations, for example:
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 StatementsEach 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. SummaryEach C++ program consists of a series of statements. Each statement:
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.
|
||||||
|
|
||||||
|
|
||||||