Answers to questions on for loops

  1. What is the output from the following loops?

    1. for (i=3; i>=1; i--) printf("%d\n", i);
      
      3
      2
      1
    2. for (i=0; i<=3; i++) printf("%d\n", i);
      
      0
      1
      2
      3

  2. What is the while loop equivalent to each of the above loops?

    1. i=3; while(i>=1) { printf("%d\n", i); i--; }
    2. i=0; while(i<=1) { printf("%d\n", i); i++; }
  3. Put each of these examples, including the while loop equivalents, into a program, compile and run them. Make sure they behave identically.
Back to the questions.


Back to the notes on for loops.