欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#語句先后順序?qū)Τ绦虻慕Y(jié)果有影響嗎

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

下面通過一段代碼給大家解析C#語句的順序不同所執(zhí)行的結(jié)果不一樣。

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 namespace Test
 {
  /// <summary>
  /// 自定義類,封裝加數(shù)和被加數(shù)屬性
  /// </summary>
  class MyClass
  {
   private int x = ;      //定義int型變量,作為加數(shù)
   private int y = ;      //定義int型變量,作為被加數(shù)
   /// <summary>
   /// 加數(shù)
   /// </summary>
   public int X
   {
    get
    {
     return x;
    }
    set
    {
     x = value;
    }
   }
   /// <summary>
   /// 被加數(shù)
   /// </summary>
   public int Y
   {
    get
    {
     return y;
    }
    set
    {
     y = value;
    }
   }
   /// <summary>
   /// 求和
   /// </summary>
   /// <returns>加法運算和</returns>
   public int Add()
   {
    return X + Y;
   }
  }
  class Program
  {
   static void Main(string[] args)
   {
    MyClass myclass = new MyClass(); //實例化MyClass的對象
    myclass.X = ;     //為MyClass類中的屬性賦值
    myclass.Y = ;     //為MyClass類中的屬性賦值
    int kg = myclass.Add();
    Console.WriteLine(kg); //調(diào)用MyClass類中的Add方法求和
    Console.ReadLine();
   }
  }
 }

第60行的語句若是被放到第56行,則結(jié)果輸出是0不是8,所以,在設(shè)計程序時,要注意語句次序,有著清晰的思維邏輯 。

下面還有點時間,接著給大家介紹C#中循環(huán)語句總結(jié)

通過使用循環(huán)語句可以創(chuàng)建循環(huán)。 循環(huán)語句導(dǎo)致嵌入語句根據(jù)循環(huán)終止條件多次執(zhí)行。 除非遇到跳轉(zhuǎn)語句,否則這些語句將按順序執(zhí)行。

C#循環(huán)語句中使用下列關(guān)鍵字:

· do...while

· for

· foreach...in

· while

do...while

    do...while語句執(zhí)行一個語句或語句重復(fù),直到指定的表達(dá)式的計算結(jié)果為false, 循環(huán)的身體必須括在大括號內(nèi){},while條件后面使用分號結(jié)尾

示例

 下面的示例 實現(xiàn)do-while循環(huán)語句的執(zhí)行

public class TestDoWhile 
{
 static void Main () 
 {
  int x = 0;
  do 
  {
    Console.WriteLine(x);
    x++;
  } while (x < 10);
 }
}
/*

    Output:

    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
*/

    do-while循環(huán)在計算條件表達(dá)式之前將執(zhí)行一次,如果 while表達(dá)式計算結(jié)果為 true,則,執(zhí)行將繼續(xù)在第一個語句中循環(huán)。 如果表達(dá)式計算結(jié)果為 false,則會繼續(xù)從 do-while 循環(huán)后的第一個語句執(zhí)行。

     do-while 循環(huán)還可以通過break、goto、return 或 throw 語句退出。

for

for 循環(huán)重復(fù)執(zhí)行一個語句或語句塊,直到指定的表達(dá)式計算為 false 值。 for 循環(huán)對于循環(huán)數(shù)組和順序處理很有用。

示例

在下面的示例中,int i 的值將寫入控制臺,并且 i 在每次通過循環(huán)時都加 1。

class ForTest 
{
 static void Main() 
 {
  for (int i = 1; i <= 10; i++)
  {
   Console.WriteLine(i);
  }
 }
}
/*

Output:

1
2
3
4
5
6
7
8
9
10
*/

for 語句重復(fù)執(zhí)行括起來的語句,如下所述:

· 首先,計算變量 i 的初始值。

· 然后,只要 i 的值小于或等于 10,條件計算結(jié)果就為 true。此時,將執(zhí)行 Console.WriteLine 語句并重新計算 i。

· 當(dāng) i 大于10 時,條件變成 false 并且控制傳遞到循環(huán)外部。

    由于條件表達(dá)式的測試發(fā)生在循環(huán)執(zhí)行之前,因此 for 語句可能執(zhí)行零次或多次??梢酝ㄟ^使用break、goto 、 throw或return語句退出該循環(huán)。

所有表達(dá)式的 for語句是可選的例如對于下面的語句用于寫一個無限循環(huán)。

for (; ; )
{
 // ...
}

foreach...in

    foreach 語句對實現(xiàn) System.Collections.IEnumerable 或 System.Collections.Generic.IEnumerable<T>接口的數(shù)組或?qū)ο蠹现械拿總€元素重復(fù)一組嵌入式語句。 foreach 語句用于循環(huán)訪問集合,以獲取您需要的信息,但不能用于在源集合中添加或移除項,否則可能產(chǎn)生不可預(yù)知的副作用。 如果需要在源集合中添加或移除項,請使用 for循環(huán)。

    嵌入語句為數(shù)組或集合中的每個元素繼續(xù)執(zhí)行。 當(dāng)為集合中的所有元素完成循環(huán)后,控制傳遞給 foreach 塊之后的下一個語句。

    可以在 foreach 塊的任何點使用 break 關(guān)鍵字跳出循環(huán),或使用 continue 關(guān)鍵字進(jìn)入循環(huán)的下一輪循環(huán)。

foreach 循環(huán)還可以通過 break、goto、return 或 throw 語句退出。

示例

 在此示例中,使用 foreach 顯示整數(shù)數(shù)組的內(nèi)容。

 class ForEachTest
 {
  static void Main(string[] args)
  {
   int[] barray = new int[] { 0, 1, 2, 3, 4, 5};
   foreach (int i in barray)
   {
    System.Console.WriteLine(i);
   }
  }
 }
 /*

    Output:

    0
    1
    2
    3
    4
    5
*/

while

   while 語句執(zhí)行一個語句或語句塊,直到指定的表達(dá)式計算為 false。 

 class WhileTest 
 {
  static void Main() 
  {
   int n = 1;
   while (n < 5) 
   {
    Console.WriteLine(n);
    n++;
   }
  }
 }
 /*

        Output:

        1
        2
        3
        4
        */

上一篇:C#語言中的修飾符匯總

欄    目:C#教程

下一篇:C#開發(fā)中的垃圾回收機(jī)制簡析

本文標(biāo)題:C#語句先后順序?qū)Τ绦虻慕Y(jié)果有影響嗎

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6885.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有