Defining Programming Standards   
for Professional Programmers 
  

         

Home

Contents

1: Standards

2: Psychological Factors

3: General Principles

4: Commenting

5: Naming

6: Code Layout

7: File Layout

8: Language Usage

9: Data Usage

10: Programming Usage

11: Implementing Standards

A: Example Standard

B: References

C: Glossary

Syque

About

Share this page:

Google
C Style
syque.com
Web

 

 

Books and
more at:

USA:

In association with amazon.com

UK:

In Association with Amazon.co.uk

Canada:

In Association with amazon.ca

 

 

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.9  Using 'continue' and 'break'

The continue and break statements are typically used to escape from loops without having to add additional levels of nesting (break, of course, is also used in switch statements - see 8.7.1).

These statements are unconditional branches, in the same manner as goto, but with the predefined 'label' positioned at the beginning or end of the smallest enclosing for, while or do loop. Thus a similar set of arguments applies for and against their use. In some ways they are less readable than goto, as the places where they jump to do not stand out as much as a label, especially when there are several for, while or do statements in the vicinity or where the exited loop is large. This can be helped by using an appropriate comment immediately after the keyword, linking it to the loop that it is in:

 

for ( WordLen = 0; ... )
{
    while ( CharNo < MaxChars )
    {
        if ( ... )
            break; /* while ( CharNo.. */
    ...
    }
    if ( ... )
        continue; /* for ( WordLen.. */
    ...
}

 

The potential for error in the use and reading of break and continue means that their use should in any case be minimized, and preferably restricted to a limited set of circumstances.

‘break’ is better used as an escape from a loop under abnormal, rather than normal, conditions, in the same manner as return being used to exit mid-function only under error conditions (see 8.10).

‘continue’ can usually be eliminated altogether with judicious use of the if statement.

 

<--Prev page | Next page -->

 

 

  © Syque 1995-2010

Massive Content -- Maximum Speed