Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 16.1 Loops provide a mechanism for repeating a block of code called the loop

ID: 3864918 • Letter: L

Question

Lab 16.1

Loops provide a mechanism for repeating a block of code called the loop body. We begin this lab by experimenting with while loops, the simplest form of loop code. Many loops are controlled with a single variable, which we will refer to as the loop control variable or the loop index.

Consider the code below. What is the output the program produces? Try changing the assigned value (6) of the limit variable to various integer values (e.g., 10, 100, 0, -10). What happens?

/**
   A simple program that prints a loop control variable.
*/
public class SimpleLoop
{
   public static void main(String[] args)
   {
      int i = 0;
      int limit = 6;
      while (i < limit)
      {
         System.out.println("i = " + i);
         i++;
      }
   }
}

Lab 16.2

Consider the code below again. What happens if you comment out the line that increments i (line 13)? Will the program ever stop looping?

/**
   A simple program that prints a loop control variable.
*/
public class SimpleLoop
{
   public static void main(String[] args)
   {
      int i = 0;
      int limit = 6;
      while (i < limit)
      {
         System.out.println("i = " + i);
i++; // line 13
      }
   }
}

Lab 16.3

Manipulating the loop control variable is a critical skill in learning to write code with loops. Modify the program in Lab 16.1 so that it produces the following output:

i = 6
i = 8
i = 10
i = 12
i = 14
i = 16
i = 18
...
i = 98

Explanation / Answer

16.1)for limit =0 or -10

while(i<0) is wrong so loop no out put

for limit =10

while(i<10)

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9

limit =100

while (i<100)

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10
i = 11
i = 12
i = 13
i = 14
i = 15
i = 16
i = 17
i = 18
i = 19
i = 20
i = 21
i = 22
i = 23
i = 24
i = 25
i = 26
i = 27
i = 28
i = 29
i = 30
i = 31
i = 32
i = 33
i = 34
i = 35
i = 36
i = 37
i = 38
i = 39
i = 40
i = 41
i = 42
i = 43
i = 44
i = 45
i = 46
i = 47
i = 48
i = 49
i = 50
i = 51
i = 52
i = 53
i = 54
i = 55
i = 56
i = 57
i = 58
i = 59
i = 60
i = 61
i = 62
i = 63
i = 64
i = 65
i = 66
i = 67
i = 68
i = 69
i = 70
i = 71
i = 72
i = 73
i = 74
i = 75
i = 76
i = 77
i = 78
i = 79
i = 80
i = 81
i = 82
i = 83
i = 84
i = 85
i = 86
i = 87
i = 88
i = 89
i = 90
i = 91
i = 92
i = 93
i = 94
i = 95
i = 96
i = 97
i = 98
i = 99

-------------------

16.2)if we remove the increment

i++;

the the i value is o always and it is a infinity loop

16.3)

public class loop {
   public static void main(String[] args)
   {
   int i = 6;
   int limit = 100;
   while (i < limit)
   {
   System.out.println("i = " + i);
   i=i+2;
   }
   }
}

out put

i = 6
i = 8
i = 10
i = 12
i = 14
i = 16
i = 18
i = 20
i = 22
i = 24
i = 26
i = 28
i = 30
i = 32
i = 34
i = 36
i = 38
i = 40
i = 42
i = 44
i = 46
i = 48
i = 50
i = 52
i = 54
i = 56
i = 58
i = 60
i = 62
i = 64
i = 66
i = 68
i = 70
i = 72
i = 74
i = 76
i = 78
i = 80
i = 82
i = 84
i = 86
i = 88
i = 90
i = 92
i = 94
i = 96
i = 98