C# Nested If
As we discussed in the previous section, if the condition is true in If-else statement, we'll execute the code after the condition, otherwise we'll execute Else code if Else exists. The code can be one statement or multiple statement. If there is only one statement, the wrapper curly braces can be omitted. Because If-else itself is a statement, what will be happened if a nested If-statement is put in the code? Let's see the examples below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System; namespace TestNestedIf1 { class Program { static void Main(string[] args) { int yourmark = 80; if( yourmark >= 75 ){ if( yourmark < 85 ){ Console.WriteLine("You got B."); } } if( yourmark >= 75 && yourmark < 85 ){ Console.WriteLine("You got B."); } Console.Read(); } } }
Output
You got B. You got B.
- Line 10: This is If statement, if yourmark >= 75, we'll execute Line11-13.
- Line 11-13: This is a Nested If statement. if yourmark < 85, we'll output "You got B." So only both condition yourmark >= 75 and yourmark < 85 are met, then output the string.
- Line 16-18: In this case, we can combine condition Line 10-11 together in one line by using && operator to get the same result. This block code is the same as the code Line 10-14. Apparently, Line 16-18 is simple and clear.
We change a little above code below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System; namespace TestNestedIf2 { class Program { static void Main(string[] args) { int yourmark = 70; if( yourmark >= 75 ) if( yourmark < 85 ){ Console.WriteLine("You got B."); }else{ Console.WriteLine("You did not get B."); } Console.Read(); } } }
Output
You did not get B.
- Line 10-12: if yourmark >= 75 is true and yourmark < 85 is true, we'll output "You got B.".
- Line 13: it is an Else clause. So which If-statement does it belong to? It belongs to the nearest If-statement. So Line 10-15 is equivalent to the code below.
if( yourmark >= 75 ){ if( yourmark < 85 ){ Console.WriteLine("You got B."); }else{ Console.WriteLine("You did not get B."); } }
We can put a nested If statement in the Else clause of the first If statement, and then put the third If statement in the second Else clause and so on.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System; namespace TestNestedIf3 { class Program { static void Main(string[] args) { int yourmark = 58; if( yourmark >= 85 ){ Console.WriteLine("You got A."); }else if( yourmark >= 75 ){ Console.WriteLine("You got B."); }else if( yourmark >= 60 ){ Console.WriteLine("You got C."); }else{ Console.WriteLine("You got D."); } Console.Read(); } } }
Output
You got D.
- Line 11: Start the first If statement.
- Line 13: Start the second If statement in the else clause of the first one. This is one statement so curly braces after Else are omitted.
- Line 15: Start the third If statement in the else clause of the second one. This is one statement so curly braces after Else are omitted.
- Line 17: The Else clause belongs to the nearest If statement - the third one.
- Line 11-19: The block of the code is equivalent to the following.
if( yourmark >= 85 ){ Console.WriteLine("You got A."); }else{ if( yourmark >= 75 ){ Console.WriteLine("You got B."); }else{ if( yourmark >= 60 ){ Console.WriteLine("You got C."); }else{ Console.WriteLine("You got D."); } } }