天天看點

C#if/if-else/if-else-if語句

通常語句在程式中都是按照先後順序執行,這樣的程式流程是線性的。如果要改變程式的流程,可以在程式中使用控制語句來有條件地選擇執行語句或重複執行某個語句塊。

程式控制語句用于改變程式執行的順序。

控制語句的基本文法:

控制語句描述{

語句(參數)

//程式塊

}

如果程式塊中隻有一條語句,則花括号{}可略去,但還是建議使用{},這樣代碼更易閱讀,可避免在修改代碼時發生錯誤。

(1)if結構的基本文法

if(條件)

  { 語句1;} 

執行過程:首先判斷條件的結果,如果條件為true,則執行語句1,如果條件為false,則跳過語句1,執行後面的語句

注意:1.if後面括号中的條件,要能計算成一個bool類型的值

           2.預設情況下,if 語句隻能帶1句話,即和if語句有關系的語句,隻有語句1.

在if語句中,如果想讓if帶多句話,則可以用{}把想讓if帶的多句話括起來組成語句塊

if(條件)

   {

  語句1;

  語句2;

。。。。。

  語句n;

}

執行過程:如果條件為false,那麼将跳過if 所帶的語句塊中的所有語句

要求:我們在寫程式時,哪怕if語句隻帶一句話,也要把他寫在大括号中。

(2)If else 結構文法

文法

If(條件)

{語句塊1;}

Else

{語句塊2;}

執行過程:

  如果條件為true,則執行if帶的語句塊1,并且跳過else帶的語句塊2

  如果條件為false,則跳過if帶的語句塊1,執行else帶的語句塊2

上面兩個語句塊,根據條件結束為true或false,總要執行一個

If –else-if 文法:

文法

If(條件1)

{

語句塊1;

}

Else if (條件2)

{

語句塊2;

}

Else if (條件3)

{

語句塊3;

}

。。。。

Else if (條件n)

{

語句塊n;

}

Else

{

}

執行過程:

在if else if語句中,隻用當上一個條件不成立時才會進入下一個if語句,并進行if語句後面的條件判斷。一旦有一個if後面的條件為true,則執行此if所帶的語句(塊),語句(塊)執行完成後,程式跳出if-else-if結構,如果所有的if條件都不成立,則,如果最後有else,則執行else所帶的語句,否則什麼都不執行。

注意:Else永遠和離他最近的if組成一對

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace if語句
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 9, b = 5, c = 7, t;
            if (a > b)
            {
                t = a; a = b; b = t;
            }
            if (a > c)
            {
                t = a; a = c; c = t;
            }
            if (b > c)
            {
                t = b; b = c; c = t;
            }
            Console.WriteLine("a="+a+",b="+b+",c="+c);
            Console.ReadKey();
        }
    }
}
           
C#if/if-else/if-else-if語句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace if_else_if語句
{
    class Program
    {
        static void Main(string[] args)
        {
            int month = 4;//4月份
            String season;
            if (month == 12 || month == 1 || month == 2)
            {
                season = "冬天";
            }
            else if (month == 3 || month == 4 || month == 5)
            {
                season = "春天";
            }
            else if (month == 6 || month == 7 || month == 8)
            {
                season = "夏天";
            }
            else if (month == 9 || month == 10 || month == 11)
            {
                season = "秋天";
            }
            else 
            {
                season = "不合法的月份";
            }
            Console.WriteLine("4月份是"+season+".");
            Console.ReadKey();
        }
    }
}
           
C#if/if-else/if-else-if語句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace if_else語句
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請輸入一個數?");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("請輸入另一個數?");
            int b = Convert.ToInt32(Console.ReadLine());
            if (a % b == 0 || a + b > 100)
            {
                Console.WriteLine(a);
            }
            else
            {
                Console.WriteLine(b);
            }
            Console.ReadKey();
        }
    }
}
           
C#if/if-else/if-else-if語句