<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # C# `for`循環 > 原文: [https://www.programiz.com/csharp-programming/for-loop](https://www.programiz.com/csharp-programming/for-loop) #### 在本文中,我們將學習 C# 中的`for`循環以及在程序中使用它們的不同方法。 在編程中,通常需要執行特定次數的語句塊。 一種可能的解決方案是鍵入所需次數的語句。 但是,重復的次數可能事先未知(在編譯時),或者可能不夠大(例如 10000)。 此類問題的最佳解決方案是循環。 循環在編程中用于重復執行某個語句塊,直到滿足某些條件為止。 在本文中,我們將介紹 C# 中的`for`循環。 * * * ## C# `for`循環 關鍵字`for`**用于在 C# 中創建`for`循環**。`for`循環的語法為: ```cs for (initialization; condition; iterator) { // body of for loop } ``` * * * ## `for`循環如何工作? 1. C# `for`循環具有三個語句:`initialization`,`condition`和`iterator`。 2. 首先執行`initialization`語句,并且僅執行一次。 在這里,變量通常被聲明和初始化。 3. 然后,求值`condition`。`condition`是布爾表達式,即它返回`true`或`false`。 4. 如果將`condition`求值為`true`: 1. `for`循環內的語句將執行。 2. 然后,執行`iterator`語句,該語句通常會更改初始化變量的值。 3. 再次求值`condition`。 4. 該過程一直持續到`condition`被求值為`false`為止。 5. 如果將`condition`求值為`false`,則`for`循環終止。 * * * ## `for`循環流程圖 ![C# for loop flowchart](https://img.kancloud.cn/5f/f4/5ff44436560dbfbd71e799202c73a5bd_467x511.png "C# for loop flowchart") * * * ### 示例 1:C# `for`循環 ```cs using System; namespace Loop { class ForLoop { public static void Main(string[] args) { for (int i=1; i<=5; i++) { Console.WriteLine("C# For Loop: Iteration {0}", i); } } } } ``` 當我們運行程序時,輸出將是: ```cs C# For Loop: Iteration 1 C# For Loop: Iteration 2 C# For Loop: Iteration 3 C# For Loop: Iteration 4 C# For Loop: Iteration 5 ``` 在這個程序中 * `initialization`語句為`int i=1` * `condition`語句為`i<=5` * `iterator`語句為`i++` 該程序運行時 * 首先,聲明變量`i`并將其初始化為 1。 * 然后,求值條件(`i<=5`)。 * 由于條件返回`true`,因此程序將執行`for`循環的主體。 它使用迭代 1 打印給定的行(迭代只是表示重復)。 * 現在,求值迭代器(`i++`)。 這會將`i`的值增加到 2。 * 再次求值條件(`i<=5`),最后將`i`的值加 1。該條件將在前 5 次求值為`true`。 * 當`i`的值為 6 并且條件為`false`時,循環將終止。 ### 示例 2:計算前 n 個自然數之和的`for`循環 ```cs using System; namespace Loop { class ForLoop { public static void Main(string[] args) { int n = 5,sum = 0; for (int i=1; i<=n; i++) { // sum = sum + i; sum += i; } Console.WriteLine("Sum of first {0} natural numbers = {1}", n, sum); } } } ``` 當我們運行程序時,輸出將是: ```cs Sum of first 5 natural numbers = 15 ``` 在此,將`sum`和`n`的值分別初始化為 0 和 5。 迭代變量`i`初始化為 1,并在每次迭代時遞增。 在`for`循環內,`sum`的值增加`i`即`sum = sum + i`的值。`for`循環繼續進行,直到`i`小于或等于`n`(用戶輸入)。 讓我們看看給定程序在每次迭代中會發生什么。 最初,`i = 1`,`sum = 0`,`n = 3` `for`循環執行步驟 | 迭代 | `i`的值 | `i <= 5` | `sum`的值 | | --- | --- | --- | --- | | 1 | 1 | `true` | `0+1 = 1` | | 2 | 2 | `true` | `1+2 = 3` | | 3 | 3 | `true` | `3+3 = 6` | | 4 | 4 | `true` | `6+4 = 10` | | 5 | 5 | `true` | `10+5 = 15` | | 6 | 6 | `false` | 循環終止 | 因此,當`n = 5`時,總和的最終值為 15。 * * * ## `for`循環內的多個表達式 我們還可以在`for`循環中使用多個表達式。 這意味著我們在`for`循環中可以有多個初始化和/或迭代器語句。 讓我們看下面的例子。 ### 示例 3:具有多個初始化和迭代器表達式的`for`循環 ```cs using System; namespace Loop { class ForLoop { public static void Main(string[] args) { for (int i=0, j=0; i+j<=5; i++, j++) { Console.WriteLine("i = {0} and j = {1}", i,j); } } } } ``` 當我們運行程序時,輸出將是: ```cs i = 0 and j = 0 i = 1 and j = 1 i = 2 and j = 2 ``` 在此程序中,我們已聲明并初始化了兩個變量:初始化語句中的`i`和`j`。 同樣,我們在迭代器部分有兩個表達式。 這意味著`i`和`j`在每次迭代中均增加 1。 * * * ## 沒有初始化和迭代器語句的`for`循環 初始化,條件和迭代器語句在`for`循環中是可選的。 這意味著我們也可以在沒有這些語句的情況下運行`for`循環。 在這種情況下,`for`循環充當[`while`循環](/csharp-programming/do-while-loop "while loop in c#")。 讓我們看下面的例子。 ### 示例 4:不帶初始化和迭代器語句的`for`循環 ```cs using System; namespace Loop { class ForLoop { public static void Main(string[] args) { int i = 1; for ( ; i<=5; ) { Console.WriteLine("C# For Loop: Iteration {0}", i); i++; } } } } ``` 當我們運行程序時,輸出將是: ```cs C# For Loop: Iteration 1 C# For Loop: Iteration 2 C# For Loop: Iteration 3 C# For Loop: Iteration 4 C# For Loop: Iteration 5 ``` 在此示例中,我們沒有使用初始化和迭代器語句。 變量`i`在`for`循環上方初始化,并且其值在循環體內遞增。 該程序與示例 1 中的程序相同。 同樣,條件也是可選語句。 但是,如果我們不使用測試表達式,則`for`循環將不會測試任何條件,并且將永遠運行(無限循環)。 * * * ## 無限循環 如果`for`循環中的條件始終為`true`,則`for`循環將永遠運行。 這稱為無限循環。 ### 示例 5:無限循環 ```cs using System; namespace Loop { class ForLoop { public static void Main(string[] args) { for (int i=1 ; i>0; i++) { Console.WriteLine("C# For Loop: Iteration {0}", i); } } } } ``` 在此,`i`初始化為 1,條件為`i>0`。 在每次迭代中,我們將`i`的值增加 1,因此條件永遠不會是`false`。 這將導致循環無限執行。 我們也可以通過將條件替換為空白來創建無限循環。 例如, ```cs for ( ; ; ) { // body of for loop } ``` 要么 ```cs for (initialization ; ; iterator) { // body of for loop } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看