Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CQG / Задание1 / HDS_VCPP_Programming Rules.doc
327.68 Кб

Flow Control Structures

Rule 53.The code following acaselabel must always be terminated by abreakstatement or another exiting statement.

Exception:When severalcaselabels are followed by the same block of code, only onebreakstatement is needed. If the program logic requires a fall-through block, it must be clearly documented.

Rule 54.Aswitchstatement must always contain adefaultbranch. If all reasonable cases have been fixed thedefaultbranch must handle unexpected cases.

Rule 55.Do not write logical statements whose expression is not a Boolean value.

Rec. 62.Never usegoto.

Rec. 63.The choice of loop construct (for,while, ordo-while) should depend on the specific use of the loop.

Rec. 64.Usebreakto exit a loop if it avoids using flags.

If the code that follows a caselabel is not terminated bybreak, the execution continues after the nextcaselabel. This means that poorly tested code can be erroneous and still seem to work. SeeExample 61.

C++ has a very loose and, simultaneously, very free way of determining if an expression is true or false. If an expression is evaluated as 0, it is false; otherwise, it is considered to be true. We do not recommend logical tests such asif (test)wheretestis not a Boolean value. Always use explicit comparison toNULLor another value iftestis not Boolean (seeExample 62). The only reason is readability; many programmers find it difficult to read such code.

A gotostatement breaks the control flow and can lead to messy code. In addition, there are limitations for whengotocan be used. For example, it is not permitted to jump past a statement that initializes a local object having a destructor.

In some cases, gotomay be permitted. Every such usage should be carefully motivated.

Each loop construct has a specific usage. A forloop is used only when the loop variable is increased by a constant amount for each iteration and when the termination of the loop is determined by a constant expression. In other cases,whileordo-whileshould be used. When the terminating condition can be evaluated at the beginning of the loop,whileshould be used;do-whileis used when the terminating condition is best evaluated at the end of the loop.

Example 63illustrates how abreakstatement is used in order to avoid flags.


Rec. 65.Use parentheses to clarify the order of evaluation for operators in expressions.

There are a number of common pitfalls concerning the order of evaluation for operators in an expression. Binary operators in C++ have associativity (either leftwards or rightwards) and precedence. If an operator has leftwards associativity and occurs on both sides of a variable in an expression, then the variable belongs to the same part of the expression as the operator on its left side. See Example 64.

In doubtful cases, parentheses always must be used to clarify the order of evaluation. See Example 65.

Another common mistake is to confuse the assignment operator and the equality operator. Since the assignment operator returns a value, it is entirely permitted to have an assignment statement instead of a comparison expression. This, however, most often leads straight to an error.

C++ allows the overloading of operators, sometimes it can easily become confusing. For example, the operators <<(shift left) and>>(shift right) are often used for input and output. Since they were originally bit operations, it is necessary that they have higher priority than relational operators do. This means that parentheses must be used when outputting the values of logical expressions.