Class X - CTA - PDF - 4
Class X - CTA - PDF - 4
CHAPTER 9
ITERATIVE CONSTRUCTS IN JAVA
1
2
3
4
Syntax:
5
6
Working of for Loop in Java
7
8
Working of while Loop in Java
9
10
Working of do-while Loop in Java
11
12
13
14
Break Statement in Java
The break statement terminates the loop containing it. Control of the
program flows to the statement immediately after the body of the loop.
If break statement is inside a nested loop (loop inside another
loop), break will terminate the innermost loop. The most common use
for break is when some external condition is triggered requiring a
hasty exit from a loop. The break statement can be used in
both while and for loops as shown in the following figure.
15
Continue Statement in Java
The continue statement is used to skip the rest of the code inside a
loop for the current iteration only. Loop does not terminate but
continues on with the next iteration. The continue statement can be
used in both while and for loops.
16
17
Fill in the blanks
Question 1
Question 2
For loop is also known as entry controlled loop.
Question 3
Question 4
Question 5
while loop checks the condition first before its execution.
Question 6
To find the sum of any ten numbers, the loop will run ten times.
18
Question 1
What is 'for' loop? What are the parameters used in 'for' loop?
for loop is an entry-controlled loop. The following parameters are
commonly used in a for loop:
Question 2
Define the following with their constructs:
(a) Entry controlled loop
An entry-controlled loop checks the condition at the time of entry.
Only if the condition is true, the program control enters the body of
the loop. for and while loops are entry-controlled loops.
(b) Exit controlled loop
An exit-controlled loop checks the condition after executing its body.
If the condition is true, loop will perform the next iteration otherwise
program control will move out of the loop. do-while loop is an exit-
controlled loop.
19
Question 3
Write down the syntax of:
(a) do - while
do {
//loop-body
} while (condition);
(b) while loop
while (condition) {
//loop-body
}
Question 4
What is the purpose of using
(a) break statement
break statement is used to unconditionally jump out of the loop
(b) continue statement in a program?
continue statement is used to unconditionally jump to the next
iteration of the loop, skipping the remaining statements of the current
iteration.
Question 5
Distinguish between while and do-while loop.
while do-while
20
Question 6
What is meant by an infinite loop? Give an example.
A loop which continues iterating indefinitely and never stops is
termed as infinite loop. Below is an example of infinite loop:
for (;;)
System.out.println("Infinite Loop");
Question 7
State one difference and one similarity between while loop and do-
while loop.
Similarity — Both while and do-while are suitable in situations where
numbers of iterations is not known.
Difference — while is an entry-controlled loop whereas do-while is
an exit-controlled loop
Show the output:
Question 8
Output
x=2
y=2
21
Explanation
As n is 1, so if condition is true. x and y are incremented by 1 so both
become 2.
(ii) 0
Output
x=1
y=1
Explanation
As n is 1, so if condition is false. Values of both x and y remain
unchanged.
Question 9
Analyze the following program segment and determine how many
times the body of the loop will be executed (show the working).
x = 5; y = 50;
while(x<=y)
{
y = y / x;
System.out.println(y);
}
Output
10
2
The loop will execute 2 times.
22
Explanation
x y Remarks
5 50 Initial values
Question 10
What will be the output of the following code?
int m=2;
int n=15;
for(int i=1;i<5;i++)
m++;
--n;
System.out.println("m="+m);
System.out.println("n="+n);
Output
m=6
n=14
Explanation
As there are no curly braces after the for loop so only the
statement m++; is inside the loop. Loop executes 4 times
so m becomes 6. The next statement --n; is outside the loop so it is
executed only once and n becomes 14.
23
Question 11
Output
60
Explanation
This table shows the change in values of i and k as while loop iterates:
i k Remarks
2 1 Initial values
3 3 1st Iteration
4 12 2nd Iteration
5 60 3rd Iteration
24
Question 12
Give the output of the following program segment and also mention
the number of times the loop is executed.
int a,b;
for(a=6;b=4; a <= 4; a=a+ 6)
{
if(a%b==0)
break;
}
System.out.println(a);
Output
6
The loop executes 0 times.
Explanation
a is initialized to 6 and as the loop condition is false before the first
iteration itself so the loop doesn't execute.
Question 13
Give the output of the following program segment and also mention
how many times the loop is executed.
int i;
for(i = 5; i > 10; i++)
System.out.println(i);
System.out.println(i * 4);
Output
20
The loop executes 0 times.
25
Explanation
i is initialized to 5 and as the loop condition is false before the first
iteration itself so the loop doesn't execute. The
statement System.out.println(i * 4); is outside the loop so it gets
executed once, printing 20 to the console.
Question 14
Using for loop:
int i=1;
int d=5;
do
{
d=d*2;
System.out.println(d);
i++;
}
while (i<=5);
Answer
int d=5;
for (int i = 1; i <= 5; i++) {
d=d*2;
26
Questions based on Programs
Question 15
Write the programs in Java to find the sum of the following series:
(a) S = 1 + 1 + 2 + 3 + 5 + ....... to n terms
(b) S = 2 - 4 + 6 - 8 + ....... to n
(c) S = 1 + (1+2) + (1+2+3) + ....... + (1+2+3+ ....... + n)
Question 16
Write the programs to find the sum of the following series:
(a) S = a + a2 + a3 + ....... + an
(b) S = (a+1) + (a+2) + (a+3) + ....... + (a+n)
(c) S = (a/2) + (a/5) + (a/8) + (a/11) + ....... + (a/20)
Question 17
Write a program to input a number. Display the product of the
successors of even digits of the number entered by user.
Input: 2745
Output: 15
[Hint: The even digits are: 2 and 4
The product of successor of even digits is: 3*5= 15]
27
Question 18
Write a program to input a number and check and print whether it is
a Pronic number or not. [Pronic number is the number which is the
product of two consecutive integers.]
Examples:
12 = 3 * 4
20 = 4 * 5
42 = 6 * 7
28