- Back to Home »
- C »
- C - Loops
Monday, April 27, 2015
1. while
Syntax:
Kiểm tra trước, thực hiện sau
 while(condition)  
 {  
   statement(s);  
 }  
Flow:
Ex:
 #include <stdio.h>  
 int main ()  
 {  
   /* local variable definition */  
   int a = 10;  
   /* while loop execution */  
   while( a < 20 )  
   {  
    printf("value of a: %d\n", a);  
    a++;  
   }  
   return 0;  
 }  
Execute
 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  
2. do ... while
Syntax:
Thực hiện trước, kiểm tra sau
 do  
 {  
   statement(s);  
 }while( condition );  
Flow:
Ex:
 #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;  
 }  
3. for
Syntax:
Lặp vòng với số lần xác định
 for ( init; condition; increment )  
 {  
   statement(s);  
 }  
Flow:
Ex:
 #include <stdio.h>  
 int main ()  
 {  
   /* for loop execution */  
   for( int a = 10; a < 20; a = a + 1 )  
   {  
    printf("value of a: %d\n", a);  
   }  
   return 0;  
 }  
Compile & Execute
 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  
4. Nested Loop
Vòng lặp lồng nhau
 for ( init; condition; increment )  
 {  
   for ( init; condition; increment )  
   {  
    statement(s);  
   }  
   statement(s);  
 }  
 while(condition)  
 {  
   while(condition)  
   {  
    statement(s);  
   }  
   statement(s);  
 }  
 do  
 {  
   statement(s);  
   do  
   {  
    statement(s);  
   }while( condition );  
 }while( condition );  
Ex:
 #include <stdio.h>  
 int main ()  
 {  
   /* local variable definition */  
   int i, j;  
   for(i=2; i<100; i++) {  
    for(j=2; j <= (i/j); j++)  
     if(!(i%j)) break; // if factor found, not prime  
    if(j > (i/j)) printf("%d is prime\n", i);  
   }  
   return 0;  
 }  
5. Infinite loop
Vòng lặp không xác định (lặp mãi mãi)
 #include <stdio.h>  
 int main ()  
 {  
   for( ; ; )  
   {  
    printf("This loop will run forever.\n");  
   }  
   return 0;  
 }  
 #include <stdio.h>  
 int main ()  
 {  
   while(1)  
   {  
    printf("This loop will run forever.\n");  
   }   
   return 0;  
 }  
 #include <stdio.h>  
 int main ()  
 {    
   do{  
     printf("This loop will run forever.\n");      
   }while(1);  
   return 0;  
 }  
6. Loop Control Statements
break, continue và goto làm thay đổi trình tự thực hiện của các câu lệnh trong vòng lặp.
| Control Statement | Description | 
|---|---|
| break | Thoát khỏi vòng lặp hoặc switch | 
| continue | Bỏ qua các câu lệnh phía sau continue và chuyển sang kiểm tra và thực hiện vòng lặp mới | 
| goto | Nhảy đến label | 
6.1 Break
Syntax:
+ Thoát khỏi vòng lặp while, do ... while và for (nếu có nhiều vòng lặp lồng nhau thì chỉ thoát ra khỏi vòng lặp trực tiếp chứa nó)
+ Thoát khỏi case trong điều kiện switch
Flow:
Ex:
 #include <stdio.h>  
 int main ()  
 {  
   /* local variable definition */  
   int a = 10;  
   printf("========== while loop execution ============ \n");  
   /* while loop execution */  
   while( a < 20 )  
   {  
     printf("<while> value of a: %d\n", a);  
     a++;  
     if( a > 15)  
     {  
       /* terminate the loop using break statement */  
       printf("<while> break \n");  
       break;  
     }  
   }  
   printf("========== do ... while loop execution ============ \n");  
   a = 10;  
   /* do ... while loop execution */  
   do  
   {  
     printf("<do while> value of a: %d\n", a);  
     a++;  
     if( a > 15)  
     {  
       /* terminate the loop using break statement */  
       printf("<do while> break \n");  
       break;  
     }  
   }while( a < 20 );  
   printf("========== for loop execution ============ \n");  
   /* for loop execution */  
   for(a = 10; a < 20; a++){  
     printf("<for> value of a: %d\n", a);  
     if( a > 15)  
     {  
       /* terminate the loop using break statement */  
       printf("<for> break \n");  
       break;  
     }  
   }  
   printf("========== nested loop execution ============ \n");  
   int b;  
   a = 10;  
   /* nested loop execution */  
   while( a < 15 )  
   {  
     printf("<parent while> value of a: %d \n", a);  
     a++;  
     for(b = 0; b < 10; b++){  
       printf("<child for> value of b: %d \n", b);  
       if( b > 5)  
       {  
         /* terminate the loop using break statement */  
         printf("<child for> break \n");  
         break;  
       }  
     }  
   }  
   return 0;  
 }  
