The Psychology of Quality and More |
CHAPTER 6 : Layout
6.11 'else..if'The switch statement in C is relatively limited in the cases that it can act upon, even with the extension of ANSI C that allows switching on more than just int's. If one of a series of actions must be carried out, where one or more of the actions depends on more than a simple constant value of a variable, then multiple if..else if statements must be used:
if ( Ch >= 'a' && Ch <= 'z') ---------------------------------------- This follows indentation guidelines, but matching if's and else's can be very difficult. Even the use of braces would not help too much. In any case, the code rapidly tramps across towards the right hand margin. Multiple choices are best treated as a special case, where the else if is treated as one keyword, and stays at the same indentation level as the original if. The final else prefaces the default case:
if ( Ch >= 'a' && Ch <= 'z')
How do you order these? Sometimes the order is natural. Sometimes, in a long list and especially if the comparisons are complex, then performance considerations may be important, and comparisons which are met most often will be put earlier in the list.
|
Site Menu |
Quality: | Quality Toolbook | Tools of the Trade | Improvement Encyclopedia | Quality Articles | Being Creative | Being Persuasive | |
And: | C Style (Book) | Stories | Articles | Bookstore | My Photos | About | Contact | |
Settings: | Computer layout | Mobile layout | Small font | Medium font | Large font | Translate | |
You can buy books here |
And the big |