# Nesting if statements

Situations arise when you have to use nested IF structure. Nested structures simply contain other IF structures. You can easily nest IF structures if you already know how to create an IF structure. Here is an example:

`IF condition1 THEN`
`IF condition2 THEN`
`ASP Statement(s) for when condition2 is met`
`ELSE`
`ASP Statement(s) when this condition2 is NOT met`
`END IF`
`END IF`

The following shows a graphical representation of the above nested IF structure example:

In the following example, we print a message based on a student's grade:

`<%`
`dim iGrade`
`iGrade = 84`
`if iGrade < 101 then`
`if iGrade = 100 then`
`response.Write "Super! You have 100%!!!"`
`end if `
`if iGrade < 100 then`
`if iGrade > 95 then`
`response.Write "Excellent work! Your score is between 95-99.!!!"`
`else`
`if iGrade > 90 then`
`response.Write "Great work! Your score is between 90-95.!!!"`
`else`
`response.Write "Your score is below 90!"`
`end if`
`end if`
`end if`
`else`
`response.Write "Terrific! You have over 100%!!" `
`end if `
`%>`

Let's analyze this script in detail. (Remember to substitute 84 for iGrade as you follow this script.) We start by declaring a variable iGrade and we assign the numerical value 84 to this variable. The next statement is our main IF statement that checks if iGrade value is 101 or less. If iGrade is less than 101, then, we check at least one more condition. In words, we are saying process the nested IF statements if iGrade is not over 100. If iGrade is over 100, then, nested IF statements are not executed; rather ELSE clause is executed that would print: "Terrific! You have over 100%!!".

Because iGrade is 84 and as we stated earlier, at least one of the nested IF statements will be executed. (With a quick inspection of this code, you may realize that most of the nested IF statements will be checked. You may also realize that we could have used logical expressions to combine some of these conditions. The script is purposely written this way to conveniently just show how to construct nested IF statements.)

Our first nested IF statement, `if iGrade = 100 then`, checks if iGrade is equal to 100. We know it is not. Consequently, this condition is not satisfied.

The second condition, `if iGrade < 100 then`, checks if iGrade is less 100. Because iGrade is less than 100, this condition is satisfied. This IF structure, as you may have noticed, also contains sub nested IF structures. Our first sub nested IF structure, `if iGrade > 95 then`, tests if iGrade is greater than 95. Because 84 is not greater than 95, the ELSE clause of this IF structure is executed. In the ELSE clause, we have one more IF structure, `if iGrade > 90 then`, testing if iGrade is greater than 90. Because this condition is not satisfied, the statement inside the ELSE clause, `response.Write "Your score is below 90!"`, will be executed.

The following output verifies our analysis:

For your convenience, a graphical version of the above example is also shown in figure 1 below. A closer inspection of this graphic reveals that your answer should be the same as shown above. A "No" in the graphic indicates an IF structure condition is not satisfied; in contrast, a "Yes" indicates the condition is satisfied. In this example, following IF structure conditions are satisfied:

1. `Is iGrade < 101?`
2. `Is iGrade < 100?`

The following IF structure conditions are not satisifed:

1. `Is iGrade = 100?`
2. `Is iGrade > 95?`
3. `Is iGrade > 90?`
###### Figure 1 shows the output of our nested and sub-nested IF statements from our example for testing the value of iGrade

Keep the following points in mind when working with nested IF/ELSE structures. Make sure to properly end your condition with a corresponding END IF statement. Otherwise, you are likely to find your script does not behave the way you intended. Also, an ELSE clause can only be used with an IF structure. You can not use an ELSE clause without an IF structure.