Advertisement
dmilicev

for loop increment decrement operators v1.c

Oct 16th, 2019
194
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None
  1. /*
  2.     for loop increment decrement operators v1.c
  3.  
  4.     Corrected code.
  5.  
  6. I understood the misunderstanding and corrected my code to make it clearer.
  7.  
  8. i is loop counter,
  9. numberOfPasses is the loop execution counter.
  10.  
  11. These are different things.
  12.  
  13. Notice that their values are displayed as they are inside the loop body.
  14.  
  15.     for ( i=0 ; i++ < 10 )
  16.  
  17.     for ( i=0 ; ++i <=10 )
  18.  
  19. What is the difference between these two for loops?
  20.  
  21. What will be the outcome of this?
  22.  
  23. In C program, Code::Blocks reports error:
  24. expected ';' before ')' token
  25.  
  26. because there is no increment part in for loops.
  27.  
  28. for ( init; condition; increment )
  29. {
  30.    statement(s);
  31. }
  32.  
  33. for loop must have two ; between brackets ()
  34.     for( ; ; )
  35.  
  36. So let's write like this
  37.  
  38.     for ( i=0 ; i++ < 10 ; )
  39.  
  40.     for ( i=0 ; ++i <=10 ; )
  41.  
  42. or
  43.  
  44.     for ( i=0 ; i < 10 ; i++ )
  45.  
  46.     for ( i=0 ; i <= 10 ; ++i )
  47.  
  48. Let's write simple printfs that will answer these questions.
  49.  
  50. We have debugger, but this is simlpliest and fastest way to find answers.
  51.  
  52. Let's use this method in the future to find the answers ourselves.
  53.  
  54. Look this:
  55.  
  56. https://www.2braces.com/c-questions/for-loop-questions-c-3
  57.  
  58. */
  59.  
  60. #include "stdio.h"
  61.  
  62. int main(void)
  63. {
  64.     // i is loop counter, numberOfPasses is the loop execution counter
  65.     int i, numberOfPasses;
  66.  
  67.     printf("\n");
  68.     numberOfPasses = 0;         // reset numberOfPasses for next counting
  69.  
  70.     for ( i=0 ; i++ < 10 ; )
  71.     {
  72.         numberOfPasses++;       // we count passes through loop
  73.         printf("\n 1. for loop: i = %2d  ,  numberOfPasses = %2d", i, numberOfPasses);
  74.     }
  75.  
  76.     printf("\n");
  77.     numberOfPasses = 0;         // reset numberOfPasses for next counting
  78.  
  79.     for ( i=0 ; ++i <=10 ; )
  80.     {
  81.         numberOfPasses++;       // we count passes through loop
  82.         printf("\n 2. for loop: i = %2d  ,  numberOfPasses = %2d", i, numberOfPasses);
  83.     }
  84.  
  85.     printf("\n");
  86.     numberOfPasses = 0;         // reset numberOfPasses for next counting
  87.  
  88.     for ( i=0 ; i < 10 ; i++ )
  89.     {
  90.         numberOfPasses++;       // we count passes through loop
  91.         printf("\n 3. for loop: i = %2d  ,  numberOfPasses = %2d", i, numberOfPasses);
  92.     }
  93.  
  94.     printf("\n");
  95.     numberOfPasses = 0;         // reset numberOfPasses for next counting
  96.  
  97.     for ( i=0 ; i <= 10 ; ++i )
  98.     {
  99.         numberOfPasses++;       // we count passes through loop
  100.         printf("\n 4. for loop: i = %2d  ,  numberOfPasses = %2d", i, numberOfPasses);
  101.     }
  102.  
  103.     printf("\n");
  104.  
  105.     return 0;
  106. }
Advertisement
RAW Paste Data Copied
Advertisement