Loop
For
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. for(int i = 0; i < 10; i++)
  6. {//start block
  7. printf("Hello World!\n");
  8. }//end block
  9.  
  10. // the braces can be ignored if there is only one line of code
  11. for(int i = 0; i < 10; i++)
  12. printf("Hello World!\n");
  13.  
  14. return 0;
  15. }
  • break, provides an early exit from for, while, and do ... while, and switch
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5. for(int i = 0; i < 10; i++)
    6. {
    7. if(i == 6) break;
    8. printf("%d\n", i);
    9. }
    10.  
    11. return 0;
    12. }
  • continue, skip the rest statements of the current iteration, applies only to loops, not to switch
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5. for(int i = 0; i < 10; i++)
    6. {
    7. if(i == 6) continue;
    8. printf("%d\n", i);
    9. }
    10.  
    11. return 0;
    12. }
  • Nested For Loop
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5. for(int i = 1; i < 10; i++)
    6. {
    7. for(int j = 1; j < 10; j++)
    8. printf("%3d * %3d = %4d", i, j, i*j);
    9.  
    10. printf("\n");
    11. }
    12.  
    13. return 0;
    14. }
    While
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5. int i = 10;
    6.  
    7. while (i > 0)
    8. {
    9. printf("%d\n", i);
    10. i--;
    11. }
    12.  
    13. return 0;
    14. }
    Do ... While
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5. int i = 10;
    6.  
    7. do
    8. {
    9. printf("%d\n", i);
    10. i--;
    11. }while ( i > 0 );
    12.  
    13. return 0;
    14. }