|















|
C++ Programming Source-Code Indentation
The two columns below illustrate two possible indentation styles for
the source code of C or C++ programs.
For your programming work not related to this class, you should
choose one of the two styles and use it consistently in all of your programs.
For this class, use the style at left.
int main() int main() {
{ statements(s);
statement(s); }
}
------------------------------------------------------------
if (condition) if (condition) {
{ statement(s);
statement(s); }
}
------------------------------------------------------------
if (condition) if (condition) {
{ statement(s);
statement(s); } else if (condition) {
} statement(s);
else if (condition) } else {
{ statement(s);
statement(s); }
}
else
{
statement(s);
}
------------------------------------------------------------
for (loop control expressions) for (loop control expressions) {
{ statement(s);
statement(s); }
}
------------------------------------------------------------
while (condition) while (condition) {
{ statement(s);
statement(s); }
}
------------------------------------------------------------
do do {
{ statement(s)
statement(s); } while (condition);
}
while (condition);
------------------------------------------------------------
switch (integer expression) switch (integer expression) {
{ case constant1:
case constant1: statement(s);
statement(s); case constant2:
case constant2: statement(s);
statement(s); default:
default: statement(s);
statement(s); }
}
The left column is the traditional C++/C-style indentation.
The right column is common to Java-style code.
You instructor prefers the C++/C-style indentation because it makes it
easier to see the scope of statements for each control structure.
|