C# if中連續幾個條件判斷
1.if (條件表達式1 && 條件表達式2)
當條件表達式1為true時
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleApplication1
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 int num = 1;
13 string a = "123";
14 if (a != null && (++num) > 0)
15 {
16 a = "456";
17 }
18 Console.WriteLine(num);
19 Console.ReadKey();
20 }
21 }
22 }
2.if (條件表達式1 && 條件表達式2)
當條件表達式1為false時
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleApplication1
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 int num = 1;
13 string a = "123";
14 if (a == null && (++num) > 0)
15 {
16 a = "456";
17 }
18 Console.WriteLine(num);
19 Console.ReadKey();
20 }
21 }
22 }
總結,在if中只有前面的條件表達式為true,才會執行後面的條件表達式。