while (expression){
Statement(s) to be executed if expression is true
}
while循环的目的是为了反复执行语句或代码块(只要表达式为true)。一旦表达式为假,则循环将被退出。
例子:下面的例子说明了一个基本的while循环:
这将产生以下结果:
Starting Loop Current Count : 0 Current Count : 1 Current Count : 2 Current Count : 3 Current Count : 4 Current Count : 5 Current Count : 6 Current Count : 7 Current Count : 8 Current Count : 9 Loop stopped!
do...while 循环:
do...while loop 类似于while循环,不同之处在于条件检查发生在循环的末端。这意味着,在循环将总是至少执行一次,即使条件为假。
语法
do{
Statement(s) to be executed;
} while (expression);
注意在do... while循环的末尾使用分号。
例子:如在上面的例子中编写一个使用do... while循环程序。
这将产生以下结果:
Starting Loop Current Count : 0 Loop stopped!



