2012年10月13日 星期六

邏輯判斷

如果你未來要進入程式設計這個領域,這個主題無疑是你以後會經常接觸的,讓我們開始吧,首先,先建立一個【主控台應用程式】專案。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace If_and_Else
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

我們先宣告一個整數變數【index】和一個字串變數【result】,並給他們初始值。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace If_and_Else
{
    class Program
    {
        static void Main(string[] args)
        {
            int index = 0;
            string result = "";
        }
    }
}

接著要開始寫下第一個判斷式了,讓我們來看看要怎麼做。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace If_and_Else
{
    class Program
    {
        static void Main(string[] args)
        {
            int index = 0;
            string result = "";

            if (index == 0)
            {
                result = "這是0";
            }
            else
            {
                result = "這不是0";
            }

            Console.Write(result + "\r\n");
            Console.ReadKey();
        }
    }
}

上我說明一下邏輯判斷的用法,【if...else】就是用來作邏輯判斷,【if】後面接的是【要判斷的敘述】,通常會稱之為【條件式】,如果【條件式】【成立】,那麼就會執行if下面大括弧裡面的程式碼內容,如果【條件式】【不成立】,那麼就會執行else下面大括弧裡面的程式碼內容。

有看沒有懂?以上面程式碼為例,【index == 0】就是【條件式】,意思是要判斷整數變數【index】的內容是不是整數【0】,如果【是】整數【0】,則字串變數【result】的內容為【這是0】,如果【不是】整數【0】,則字串變數【result】的內容為【這不是0】,你可以試著改變整數【index】的值,並且【重新編譯】程式碼,並執行【開始偵錯】,看看結果有什麼變化。

接著要討論的是很多情況不是只有二分法,怎麼辦?因為其實【if..else】用法是允許【if】敘述單獨出現,【else】不一定要有,所以你可以用很多單獨的【if】來判斷,來看下面的例子。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace If_and_Else
{
    class Program
    {
        static void Main(string[] args)
        {
            int index = 1;
            string result = "";

            if (index == 1)
            {
                result = "星期一";
            }
            if (index == 2)
            {
                result = "星期二";
            }
            if (index == 3)
            {
                result = "星期三";
            }
            if (index == 4)
            {
                result = "星期四";
            }
            if (index == 5)
            {
                result = "星期五";
            }
            if (index == 6)
            {
                result = "星期六";
            }
            if (index == 7)
            {
                result = "星期日";
            }
         
            Console.Write(result + "\r\n");
            Console.ReadKey();
        }
    }
}

除此之外,還可以使用【if..else if..else】的語法來處理,【if..else if..else】怎麼用?

檢查【if】的【條件式】是否成立,
如果【if】的【條件式】【成立】,那麼就執行【if】大括弧裡面的程式碼,
如果【if】的【條件式】【不成立】,那就繼續檢查【else if】的【條件式】,
如果【else if】的【條件式】【成立】,那麼就執行【else if】大括弧裡面的程式碼,
如果【else if】的【條件式】【不成立】,那就繼續檢查下一個【else if】的【條件式】,
都沒有任何【條件式】【成立】,則執行【else】大括弧裡面的程式碼,
頭昏了嗎,讓我們看下面的例子。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace If_and_Else
{
    class Program
    {
        static void Main(string[] args)
        {
            int index = 1;
            string result = "";

            if (index == 1)
            {
                result = "星期一";
            }
            else if (index == 2)
            {
                result = "星期二";
            }
            else if (index == 3)
            {
                result = "星期三";
            }
            else if (index == 4)
            {
                result = "星期四";
            }
            else if (index == 5)
            {
                result = "星期五";
            }
            else if (index == 6)
            {
                result = "星期六";
            }
            else if (index == 7)
            {
                result = "星期日";
            }
            else
            {
                result = "一星期的日子裡面沒有這天";
            }

            Console.Write(result + "\r\n");
            Console.ReadKey();
        }
    }
}

看了程式碼,是不是比較清楚【if..else if..else】該怎麼使用呢?這時候也許你會問,這寫法跟用很多個單獨的【if】差別在哪裡?

讓我告訴你,差別在於【判斷的次數】,不明白?讓我說清楚一點,以上面的例子來看,用很多個單獨的【if】這種寫法,不管你的整數【index】內容是哪個數字,程式【總是】判斷7次,而使用【if..else if..else】的寫法,程式則是【最多】判斷7次。

讓我舉個例子,假設整數【index】內容是【1】,用很多個單獨的【if】這種寫法,會先判斷是不是1,【判斷式】成立,執行大括弧裡面的程式碼,接著繼續判斷是不是2......一直到判斷是不是7,然後結束。

而使用【if..else if..else】的寫法,開始依然會先判斷是不是1,【判斷式】成立,執行大括弧裡面的程式碼,然後就結束了。

當然,你還有另外一種選擇,那就是【switch..case】,怎麼用?【switch】後面放置【要判斷的變數】,【case】後面放置【判斷的條件】,在講蝦米鬼東西,看程式碼比較清楚。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace If_and_Else
{
    class Program
    {
        static void Main(string[] args)
        {
            int index = 1;
            string result = "";

            switch (index)
            {
                case 1:
                    {
                        result = "星期一";
                    }
                    break;
                case 2:
                    {
                        result = "星期二";
                    }
                    break;
                case 3:
                    {
                        result = "星期三";
                    }
                    break;
                case 4:
                    {
                        result = "星期四";
                    }
                    break;
                case 5:
                    {
                        result = "星期五";
                    }
                    break;
                case 6:
                    {
                        result = "星期六";
                    }
                    break;
                case 7:
                    {
                        result = "星期日";
                    }
                    break;
                default:
                    {
                        result = "一星期的日子裡面沒有這天";
                    }
                    break;
            }
          
            Console.Write(result + "\r\n");
            Console.ReadKey();
        }
    }
}

基本上這個寫法與【if..else if..else】的效果相同,什麼情況用哪個?我的經驗是,條件少的用【if..else if..else】,條件多的用【switch..case】,小弟一點拙見,今天就到這,明天見。

沒有留言:

張貼留言