Statements

Statements

Simple Statements

Assignment and Expression Statements

Each line should contain at most one statement. For example,

 a = b + c; count++;     // WRONG        a = b + c;              // RIGHT        count++;                // RIGHT 
Local Variable Declarations

Generally, local variable declarations should be on separate lines; however, an exception is allowed for temporary variables that do not require initializers. For example,

 int i, j = 4, k;        // WRONG        int i, k;               // acceptable        int j = 4; 

Compound Statements

Compound statements are statements that contain a statement block enclosed in {} braces. All compound statements follow the same braces style; namely, the style commonly known as the "K & R" braces style. This includes class and method declarations. This style is specified below.

  1. The opening left brace is at the end of the line beginning the compound statement.

  2. The closing right brace is alone on a line, indented to the same column as the beginning of the compound statement.

  3. The statements inside the enclosed braces are indented one more level than the compound statement.

In cases where the language allows it, the braces may be omitted in the following situations:

  1. The statement block consists of the null statement ";".

  2. The statement block consists of a single simple (not compound) statement that fits on a single line.

The rules on how to format particular statements are described below.

if Statement
 if (condition) {            statements;         }        if (condition) {            statements;         } else {            statements;         }         if (condition) {            statements;         } else if (condition) {            statements;         } else {            statements;         } 
for Statement
 for (initialization; condition; update) {            statements;         } 
while Statement
 while (condition) {            statements;         } 
do-while Statement
 do {            statements;         } while (condition); 
switch Statement
 switch (condition) {        case 1:         case 2:             statements;             break;         case 3:             statements;             break;         default:             statements;             break;         } 
try Statement
 try {            statements;         } catch (exception-declaration) {            statements;         } finally {            statements;        } 
synchronized Statement
 synchronized (expression) {            statements;        } 

Labeled Statements

Labeled statements should always be enclosed in braces {}. The label itself should be indented to the normal indentation level, followed by a colon , single space, and opening brace. The closing brace should have a trailing comment on the same line with the label repeated:

 statement-label: {        } // statement-label 


Software Development. Building Reliable Systems
Software Development: Building Reliable Systems
ISBN: 0130812463
EAN: 2147483647
Year: 1998
Pages: 193
Authors: Marc Hamilton

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