.NET HELP C# For Loop (How it Works for Developers) Jacob Mellor 更新:2025年6月20日 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 在本教程中,我們將全面介紹在public static void Main方法中使用 for 迴圈所需的一切。 我們將探討 for 迴圈、迴圈變數、迴圈體、迭代變數、內迴圈和外迴圈、無限迴圈、布林運算式、巢狀迴圈等等。 我們開始吧! for迴圈入門 for循環是 C# 中的循環,專門用於你知道要迭代多少次的情況。 C# 中for迴圈的語法如下程式碼區塊所示: for (initialization; condition; increment) { // Loop body } for (initialization; condition; increment) { // Loop body } $vbLabelText $csharpLabel 讓我們來分解一下for迴圈的組成部分: 1.初始化:這是宣告和初始化循環變數或迭代變數的地方。 2.條件:一個布林/條件表達式,用於確定循環是否應該繼續多次執行語句。 3.遞增:此語句在每次迭代後更新迭代變數。 靜態空主變數和迴圈變數 在 C# 中, static void Main方法或static void Main(String []args)是應用程式的入口點。 這是程式開始執行的地方。 以下是在static void Main中使用 for 迴圈的範例: using System; class Program { static void Main() { for (int i = 0; i < 5; i++) { Console.WriteLine("This is the first for loop!"); } } } using System; class Program { static void Main() { for (int i = 0; i < 5; i++) { Console.WriteLine("This is the first for loop!"); } } } $vbLabelText $csharpLabel 在這個例子中,循環變數int i被初始化為 0,並作為變數。 只要i小於5迴圈就會繼續執行。 每次迭代後,遞增操作i++將i的值增加 1。 探索嵌套循環 嵌套循環是指將循環放置在其他循環內部,形成一個內循環和一個帶有迭代器部分的外循環。 在處理矩陣等多維資料結構,或需要對元素的每個組合執行特定操作時,這些方法非常有用。 以下是一個 C# 中巢狀 for 迴圈的範例,其中內層迴圈位於外層循環之內: for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { Console.WriteLine($"i: {i}, j: {j}"); } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { Console.WriteLine($"i: {i}, j: {j}"); } } $vbLabelText $csharpLabel 在這個例子中,外層迴圈執行,從i等於 0 開始。然後,內層循環遍歷j的所有可能值,然後再處理i的下一個值。 無限循環和循環控制 無限循環是指永遠不會結束的循環,因為它的測試條件永遠不會變成假。 這些操作可能很危險,因為它們會導致程式無限期地掛起。 編寫 while 迴圈或foreach迴圈等迴圈時要謹慎,確保最終能夠滿足退出條件。 以下是 C# 中沒有指定條件的無限迴圈範例。 // This is an example of an infinite loop for (int i = 0; ; i++) { Console.WriteLine("This loop will run forever!"); } // This is an example of an infinite loop for (int i = 0; ; i++) { Console.WriteLine("This loop will run forever!"); } $vbLabelText $csharpLabel 除了標準的 for 迴圈結構外,C# 還提供了迴圈控制語句,例如break和continue ,可以幫助您更有效地管理迴圈。 break :此語句用於立即退出迴圈。遇到break語句時,循環終止,程式繼續執行循環外的下一行程式碼。 continue :此語句用於跳過目前迭代循環體中的剩餘程式碼,並跳到循環的下一個迭代。 下面這個範例示範如何在 for 迴圈中使用break和continue : for (int i = 0; i < 10; i++) { if (i == 5) { break; // Exits the loop when i is equal to 5 } if (i % 2 == 0) { continue; // Skips even numbers } Console.WriteLine($"Odd number: {i}"); } for (int i = 0; i < 10; i++) { if (i == 5) { break; // Exits the loop when i is equal to 5 } if (i % 2 == 0) { continue; // Skips even numbers } Console.WriteLine($"Odd number: {i}"); } $vbLabelText $csharpLabel 在這個例子中,當i達到5 ,循環停止執行。 continue語句用來跳過偶數,因此只會印出小於5奇數。 布林表達式和循環條件 循環條件是一個布林表達式,用於確定循環是否應該繼續執行。 該表達式在每次迭代之前都會進行求值,並且只有當該表達式為true循環才會運行。 許多循環中常用的布林表達式包括: 比較: i < 10 , i >= 10 , i > 10 , i == 10 , i != 10 邏輯運算子: && (與), *||(OR), ** (不是) 您可以使用邏輯運算子組合多個表達式,以建立更複雜的循環條件。 例如: for (int i = 0; i < 10 && i != 5; i++) { Console.WriteLine(i); } for (int i = 0; i < 10 && i != 5; i++) { Console.WriteLine(i); } $vbLabelText $csharpLabel 在這個例子中,只要i小於10且不等於5 ,迴圈就會執行。 程式碼區塊和循環體 程式碼區塊是由花括號{}括起來的一組語句。 在 for 迴圈中,緊接在循環宣告之後的程式碼區塊稱為迴圈體。 在這裡,你可以放置要在循環每次迭代中執行的程式碼。 for (int i = 0; i < 5; i++) { // This is the loop body Console.WriteLine($"Iteration: {i}"); } for (int i = 0; i < 5; i++) { // This is the loop body Console.WriteLine($"Iteration: {i}"); } $vbLabelText $csharpLabel 在這個例子中,循環體由一個Console.WriteLine語句組成,該語句會列印目前迭代次數。 循環執行步驟 當你的程式碼中遇到 for 迴圈時,會發生以下一系列事件: 循環變數已初始化。 對布林表達式進行求值。 如果表達式為false ,則跳過循環,程式繼續執行循環外的下一行程式碼。 如果表達式為true ,則執行循環體。 循環變數遞增或更新。 重複步驟 2-4,直到布林表達式變成false 。 將 IronPDF 整合到 For 循環中以產生報告 了解 IronPDF 的 PDF 產生功能,使用 C# 建立動態且強大的 PDF 報告。 在使用 for 迴圈時,它可以成為一個有用的工具,尤其是在需要根據循環中處理的資料建立動態報告或文件時。 在本節中,我們將向您展示如何將 IronPDF 與 C# for 迴圈結合使用來產生簡單的報告。 首先,您需要安裝 IronPDF NuGet 套件。 您可以使用 Visual Studio 中的套件管理器控制台來完成此操作: Install-Package IronPdf 安裝好 IronPDF 之後,讓我們建立一個簡單的範例,使用 IronPDF 從 HTML 產生 PDF 報告,其中包含一個數字及其平方的表格,並使用 for 迴圈來實現。 步驟 1:新增必要的命名空間。 using IronPdf; using System.IO; using IronPdf; using System.IO; $vbLabelText $csharpLabel 步驟 2:建立一個名為GenerateReport新方法。 static void GenerateReport() { // Create an HTML template for the report var htmlTemplate = @" <html> <head> <style> table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid black; padding: 8px; text-align: center; } </style> </head> <body> <h1>Number Squares Report</h1> <table> <thead> <tr> <th>Number</th> <th>Square</th> </tr> </thead> <tbody> {0} </tbody> </table> </body> </html>"; // Generate the table rows using a for loop string tableRows = ""; for (int i = 1; i <= 10; i++) { tableRows += $"<tr><td>{i}</td><td>{i * i}</td></tr>"; } // Insert the generated table rows into the HTML template string finalHtml = string.Format(htmlTemplate, tableRows); // Create a new PDF document from the HTML using IronPdf var pdf = new IronPdf.ChromePdfRenderer(); var document = pdf.RenderHtmlAsPdf(finalHtml); // Save the PDF to a file document.SaveAs("NumberSquaresReport.pdf"); } static void GenerateReport() { // Create an HTML template for the report var htmlTemplate = @" <html> <head> <style> table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid black; padding: 8px; text-align: center; } </style> </head> <body> <h1>Number Squares Report</h1> <table> <thead> <tr> <th>Number</th> <th>Square</th> </tr> </thead> <tbody> {0} </tbody> </table> </body> </html>"; // Generate the table rows using a for loop string tableRows = ""; for (int i = 1; i <= 10; i++) { tableRows += $"<tr><td>{i}</td><td>{i * i}</td></tr>"; } // Insert the generated table rows into the HTML template string finalHtml = string.Format(htmlTemplate, tableRows); // Create a new PDF document from the HTML using IronPdf var pdf = new IronPdf.ChromePdfRenderer(); var document = pdf.RenderHtmlAsPdf(finalHtml); // Save the PDF to a file document.SaveAs("NumberSquaresReport.pdf"); } $vbLabelText $csharpLabel 從Program.cs檔呼叫GenerateReport方法: GenerateReport(); GenerateReport(); $vbLabelText $csharpLabel IronPDF 的數字平方報告 執行此範例時,將在應用程式的輸出目錄中產生名為"NumberSquaresReport.pdf"的 PDF 報告。 該報告將包含一個表格,表格中包含 1 到 10 之間的數字及其平方,這些數字是使用 C# for 迴圈產生的。 結論 總之,這篇全面的教學為你打下了C# for迴圈及其相關概念的堅實基礎。我們探討了循環變數、循環體、迭代變數、內循環和外循環、無限循環、布林表達式、程式碼區塊、嵌套循環,甚至還示範如何整合強大的IronPDF庫,使用for循環產生動態PDF報告。 IronPDF 提供免費試用版,供您測試其功能。如果您覺得有用,我們提供價格實惠的授權方案,以滿足您的需求。 常見問題解答 for 環路在 C# 中如何運作? C# 中的 for 環路是用來重複執行一個程式碼區塊指定的次數。它由三個主要部分組成:初始化、條件和增量,這三個部分控制循環的執行。 C# 中「static void Main」方法的作用是什麼? static void Main "方法是 C# 應用程式的入口點。它是程式開始執行的地方,通常包含初始程式碼,例如執行各種任務的 for Loops。 如何使用 for 循环在 C# 中生成 PDF 报告? 您可以使用 IronPDF 之類的函式庫在 C# 中產生 PDF 報表。可以利用 For 環路來處理資料,並將其格式化為表格或報表,然後再使用 IronPdf 將其呈現為 PDF 文件。 什麼是嵌套循環,在 C# 中如何運作? C# 中的嵌套循環是放置在其他循環內的循環。它們對於處理多維資料結構特別有用,因為它們允許您對元素的組合執行操作。 如何防止 C# 中的無限循環? 為了防止無限循環,請確保您的循環條件最終會為 false。利用迴圈控制語句,例如「break」,在滿足特定條件時退出迴圈。 在 C# 迴圈中,「break」和「continue」語句用來做什麼? 在 C# 中,"break 「語句用於立即退出循環,而 」continue "語句跳過當前的迭代,繼續循環的下一次迭代。 布林表達式在 for 環路中如何運作? for 環路中的布林表達式決定環路是否應該繼續執行。它們會在每次迭代之前進行評估,並且必須回傳 true 才能繼續執行循環。 如何安裝 C# 函式庫以配合 for 環路使用? 您可以透過 Visual Studio 中的套件管理員控制台,使用適當的安裝指令來安裝 C# 函式庫,讓您可以在 for 環路內利用其功能。 Jacob Mellor 立即與工程團隊聊天 首席技術長 Jacob Mellor 是 Iron Software 的首席技術長,也是開創 C# PDF 技術的有遠見的工程師。作為 Iron Software 核心程式碼庫背後的原始開發人員,他從公司成立之初就塑造了公司的產品架構,與首席執行官 Cameron Rimington 一起將公司轉型為一家 50 多人的公司,為 NASA、Tesla 和全球政府機構提供服務。Jacob 持有曼徹斯特大學土木工程一級榮譽工程學士學位 (BEng)(1998-2001 年)。Jacob 於 1999 年在倫敦開設了他的第一家軟體公司,並於 2005 年創建了他的第一個 .NET 元件,之後,他專門解決微軟生態系統中的複雜問題。他的旗艦產品 IronPDF & Iron Suite for .NET 函式庫在全球的 NuGet 安裝量已超過 3000 萬次,他的基礎程式碼持續為全球使用的開發人員工具提供動力。Jacob 擁有 25 年的商業經驗和 41 年的編碼專業知識,他一直專注於推動企業級 C#、Java 和 Python PDF 技術的創新,同時指導下一代的技術領導者。 相關文章 更新2025年12月11日 Bridging CLI Simplicity & .NET : Using Curl DotNet with IronPDF Jacob Mellor has bridged this gap with CurlDotNet, a library created to bring the familiarity of cURL to the .NET ecosystem. 閱讀更多 更新2025年12月20日 RandomNumberGenerator C# Using the RandomNumberGenerator C# class can help take your PDF generation and editing projects to the next level 閱讀更多 更新2025年12月20日 C# String Equals (How it Works for Developers) When combined with a powerful PDF library like IronPDF, switch pattern matching allows you to build smarter, cleaner logic for document processing 閱讀更多 C# Wait For Seconds (How it Works for Developers)C# String Replace (How it Works For...
更新2025年12月11日 Bridging CLI Simplicity & .NET : Using Curl DotNet with IronPDF Jacob Mellor has bridged this gap with CurlDotNet, a library created to bring the familiarity of cURL to the .NET ecosystem. 閱讀更多
更新2025年12月20日 RandomNumberGenerator C# Using the RandomNumberGenerator C# class can help take your PDF generation and editing projects to the next level 閱讀更多
更新2025年12月20日 C# String Equals (How it Works for Developers) When combined with a powerful PDF library like IronPDF, switch pattern matching allows you to build smarter, cleaner logic for document processing 閱讀更多