Java: Loop Exercises 1

Name: _________________________________

What is the value of the variable n after the code in these questions? If a loop never finishes, put infinite in the answer space.

Assume: int i, j, n;

  1. Value of n? __________
       n = 0;
       i = 1;
       while (i < 4) {
          n++;
          i++;
       }
    
  2. Value of n? __________
       n = 0;
       i = 1;
       do {
          n++;
          i++;
       } while (i <= 5);
    
  3. Value of n? __________
       n = 0;
       i = 1;
       while (!(i > 4)) {
          n = n + 1;
       }
    
  4. Value of n? __________
       n = 0;
       i = 1;
       while (i < 4) {
          for (j=1; j<=i; j++) {
             n += 1;
          }
          i = i + 1;
       }
    
  5. Value of n? __________
       n = 0;
       i = 3;
       while (i < 4) {
          n++;
          i--;
       }