C语言 do...while循环
2019-09-09
8
0
与for和while循环不同,for和while循环在循环顶部测试循环条件,而C编程中的do.while循环在循环底部检查其条件。
do.while循环类似于while循环,不同之处在于它保证至少执行一次。
do…while - 语法
C编程语言中do.while循环语法是:
do {
statement(s);
} while( condition );
do…while - 流程图
do…while - 示例
#include <stdio.h>
int main () {
/* local variable definition */
int a = 10;
/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}
编译并执行上述代码后,将产生以下结果-
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