程序清单6.15 do_while.c
#include <stdio.h>
int main (void)
{
const int secret_code = 13;
int code_entered;
do
{
printf("To enter the triskaidekaphobia therapy club,\n");
printf("please enter the secret code number: ");
scanf("%d",&code_entered);
}
while (code_entered!=secret_code);
printf("Congratulations! You are cured!\n");
return 0;
}
下面是do while循环的一般形式:
do
statement
while (expression);
请注意do while循环本身是一个语句,因此它需要一个结束的分号!
应该把do while循环仅用于那些至少需要执行一次循环的情况。例如,一个密码程序要包括一个循环,它的伪代码如下:
do
{
prompt for password
read user input
}while(input not epual to passward);