例5.2从键盘连续输入字符,直到输入“回车”符为 统计输入的字符个 star t len=o (ch=getcho)!+r len++ 输出len stop 图53统计输入字符的个数
例 5.2 从键盘连续输入字符,直到输入“回车”符为 止, 统计输入的字符个数st ar t 。 le n= 0; (c h= ge tc h( )) !=′ / r′ st op 输出len N Y le n+ + 图 5.3 统计输入字符的个数
t include <stdio. h> maino h char c int len=0 puts( Type in a sentence then press <Enter>In while((ch=getcho)=r) i putchar(ch) len++ printf("nSentenceis %d characters long. n",len) 运行结果 Type in a sentence, then press <Enter> this is a sentence Sentence is 19 characters long
#include <stdio.h> main() { char ch; int len=0; puts("Type in a sentence, then press <Enter>\n"); while ((ch=getch())!=′\r′) { putchar(ch); len++; } printf("\nSentence is %d characters long.\n", len); } 运行结果: Type in a sentence, then press <Enter> this is a sentence. Sentence is 19 characters long
52do- while循环语句 do-whil循环的一般形式如下 a0 语句 while(表达式
5.2 do-while循环语句 do-while循环的一般形式如下: do while (表达式);
循环体 满足 循环体 表达式 直到表达式为假 不满足 (b) 图54do- while循环
图 5.4 do-while循环 (a) (b) 表达式 不满足 满 足 直到表达式为假 循环体 循环体
do-while语句的执行过程是:先执行循环体中的语句, 然后计算表达式的值。若表达式的值为“真”(非0), 则再次执行循环体。如此重复,直至表达式的值为“假” (0)时,结束循环
do—while语句的执行过程是:先执行循环体中的语句, 然后计算表达式的值。若表达式的值为“真”(非0), 则再次执行循环体。如此重复,直至表达式的值为“假” (0)时, 结束循环