while(condition) { statement(s); }
#include <stdio.h> int main () { /* 局部变量定义 */ int a = 10; /* while 循环执行 */ while( a < 20 ) { printf("a 的值: %d\n", a); a++; } return 0; }
for ( init; condition; increment ) { statement(s); }
#include <stdio.h> int main () { /* for 循环执行 */ for( int a = 10; a < 20; a = a + 1 ) { printf("a 的值: %d\n", a); } return 0; }
do { statement(s); }while( condition );
#include <stdio.h> int main () { /* 局部变量定义 */ int a = 10; /* do 循环执行 */ do { printf("a 的值: %d\n", a); a = a + 1; }while( a < 20 ); return 0; }
for (initialization; condition; increment/decrement) { statement(s); for (initialization; condition; increment/decrement) { statement(s); ... ... ... } ... ... ... }
while (condition1) { statement(s); while (condition2) { statement(s); ... ... ... } ... ... ... }
do { statement(s); do { statement(s); ... ... ... }while (condition2); ... ... ... }while (condition1);
#include <stdio.h> int main () { /* 局部变量定义 */ int i, j; for(i=2; i<100; i++) { for(j=2; j <= (i/j); j++) if(!(i%j)) break; // 如果找到,则不是质数 if(j > (i/j)) printf("%d 是质数\n", i); } return 0; }
#include <stdio.h> int main() { int i=1,j; while (i <= 5) { j=1; while (j <= i ) { printf("%d ",j); j++; } printf("\n"); i++; } return 0; }
#include <stdio.h> int main() { int i=1,j; do { j=1; do { printf("*"); j++; }while(j <= i); i++; printf("\n"); }while(i <= 5); return 0; }
#include <stdio.h> int main () { /* 局部变量定义 */ int a = 10; /* while 循环执行 */ while( a < 20 ) { printf("a 的值: %d\n", a); a++; if( a > 15) { /* 使用 break 语句终止循环 */ break; } } return 0; }
#include <stdio.h> int main () { /* 局部变量定义 */ int a = 10; /* do 循环执行 */ do { if( a == 15) { /* 跳过迭代 */ a = a + 1; continue; } printf("a 的值: %d\n", a); a++; }while( a < 20 ); return 0; }
goto label; .. . label: statement;
#include <stdio.h> int main () { /* 局部变量定义 */ int a = 10; /* do 循环执行 */ LOOP:do { if( a == 15) { /* 跳过迭代 */ a = a + 1; goto LOOP; } printf("a 的值: %d\n", a); a++; }while( a < 20 ); return 0; }
#include <stdio.h> int main () { for( ; ; ) { printf("该循环会永远执行下去!\n"); } return 0; }