syque.com

The Psychology of Quality and More

| Menu | Books | Share | Search | Settings |

C Style: Standards and Guidelines (contents)

CHAPTER 8 : Language Usage

PART 4 : USAGE

CHAPTER 8 : Language Usage
8.1 General principles of language usage
8.2 Using expressions
8.3 Using 'if'
8.4 Using 'while'
8.5 Using 'for'
8.6 Using 'do'
8.7 Using 'switch'
8.8 Using 'goto'
8.9 Using 'continue' and 'break'
8.10 Using 'return'
8.11 Using functions
8.12 Using '#define'
8.13 Conditional compilation
8.14 Other preprocessor commands
8.15 Summary

<--Prev page | Next page -->

 

8.4  Using 'while'

The while statement is a fundamental construct of structured languages. With it, all loops can be controlled without recourse to goto. C, in particular, makes heavy use of it, as other loop controls are thinly disguised variations on while. The proper use of a while loop is where it is required to repeat the loop an indeterminate (possibly zero) number of times.

The readability of a while loop can be impaired if the comparison becomes false early in the loop:

 

while ( IsSafeTemp )
{
    if ( RoomTemp < MinTemp )
        IsSafeTemp = FALSE;

    /* poor style: the rest of the loop will be with comparison false */
    ...
}

--------------------------------------------------------------------------

This can often be improved by putting the comparison-changing statement at the bottom of the block and/or by using else's where they may not be strictly necessary:

 

while ( IsSafeTemp )
{
    /* statements which are always executed */
    ...
    if ( RoomTemp < MinTemp )
        IsSafeTemp = FALSE;
    else
        /* statements which need not be executed when temperature is unsafe */
}

 

Note that comments made above about comparisons (see 8.2.5) are equally applicable to while statement comparisons.

<--Prev page | Next page -->

 

Site Menu

| Home | Top | Settings |

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

More Kindle books:

And the big
paperback book


Look inside

 

Please help and share:

 

| Home | Top | Menu |

© Changing Works 2002-
Massive Content -- Maximum Speed