C#中while循環(huán)和do-while循環(huán)舉例詳解
一般情況下,知道循環(huán)幾次采用for循環(huán),如果不知道循環(huán)要執(zhí)行幾次,那么 while 循環(huán)或 do-while 循環(huán)就是比較好的處理方式。
一、while循環(huán)
while(條件表達式) { 條件表達式成立(true)時要執(zhí)行的語句; }
進入 while 循環(huán)時,必須先檢查條件表達式,如果符合,就會執(zhí)行循環(huán)體內(nèi)的語句:如果不符合,就會跳離循環(huán)。因此循環(huán)內(nèi)的某一段語句必須以改變條件表達式的值來結(jié)束循環(huán)的執(zhí)行,否則就會形成無限循環(huán)。
二、do-while循環(huán)
無論是 while 循環(huán)還是 do/while 循環(huán)都是用來處理未知循環(huán)執(zhí)行次數(shù)的程序。while 循環(huán)先進行條件運算,再進入循環(huán)體執(zhí)行語句;do/while 循環(huán)恰好相反,先執(zhí)行循環(huán)體內(nèi)的語句,再進行條件運算。對于 do/while 循環(huán)來說,循環(huán)體內(nèi)的語句至少會被執(zhí)行一次;while 循環(huán)在條件運算不符合的情況下不會進入循環(huán)體來執(zhí)行語句。
do/while 循環(huán)語法如下:
do { //程序語句; }while(條件運算);
不要忘記條件運算后要有“:”結(jié)束循環(huán)。那么什么情況下會使用 do/while 循環(huán)呢?通常是詢問用戶是否要讓程序繼續(xù)執(zhí)行時。下面還是以“1+2+3+...+10”為例來認識一下 do/while循環(huán),語句如下:
int counter=1,sum=0://counter是計數(shù)器 do { sum += counter;//sum 存儲數(shù)值累加的結(jié)果 counter++;//控制運算:讓計數(shù)器累加 }while(counter<= 10);//條件表達式
表示會進入循環(huán)體內(nèi),運行程序語句后再進行條件運算,若條件為true 則繼續(xù)執(zhí)行,不斷重復(fù),直到 while 語句的條件運算為 fàalse 才會離開循環(huán)。它的流程控制圖如圖 4-25 所示。
語句解析:
代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //User defined using static System.Console;//導(dǎo)入靜態(tài)類 namespace while_dowhile { class Program { static void Main(string[] args) { //while循環(huán) int sum = 0; int counter = 1;//計數(shù)器 while (counter <= 10) { sum += 1; counter++;//將計數(shù)累加 } Console.WriteLine("累加結(jié)果:{0}", sum); //dowhile循環(huán) int counter2 = 1,sum2 = 0;//counter是計數(shù)器 do { sum2 += 1;//sum存儲數(shù)值累加的結(jié)果 counter2++;//控制運算:讓計數(shù)器累加 } while (counter2 <= 10);//條件表達式 Console.WriteLine("累加結(jié)果:{0}", sum2); //while循環(huán) + break中斷+continue int sum3 = 0; int num1 = 0; int counter3 = 1;//計數(shù)器 while (counter3 <= 10) { sum3 += 1; counter3++;//將計數(shù)累加 if (counter3 % 2 == 0)//找出奇數(shù) { num1 += 1; WriteLine("執(zhí)行continue"); continue;//繼續(xù)循環(huán) } if (sum3>7) { WriteLine("sum3>7,中斷"); break; } } Console.WriteLine("累加結(jié)果:{0}", sum3); Console.WriteLine("執(zhí)行continue{0}", num1); ReadKey(); } } }
三、中斷語句break和跳出本次執(zhí)行循環(huán)continue
一般來說,break 語句用來中斷循環(huán)的執(zhí)行,continue 語句用來暫停當前執(zhí)行的語句,它會回到當前語句的上一個區(qū)塊,讓程序繼續(xù)執(zhí)行下去。因此,可以在for、while、do...while循環(huán)中的程序語句中加入 break或 continue 語句,使用一個簡單的范例來說明這兩者之間的差異。
代碼:
//while循環(huán) + break中斷+continue int sum3 = 0; int num1 = 0; int counter3 = 1;//計數(shù)器 while (counter3 <= 10) { sum3 += 1; counter3++;//將計數(shù)累加 if (counter3 % 2 == 0)//找出奇數(shù) { num1 += 1; WriteLine("執(zhí)行continue"); continue;//繼續(xù)循環(huán) } if (sum3>7) { WriteLine("sum3>7,中斷"); break; } } Console.WriteLine("累加結(jié)果:{0}", sum3); Console.WriteLine("執(zhí)行continue{0}", num1);
總結(jié)
到此這篇關(guān)于C#中while循環(huán)和do-while循環(huán)的文章就介紹到這了,更多相關(guān)C# while循環(huán)和do-while循環(huán)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入多線程之:解析線程的交會(Thread Rendezvous)詳解
本篇文章是對線程的交會(Thread Rendezvous)進行了詳細的分析介紹,需要的朋友參考下2013-05-05關(guān)于C# 4.0新特性“缺省參數(shù)”的實現(xiàn)詳解
這篇文章主要給大家介紹了關(guān)于C# 4.0新特性“缺省參數(shù)”的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家學習或者使用C# 4.0具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2020-06-06C#讀取多條數(shù)據(jù)記錄導(dǎo)出到Word之圖片輸出改造
這篇文章主要為大家詳細介紹了C#讀取多條數(shù)據(jù)記錄并導(dǎo)出到Word標簽?zāi)0逯械膱D片輸出問題,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2024-11-11