Page 175 - CA_Blue( J )_Class10
P. 175
Ans. Output: 0,1,2,3,4,
The loop will execute for 6 times
4. Write the output when the program is executed. Also how many times the loop will execute?
int i=0;
while(i<4)
{
if(i==0)
{
i++;
continue;
}
System.out.print(i + ",");
i++;
}
Ans. 1,2,3,
The loop will execute for 4 times
5. What will be the value of "counter"?
int counter=10;
do
{
counter++;
}while(counter<16);
System.out.println(counter);
Ans. 16
6. Correct the error:
for(int i = 0; i < 10; i++)
{ int i = i+1; }
Ans. for(int i = 0; i < 10; i++)
{ i = i+1; }
Variable "i" was declared twice
7. What type of loop is this?
int c=1;
for(; true; c++)
{
System.out.print(c);
}
Ans. Infinite loop
8. Predict the Output of the following Program:
int i;
for(i = -1;i>-10;--i)
{
i=i-1;
System.out.print(i+ " " );
}
Ans. -2 -4 -6 -8 -10
9. Convert the following loop into do-while loop:
int a, b;
for (a=2, b=18; a<=10; b=b-2)
{
173
Iterative constructs in Java 173

