Movatterモバイル変換


[0]ホーム

URL:


Country
Contact Sales

The information on this page is for Archive Purposes Only

This page is not being actively maintained. Links within the documentation may not work and the information itself may no longer be valid. The last revision to this document was made on April 20, 1999

7 - Statements

7.1 Simple Statements

Each line should contain at most one statement. Example:

argv++;         // Correctargc--;         // Correct  argv++; argc--; // AVOID!

7.2 Compound Statements

Compound statements are statements that contain lists of statements enclosed in braces "{ statements }". See the following sections for examples.

  • The enclosed statements should be indented one more level than the compound statement.
  • The opening brace should be at the end of the line that begins the compound statement; the closing brace should begin a line and be indented to the beginning of the compound statement.
  • Braces are used around all statements, even single statements, when they are part of a control structure, such as anif-else orfor statement. This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces.

7.3 return Statements

Areturn statement with a value should not use parentheses unless they make the return value more obvious in some way. Example:

return;return myDisk.size();return (size ? size : defaultSize);

7.4 if, if-else, if else-if else Statements

Theif-else class of statements should have the following form:

if (condition) {statements;}if (condition) {statements;} else {statements;}if (condition) {statements;} else if (condition) {statements;} else {statements;}

Note:if statements always use braces,{}. Avoid the following error-prone form:

if (condition) //AVOID! THIS OMITS THE BRACES {}!statement;

7.5 for Statements

Afor statement should have the following form:

for (initialization; condition; update) {statements;}

An emptyfor statement (one in which all the work is done in the initialization, condition, and update clauses) should have the following form:

for (initialization; condition; update);

When using the comma operator in the initialization or update clause of afor statement, avoid the complexity of using more than three variables. If needed, use separate statements before thefor loop (for the initialization clause) or at the end of the loop (for the update clause).

7.6 while Statements

Awhile statement should have the following form:

while (condition) {statements;}

An emptywhile statement should have the following form:

while (condition);

7.7 do-while Statements

Ado-while statement should have the following form:

do {statements;} while (condition);

7.8 switch Statements

Aswitch statement should have the following form:

switch (condition) {case ABC:statements;/* falls through */case DEF:statements;break;case XYZ:statements;break;default:statements;break;}

Every time a case falls through (doesn't include abreak statement), add a comment where thebreak statement would normally be. This is shown in the preceding code example with the/* falls through */ comment.

Everyswitch statement should include a default case. Thebreak in the default case is redundant, but it prevents a fall-through error if later anothercase is added.

7.9 try-catch Statements

Atry-catch statement should have the following format:

try {statements;} catch (ExceptionClass e) {statements;}

Atry-catch statement may also be followed byfinally, which executes regardless of whether or not thetry block has completed successfully.

try {statements;} catch (ExceptionClass e) {statements;} finally {statements;}

[8]ページ先頭

©2009-2025 Movatter.jp