If, if-else, if else-if else Statements
The if-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;For Statements A for statement should have the following form: for (initialization; condition; update) { statements;}An empty for 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 a for statement, avoid the complexity of using more than three variables. If needed, use separate statements before the for loop (for the initialization clause) or at the end of the loop (for the update clause). While Statements A while statement should have the following form: while (condition) { statements;}An empty while statement should have the following form: while (condition);Do-while Statements A do-while statement should have the following form: do { statements;} while (condition);Switch Statements A switch 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 a break statement), add a comment where the break statement would normally be. This is shown in the preceding code example with the /* falls through */ comment. Every switch statement should include a default case. The break in the default case is redundant, but it prevents a fall-through error if later another case is added. Try-catch Statements A try-catch statement should have the following format: try { statements;} catch (ExceptionClass e) { statements;}A try-catch statement may also be followed by finally, which executes regardless of whether or not the try block has completed successfully. try { statements;} catch (ExceptionClass e) { statements;} finally { statements;}
White Space Blank Lines Blank lines improve readability by setting off sections of code that are logically related. Two blank lines should always be used in the following circumstances:
One blank line should always be used in the following circumstances:
Blank Spaces Blank spaces should be used in the following circumstances:
Note that a blank space should not be used between a method name and its opening parenthesis. This helps to distinguish keywords from method calls.
Naming Conventions Naming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier-for example, whether it's a constant, package, or class-which can be helpful in understanding the code.
Programming Practices
Популярное: Модели организации как закрытой, открытой, частично открытой системы: Закрытая система имеет жесткие фиксированные границы, ее действия относительно независимы... Генезис конфликтологии как науки в древней Греции: Для уяснения предыстории конфликтологии существенное значение имеет обращение к античной... ©2015-2024 megaobuchalka.ru Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. (822)
|
Почему 1285321 студент выбрали МегаОбучалку... Система поиска информации Мобильная версия сайта Удобная навигация Нет шокирующей рекламы |