开发者

C#中while循环和do-while循环举例详解

目录
  • 一、while循环
  • 二、do-while循环
  • 三、中断语句break和跳出本次执行循环continue
  • 总结 

一般情况下,知道循环几次采用for循环,如果不知道循环要执行几次,那么 while 循环或 do-while 循环就是比较好的处理方式。

一、while循环

while(条件表达式)
{
    条件表达式成立(true)时要执行的语句;
}

进入 while 循环时,必须先检查条件表达式,如果符合,就会执行循环体内的语句:如果不符合,就会跳离循环。因此循环内的某一段语句必须以改变条件编程客栈表达式的值来结束循环的执行,否则就会形成无限循环。

二、do-while循环

无论是 while 循环还是 do/while 循环都是用来处理未知循环执行次数的程序。while 循环先进行条件运算,再进入循环体执行语句;do/while 循环恰好相反,先执行循环体内的语句,再进行条件运算。对于 do/while 循环来说,循环体内的语句至少会被执行一次;while 循环在条件运算不符合的情况下不会进入循环体来执行语句。

do/while 循环语法如下:

do {
//程序语句;
}while(条件运算);

不要忘记条件运算后要有“:”结束循环。那么什么情况下会使用 do/while 循环呢?通常是询问用户是否要让程序继续执行时。下面还是以&ldquowww.devze.com;1+2+3+...+10”为例来认识一下 do/while循环,语句如下:

int counter=1,sum=0://counter是计数器
do {
    sum += counter;//sum 存储数值累加的结果
    counter++;//控制运算:让计数器累加
}while(counter<= 10);//条件表达式

表示会进入循环体内,运行程序语句后再进行条件运算,若条件为true 则继续执行,不断重复,直到 while 语句的条件运算为 fàalse 才会离开循环。它的流程控制图如图 4-25 所示。

C#中while循环和do-while循环举例详解

C#中while循环和do-while循环举例详解

语句解析:

C#中while循环和do-while循环举例详解

代码:

using System;
using System.Colle编程ctions.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//User defined
using static System.Console;//导入静态类

namespace while_dowhile
{
    class Program
    {

        static void Main(string[] args)
        {
            //while循环
            int sum = 0;
            int counter = 1;//计数器
            while (counter <= 10)
            {
                sum += 1;
                counter++;//将计数累加
            }
            Console.WriteLine("累加结果:{0}", sum);

            //dowhile循环
            int counter2 = 1,sum2 = 0;//counter是计数器
            do {
                sum2 += 1;//sum存储数值累加的结果
                counter2++;//控制运算:让计数器累加
            } while (counter2 <= 10);//条件表达式
            Console.WriteLine("累加结果:{0}", sum2);

            //while循环 + break中断+continue
            int sum3 = 0;
            int num1 = 0;
            int counter3 = 1;//计数器
            while (counter3 <= 10)
            {
                sum3 += 1;
                counter3++;//将计数累加
                if (counter3 % 2 == 0)//找出奇数
                {
                    num1 += 1;
                    WriteLine("执行continue");
                    continue;//继续循环
                }


                if (sum3>7)
                {
                    WriteLine("sum3>7,中断");
                    break;
                }
            }
            Console.WriteLine("累加结果:{0}", sum3);
            Console.WriteLine("执行continue{0}", num1);

            ReadKey();
        }
js    }
}

三、中断语句break和跳出本次执行循环continue

一般来说,break 语句用来中断循环的执行,continue 语句用来暂停当前执行的语句,它会回到当前语句的上一个区块,让程序继续执行下去。因此,可以在for、while、do...while循环中的程序语句中加入 break或 continue 语句,使用一个简单的范例来说明这两者之间的差异。

代码:

            //while循环 + break中断+continue
            int sum3 = 0;
            int num1 = 0;
            int counter3 = 1;//计数器
            while (counter3 <= 10)
            {
                sum3 += 1;
                counter3++;//将计数累加
                if (counter3 % 2 == 0)//找出奇数
                {
                    num1 += 1;
                    WriteLine("执行continue");
                    continue;//继续循环
                }


                iandroidf (sum3>7)
                {
                    WriteLine("sum3>7,中断");
                    break;
                }
            }
            Console.WriteLine("累加结果:{0}", sum3);
            Console.WriteLine("执行continue{0}", num1);

C#中while循环和do-while循环举例详解

总结 

到此这篇关于C#中while循环和do-while循环的文章就介绍到这了,更多相关C# while循环和do-while循环内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