Nested if Statements

An if-else statement lets you choose between just two different sets of actions. But sometimes, life presents us with three or more alternatives. Fortunately, the definition of an if-else statement allows you to nest if statements inside each other and thereby let a program choose between as many alternative actions as your mind can comprehend.

An if statement, as the name implies, is a statement in itself. Consequently, it can be positioned inside another if statement in the position where a statement is expected or as one of the statements in a compound statement. This is illustrated in Figure 8.5, which is based on the syntax of the if-else statement from Syntax Box 8.3.

Figure 8.5. Creating nested if statements.

Let's ...

Get C# Primer Plus now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.