Advertisement
dmilicev

loops_in_c.c

Dec 6th, 2019
386
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None
  1. /*
  2.  
  3.     loops_in_c.c        by Dragan Milicev
  4.  
  5.  
  6. There is no difference between loops in C,
  7. except that the do-while loop must be performed at least once.
  8.  
  9.  
  10. for loop
  11. https://www.tutorialspoint.com/cprogramming/c_for_loop.htm
  12.  
  13. for ( init; condition; increment ) {
  14.    statement(s);
  15. }
  16.  
  17.  
  18. while loop
  19. https://www.tutorialspoint.com/cprogramming/c_while_loop.htm
  20.  
  21. init;
  22. while(condition) {
  23.    statement(s);
  24.    increment;
  25. }
  26.  
  27.  
  28. do-while loop
  29. https://www.tutorialspoint.com/cprogramming/c_do_while_loop.htm
  30.  
  31. init;
  32. do {
  33.    statement(s);
  34.    increment;
  35. } while( condition );
  36.  
  37.  
  38.     You can find all my C programs at Dragan Milicev's pastebin:
  39.  
  40.     https://pastebin.com/u/dmilicev
  41.  
  42. */
  43.  
  44. #include <stdio.h>
  45.  
  46. int main(void)
  47. {
  48.     int i, n=3;
  49.  
  50.     for( i=0; i<n; i++ )                            // ( init; condition; increment )
  51.         printf("\n for loop: i = %d \n", i );       // statements
  52.  
  53.  
  54.     i=0;                                            // init
  55.  
  56.     while ( i<n )                                   // condition
  57.     {
  58.         printf("\n while loop: i = %d \n", i );     // statements
  59.         i++;                                        // increment
  60.     }
  61.  
  62.  
  63.     i=0;                                            // init
  64.  
  65.     do
  66.     {
  67.         printf("\n do-while loop: i = %d \n", i );  // statements
  68.         i++;                                        // increment
  69.     } while ( i<n );                                // condition
  70.  
  71.  
  72.     return 0;
  73. }
Advertisement
RAW Paste Data Copied
Advertisement