Skip to main content

5.3) Decisions Within Decisions


Most scientific codes will have many if statements that are almost always nested within one another. Example 12 shows an example of this.

Let us look at Example 12 to see a simple nested if statement example.

We see that if a > b tested on line 6 is true then, as the program executes, it will next go to line 7 and print and then line 8 and reset the value of a. Then on line 9 it reaches a nested if statement. We are already in an if statement and it is starting another (nested!). On line 8 a was set to a value of 5.0 and so on line 9 a is greater than 4.0 and so the program execution goes into line 10 and then line 11 (for the endif) and then jumps to the endif on line 16. In this case it would never go on lines 12 to 15 since it already entered the if statement on line 6.

When you put an if statement inside another if statement it is called nested if statements.