3、C#基礎整理(語句概述)

來源:互聯網
上載者:User

標籤:style   blog   ar   io   color   sp   for   strong   on   

語句

語句分為四個大類:

* 分支語句:if,if... else,if ...else if... else,switch case
* 迴圈語句:for,while,do while,foreach
* 跳躍陳述式:break,continue
* 異常語句:try catch finally

用法:

1、if的用法

第一種:if(true){語句}else{語句}

第二種:if(true){語句}else if{語句}…n個else if{語句}…else{語句}

第三種:if(true){語句}

*2、switch case的用法(不常用)

int i = 3;switch (i){    case 0:    case 1:        Console.WriteLine("1");        break;//每個case裡面都必須有一個break 或 goto 或 return    case 2:        Console.WriteLine("Case 2");        break;        // 接下來的語句會出現警告--語法錯誤        Console.WriteLine("Unreachable code");    case 7-4://7-4會被預估為3,並且提出錯誤警告        Console.WriteLine("Case 3");        break;    default://必須有此語句,否則不執行switch case        Console.WriteLine("輸入錯誤");}

3、for的用法

for(初始值;執行條件;狀態改變){迴圈體}   死迴圈for(;;){迴圈體}

4、while和do while的用法

while(true){語句}----true為判斷條件,初始條件寫在迴圈體外面,狀態改變寫在迴圈體中

do{語句}while(true)----註:這個迴圈會先執行一遍do裡面的內容,不管while括弧裡的內容是否為真。

5、foreach遍曆(用於數組,在數組裡面再具體講解)

6、break,continue的用法

break: 結束迴圈;

continue: 跳出此次迴圈,繼續下一迴圈,迴圈繼續執行;

*7、try{}catch{} 和try{}catch{} finally{}

try……catch……

while (true){    int j;    try    {        string s = Console.ReadLine();//如果輸入了小數則會報錯        j = int.Parse(s);    }    catch (Exception yi)//Exception是一個類,此句抓住了錯誤的類型並賦給變數    {        Console.WriteLine(yi.Message);//輸出錯誤        continue;//繼續while迴圈    }    Console.WriteLine(j);//如果沒錯就執行此句    break;//跳出迴圈}

try……catch……finally……

int e = 0;try{        string s = Console.ReadLine();        e = int.Parse(s);}catch (Exception yi1)//Exception是一個類{        Console.WriteLine(yi1.Message);}finally{        Console.WriteLine("最終執行的語句:{0}",e);}

 

 

語句部分未完待續…………

 

3、C#基礎整理(語句概述)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.