Chapter 15 using conditionals
Conditional statements
15.1 if statements if statement
· Plain if-then statements simple if-then statement
1. Write normally firstCodeChannels; then handle uncommon situations
2. Make sure that the same number of branches is correct.
3. Put the normal situation behind the if, instead of the else. Try to put all the normal and all the errors together.
4. Let the if clause follow a meaningful statement.
For example, sometimes you will see
If (sometest)
;
Else {
// Do something;
}
To:
If (! Sometest)
{
// Do something;
}
5. Consider the else clause
If you think you only need a simple if statement, consider whether you really do not need an if-then-else statement.
According to a classic analysis conducted by General Motors, an if Statement of 1976 to should have an else clause (elshoff ).
One option is to use an empty statement if needed to indicate that this else situation has been taken into consideration. Unless else is null, the cause is obvious.
Please use annotations to explain why the else clause is unnecessary here, just as below:
If (color_min <= color & Color <= color_max ){
......
}
Else {
// Else color is invalid.
}
6. Test the correctness of the else clause.
7. Check whether the IF and else clauses are reversed.
· Chains of if-then-else statements
If-then-else statement string
If-then-else statement strings are used in languages that do not support or partially support case. Pay attention to the following guiding principles:
1. simplify complex detection using Boolean function calls
2. Put the most common situations at the beginning.
3. Make sure that all situations have been taken into consideration.
4. If your language supports this function, replace the if-then-else statement string with other structures, such as case or switch.
15.2 case statements case statement
· Choosing the most valid tive ordering of cases
Select the most effective sorting order for case
1. Arrange various situations in alphabetical or numerical order
2. Put the normal situation above
3. Sort case clauses by execution frequency
· Tips for using case statements
Tips for using case statements
1. simplify operations for each scenario
2. Do not create a variable to use the case statement.
Instead of creating a phony variable that is not applicable to the case, use an if-then-else-If detection string to check the variable.
.
3. Use the default clause only to check the true default conditions.
Sometimes you have only one of the following cases to handle, so you have to write the default clause in this case. Although this is sometimes attractive, it is not
Wise.
4. Use the default clause to detect errors
If the default clause in a case statement is neither used for other processing nor can it occur in the normal execution order, add
Diagnose messages.
Check carefully to make sure that each value that may enter the case statement is valid. If you find some invalid values, rewrite these statements
The default clause performs error detection.
5. In C ++ and Java, avoid code execution exceeding the end of a case clause.
That is, each case must have a break statement.
6. In C ++, the end of case clearly indicatesProgramProcess.
If you intentionally let the Code go beyond the end of a case clause, a clear comment will be given at the corresponding position.
In fact, this code should be avoided.
Key Points
· For simple if-else statements, pay attention to the order of the IF clause and else clause, especially when dealing with a large number of errors. Make sure that the normal situation is clear.
· For if-then-else statement strings and case statements, select a sort that is most conducive to reading.
· To capture errors, you can use the default clause in the case statement or the last else clause in the if-then-else statement string.
· Various control structures are not born equal. Select the most appropriate control structure for each part of the code.