Page 196 - CA_Blue( J )_Class9
P. 196
Condition for Testing
The loop will stop executing when the condition changes its Boolean value, i.e., true to false. In the example, the
loop will stop when the value of i becomes 10 or greater, i.e., when i < 10 is no longer true.
Increment or Decrement
Each and every iteration will increase the value of the variable by 1 i.e. i++ or i=i+1.
Body of the Loop
Every loop has statements that the user wants to repeat to perform the desired task. In the above example,
products of all the numbers generated have to be calculated, i.e., p=p*i;
Program 1 Print the product of all the numbers from 1 to 10.
1 class product
2 {
3 public static void main ()
4 {
5 int i, p=1;
6 for (i=1; i<=10; i++) //LOOP
7 {
8 p=p*i;
9 }
10 System.out.println("Product of numbers from 1 to 10: "+p);
11 }
12 }
Dry run and Output
i p=p*i output
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800 Product of numbers from 1 to 10: 3628800
9.2 CATEGORIES OF LOOP
Loops can be divided into two categories based on when the loop's condition is evaluated relative to the loop's
body: at the entry level or at the exit level.
194 Touchpad Computer Applications-IX

