目录

do...while循环

while循环开始时测试循环条件的for循环和while循环不同, do...while循环do...while循环结束do...while检查其条件。

do...while循环类似于while循环,除了do ... while循环保证至少执行一次。

语法 (Syntax)

C#中do...while循环的语法是 -

do {
   statement(s);
} while( condition );

请注意,条件表达式出现在循环的末尾,因此循环中的语句在测试条件之前执行一次。

如果条件为真,则控制流跳回来执行,循环中的语句再次执行。 重复此过程直到给定条件变为假。

流程图 (Flow Diagram)

在C#中循环...

例子 (Example)

using System;
namespace Loops {
   class Program {
      static void Main(string[] args) {
         /* local variable definition */
         int a = 10;
         /* do loop execution */
         do {
            Console.WriteLine("value of a: {0}", a);
            a = a + 1;
         } 
         while (a < 20);
         Console.ReadLine();
      }
   }
} 

编译并执行上述代码时,会产生以下结果 -

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
↑回到顶部↑
WIKI教程 @2018