Nested Control Constructs

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

Nesting levels

You can nest control constructs 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 construct. One practical metric is if properly indented code goes off the screen, you are likely nesting it too deeply.

Nesting different kinds of control constructs


Overlapping control constructs

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

See also