LIWA

Looping / Iterative statements in Java

ICSE Class 9 · Computer Applications · Chapter 9

What this chapter covers

Key points

Questions answered

Define iteration. Why are loops needed in a program?
Iteration is the repeated execution of a statement or group of statements as long as a condition remains true. Loops let the work be written once and repeated any number of times, even when the count is known only while the program runs, which saves code and avoids errors.
Name the four parts of a loop and state the purpose of each.
The initialisation gives the loop variable its starting value; the test condition decides whether the body runs again; the body holds the statements to be repeated; the update expression changes the loop variable so that the condition eventually becomes false.
Distinguish between an entry-controlled loop and an exit-controlled loop, with one example of each.
An entry-controlled loop checks the condition before the body, so the body may run zero times (`for`, `while`). An exit-controlled loop checks it after the body, so the body runs at least once (`do-while`).
How many times does `for (int p = 5; p <= 25; p += 5)` run? What is the value of `p` that ends it?
It runs 5 times, for `p` = 5, 10, 15, 20 and 25. It ends when `p` becomes 30.
Give the output: `int n = 1; while (n < 50) n = n * 3; System.out.println(n);`
`81`. The value of `n` goes 1, 3, 9, 27, 81, and the loop stops when `81 < 50` is false.
Give the output and the number of iterations: `int a = 10; do { System.out.print(a + " "); a = a + 5; } while (a < 10);`
The output is `10 ` and the loop runs once. The body runs before the condition `15 < 10` is checked and found false.
What is an infinite loop? Give two different reasons why a loop can become infinite by mistake.
An infinite loop is a loop whose condition never becomes false, so it repeats endlessly. It can happen when the update statement is missing, or when the update moves the variable away from making the condition false, as in `for (i = 1; i <= 5; i--)`.
What is a delay loop? Write one.
A delay loop is a loop with an empty body that runs only to make the program wait before the next statement, for example `for (long t = 1; t <= 100000000L; t++);`.

Past board exam questions

ICSE 2027 sample paper · 15 marks

(i) Define a class “sumseries” to calculate and display the sum of the series: S = a2 + a4 + a6 + a8 ldotsldots an (ii) Name the jump statements in Java.

Show answer
(i) ```java import java.util.Scanner; class sumseries { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter the value of a: "); int a = sc.nextInt(); System.out.print("Enter the value of n: "); int n = sc.nextInt(); double s = 0; for (int i = 2; i <= n; i += 2) { s = s + Math.pow(a, i); } System.out.println("Sum of the series = " + s); } } ``` Variable description: a – int, base of each term; n – int, power of the last term; i – int, loop variable giving the even powers 2, 4, 6, … n; s – double, sum of the series. (ii) The jump …

ICSE 2027 sample paper · 15 marks

(i) Define a class “pattern” to print the following format using nested for loop only. ``` 1*2*3*4*5* 1*2*3*4*5* 1*2*3*4*5* 1*2*3*4*5* ``` (ii) Name three iteration statements in Java.

Show answer
(i) ```java class pattern { public static void main(String args[]) { int r, c; for (r = 1; r <= 4; r++) { for (c = 1; c <= 5; c++) { System.out.print(c + "*"); } System.out.println(); } } } ``` Variable description: r – int, outer loop counter for the 4 rows; c – int, inner loop counter printing 1 to 5, each followed by *. (ii) The iteration statements in Java are for, while and do-while.

ICSE 2027 sample paper · 1 mark

Which of the following does NOT execute even once?

  1. for(k=10;k<=1;k++)
  2. for(k=100;k>=10;k- =10)
  3. for(k=1;k<=10;k++)
  4. for(k=10;k<=10;k++)
Show answer
for(k=10;k<=1;k++)

ICSE 2027 sample paper · 2 marks

Give the output of the following program segment: ```java for(k=7;k<13;k+=6) System.out.println(k*2); System.out.println(k*3); ```

Show answer
Only the first println is inside the loop. k = 7: prints 14; k becomes 13 and the loop ends; then k*3 = 39 is printed. Output: 14 39

ICSE 2027 sample paper · 2 marks

The following program prints the sum of factors of a given number. Fill in the blanks with appropriate Java statements: Example: if number is 6, then output: 12 [ 1 + 2 + 3 + 6] ```java void sumfact(int n) { int i, ________; (1) for(i=1;i<=n;i++) { if(_______) (2) s=s+i; } System.out.println("Sum of factors = "+s); } ```

Show answer
(1) s=0 (2) n%i==0

ICSE 2027 sample paper · 2 marks

How many times is the following loop executed? Write the output of the same. ```java for(k=15;k>5;k-=5); System.out.println(k); ```

Show answer
The loop has an empty body (semicolon after the for). It runs for k = 15 and k = 10, i.e. 2 times; when k = 5 the condition k>5 is false. The loop is executed twice. Output: 5