0% found this document useful (0 votes)
73 views28 pages

Class X - CTA - PDF - 4

The document provides information about iterative constructs in Java including for, while, and do-while loops. It explains the syntax and working of each loop type. It also discusses the break and continue statements in Java and how they can be used to control loop execution. Examples are provided to illustrate loop concepts like infinite loops, nested loops, and using loops to solve problems. Multiple choice and descriptive questions are given at the end to test understanding of Java loops.

Uploaded by

Ruchi Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views28 pages

Class X - CTA - PDF - 4

The document provides information about iterative constructs in Java including for, while, and do-while loops. It explains the syntax and working of each loop type. It also discusses the break and continue statements in Java and how they can be used to control loop execution. Examples are provided to illustrate loop concepts like infinite loops, nested loops, and using loops to solve problems. Multiple choice and descriptive questions are given at the end to test understanding of Java loops.

Uploaded by

Ruchi Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

WORKSHEET -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

When the statements are repeated sequentially a number of times in a


program, the construct is known as loop.

Question 2
For loop is also known as entry controlled loop.

Question 3

do-while loop is called an exit controlled loop.

Question 4

do-while loop executes at least once, if the condition is false.

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.

Answer the following questions:

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:

1. An initial value for the loop control variable.


2. A condition—loop will iterate as long as this condition remains
true.
3. An update expression to modify the loop control variable after
every iteration.
4. Body of the loop which consists of the statements that needs to
be repeatedly executed.

Below is an example of for loop, it prints the table of 2 till 12:


for (int i = 1; i <= 12; i++) {
int a = 2 * i;
System.out.println("2 x " + i + "\t= " + a);
}

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

It is an entry-controlled loop. It is an exit-controlled loop.

It is helpful in situations where It is suitable when we need to


numbers of iterations are not known. display a menu to the user.

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

The following is a segment of a program.


x = 1; y = 1;
if(n>0)
{
x = x + 1;
y = y + 1;
}
What will be the value of x and y, if n assumes a value:
(i) 1

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

5 10 After 1st iteration

5 2 After 2nd iteration

After 2 iterations y becomes less than x so condition of while loop


becomes false and it stops executing.

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

Analyze the following program segment and determine how many


times the loop will be executed. What will be the output of the
program segment?
int k=1,i=2;
while(++i<6)
k*=i;
System.out.println(k);

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

6 60 Once i becomes 6, condition is false and loop stops iterating.

Notice that System.out.println(k); is not inside while loop. As there


are no curly braces so only the statement k *= i; is inside the loop.
The statement System.out.println(k); is outside the while loop, it is
executed once and prints value of k which is 60 to the console.

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

You might also like