Compile & Execute
 $ gcc break.c   
 $ ./a.out   
 ========== while loop execution ============   
 <while> value of a: 10  
 <while> value of a: 11  
 <while> value of a: 12  
 <while> value of a: 13  
 <while> value of a: 14  
 <while> value of a: 15  
 <while> break   
 ========== do ... while loop execution ============   
 <do while> value of a: 10  
 <do while> value of a: 11  
 <do while> value of a: 12  
 <do while> value of a: 13  
 <do while> value of a: 14  
 <do while> value of a: 15  
 <do while> break   
 ========== for loop execution ============   
 <for> value of a: 10  
 <for> value of a: 11  
 <for> value of a: 12  
 <for> value of a: 13  
 <for> value of a: 14  
 <for> value of a: 15  
 <for> value of a: 16  
 <for> break   
 ========== nested loop execution ============   
 <parent while> value of a: 10   
 <child for> value of b: 0   
 <child for> value of b: 1   
 <child for> value of b: 2   
 <child for> value of b: 3   
 <child for> value of b: 4   
 <child for> value of b: 5   
 <child for> value of b: 6   
 <child for> break   
 <parent while> value of a: 11   
 <child for> value of b: 0   
 <child for> value of b: 1   
 <child for> value of b: 2   
 <child for> value of b: 3   
 <child for> value of b: 4   
 <child for> value of b: 5   
 <child for> value of b: 6   
 <child for> break   
 <parent while> value of a: 12   
 <child for> value of b: 0   
 <child for> value of b: 1   
 <child for> value of b: 2   
 <child for> value of b: 3   
 <child for> value of b: 4   
 <child for> value of b: 5   
 <child for> value of b: 6   
 <child for> break   
 <parent while> value of a: 13   
 <child for> value of b: 0   
 <child for> value of b: 1   
 <child for> value of b: 2   
 <child for> value of b: 3   
 <child for> value of b: 4   
 <child for> value of b: 5   
 <child for> value of b: 6   
 <child for> break   
 <parent while> value of a: 14   
 <child for> value of b: 0   
 <child for> value of b: 1   
 <child for> value of b: 2   
 <child for> value of b: 3   
 <child for> value of b: 4   
 <child for> value of b: 5   
 <child for> value of b: 6   
 <child for> break   
6.2 continue
Syntax:
Bỏ qua các câu lệnh phía sau continue chuyển sang kiểm tra và thực hiện vòng lặp mới
Flow:
Ex:
 #include <stdio.h>  
 int main ()  
 {  
   /* local variable definition */  
   int a = 10;  
   /* do loop execution */  
   do  
   {  
    if( a == 15)  
    {  
      /* skip the iteration */  
      a = a + 1;  
      continue;  
    }  
    printf("value of a: %d\n", a);  
    a++;  
   }while( a < 20 );  
   return 0;  
 }  
Execute
 value of a: 10  
 value of a: 11  
 value of a: 12  
 value of a: 13  
 value of a: 14  
 value of a: 16  
 value of a: 17  
 value of a: 18  
 value of a: 19  
6.3 goto
Syntax:
Nhảy đến một label
 goto label;  
 ..  
 .  
 label: statement;  
Flow:
Ex:
 #include <stdio.h>  
 int main ()  
 {  
   /* local variable definition */  
   int a = 10;  
   /* do loop execution */  
   LOOP:do  
   {  
    if( a == 15)  
    {  
      /* skip the iteration */  
      a = a + 1;  
      goto LOOP;  
    }  
    printf("value of a: %d\n", a);  
    a++;  
   }while( a < 20 );  
   return 0;  
 }  
Execute:
 value of a: 10  
 value of a: 11  
 value of a: 12  
 value of a: 13  
 value of a: 14  
 value of a: 16  
 value of a: 17  
 value of a: 18  
 value of a: 19  
 









 
 
 
 
 
 
 
 
