5.5 Sequential and Nested if/else Statements

When we need the results of one if statement’s processing before we can evaluate the next condition, we can write multiple if statements either sequentially or nested within other if statements.

5.5.1 Sequential if/else Statements

Finding the Minimum or Maximum Values

To illustrate sequential if statements, let’s look at the problem of finding the smallest of three numbers.

We can use multiple, sequential if statements. First we find the smaller of the first two numbers, then we find the smaller of that result and the third number. The pseudocode for this application is:

read number1 read number2 read number3 if number1 is less than number2 smallest is number1 else smallest is number2 if number3 ...

Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.