Page 189 - CA_Blue( J )_Class10
P. 189
i++;
}while(i <= 5); [2017]
Ans. int i, d=5;
for(i = 1; i <= 5; i++)
{
d = d * 2;
System.out.println(d);
}
10. Convert the following while loop to the corresponding for loop:
int m = 5, n = 10;
while(n >= 1)
{
System.out.println(m * n);
n--;
} [2016]
Ans. int m = 5, n = 10;
for(; n >= 1; n--)
System.out.println(m * n);
11. Analyze the given program segment and answer the following questions:
(i) Write the output of the program segment.
(ii) How many times does the body of the loop gets executed?
for(int m = 5; m <= 20; m += 5){
if(m % 3 == 0)
break;
else
if(m % 5 == 0)
System.out.println(m);
continue;
} [2016]
Ans. (i) 5
10
(ii) The body of the loop executes 3 times.
12. Write the output of the following program code:
char ch;
int x = 97;
do{
ch = (char)x;
System.out.print(ch + " ");
if(x % 10 == 0)
break;
++x;
}while(x <= 100); [2015]
Ans. a b c d
13. What is an infinite loop? Write an infinite loop statement. [2014]
Ans. A loop that executes an infinite number of times as the test condition is always true. It can be created by any type of loop.
class infinite_for {
public static void main() {
for(int i=1; i>=1; i++) {
System.out.println(i);
}
}
}
187
Iterative constructs in Java 187

