while语句中说到的break语句、continue语句、goto语句等在for语句中仍然适用,这里就不再重复。本章以例子说明各种各样的for循环。
//这是标准的for循环例子#include
<stdio.h>int
main(void
) {int
i;for
(i = 0; i < 3; i++) { printf("i = %2d\n", i); } printf("------\ni = %2d\n", i);return
0; }
//在循环体内控制终止条件#include
<stdio.h>int
main(void
) {int
i = 3 ;for
( ; ; ) { printf("i = %2d\n", i--) ;if
(i == 0)break
; }return
0; }
#include
<stdio.h>#include
<conio.h>int
main(void
) {char
ch; //注意:循环体内一句也没有写for
(ch = getche(); ch != 'q'; ch = getche()) ; printf("你输入了q字符。\n");return
0; }
#include
<stdio.h>int
main(void
) {int
i, j ;for
( i = 1; i < 10; i++ ) {for
( j = 1; j <= i; j++ ) { printf("%d×%d=%2d ", i, j, i * j); } printf("\n"); }return
0; }