Nested Control Structures

You can place control structures inside of other control structures. For example, you might place a decision structure such as an If...Then...Else inside a loop structure such as a While. Such structures are said to be nested.

Nesting levels

You can nest control structures to as many levels as you want. However, you should try to limit the depth to a level you can comfortably understand.

Probably the greatest aid to make your code clean and understandable is to indent the body of each control structure. One practical metric is if properly indented code goes off the screen, you are likely nesting it too deeply.

Nesting different kinds of control structures


Overlapping control structures

You cannot overlap part of one control structure with another. Each control structure must be nested entirely within the other. Failing to do so results in a compile-time error.

See also