Looping / Iterative statements in Java
ICSE Class 9 · Computer Applications · Chapter 9
What this chapter covers
- The need for loops
- Types of looping statements
- The for loop
- The while loop
- The do-while loop
- Comparing the three loops
- Variations in looping statements
- Inter-conversion of loops
- Jump statements: break
- Jump statements: continue
Key points
- Iteration is the repeated execution of statements while a condition stays true; loops make programs short and able to handle any count.
- Every loop has an initialisation, a test condition, a body and an update expression.
- `for` and `while` are entry-controlled loops: the body may run zero times.
- `do-while` is the only exit-controlled loop: the body runs at least once, and a semicolon follows its condition.
- Use `for` for a known number of iterations, `while` when the data decides when to stop, and `do-while` when the task must happen at least once, as with input validation and menus.
- A finite loop ends; an infinite loop's condition never becomes false. `for( ; ; )` and `while (true)` are intentional infinite loops that need a `break`.
- A delay loop has an empty body and only makes the program wait; its delay depends on the computer's speed.
- A `for` loop can initialise and update several counter variables separated by commas, but it has only one test condition.
- Any loop can be converted into the other two forms by moving its four parts; a `do-while` behaves differently when the condition is false at the start.
- `break` ends the loop at once; `continue` skips the rest of the current iteration only.
- Dry runs with trace tables give the output and the number of iterations of a loop reliably.
Questions answered
Define iteration. Why are loops needed in a program?
Name the four parts of a loop and state the purpose of each.
Distinguish between an entry-controlled loop and an exit-controlled loop, with one example of each.
How many times does `for (int p = 5; p <= 25; p += 5)` run? What is the value of `p` that ends it?
Give the output: `int n = 1; while (n < 50) n = n * 3; System.out.println(n);`
Give the output and the number of iterations: `int a = 10; do { System.out.print(a + " "); a = a + 5; } while (a < 10);`
What is an infinite loop? Give two different reasons why a loop can become infinite by mistake.
What is a delay loop? Write one.
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
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
ICSE 2027 sample paper · 1 mark
Which of the following does NOT execute even once?
- for(k=10;k<=1;k++)
- for(k=100;k>=10;k- =10)
- for(k=1;k<=10;k++)
- for(k=10;k<=10;k++)
Show answer
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
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
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); ```