Page 232 - CA_Blue( J )_Class9
P. 232
3. What will be the output? i Output
int i;
20 8
for( i=20;i>=10;i-=2);
18
System.out.println(i);
16
Ans. Output : 8 14
[Explanation : After the for loop, the ";” acts as a terminator. So, the loop will execute and 12
when the condition is false i.e. i=8, the loop will stop and the value of i will be printed.] 10
4. Write the output when the program is executed. Also how many times the loop will 8>=10 false
execute?
int i;
for( i=20;i>=5;i=i-5)
{ if (i%3 == 0)
break;
else
continue; }
System.out.println(i);
Ans. Output : 15
The loop will execute for 2 times
5. Convert the for loop to a while loop.
int m;
for( m=3;m<=10;m=m+2)
{ if(m==6)
{ System.out.println(m); }
}
Ans. int m=3;
while(m<=10)
{ if(m==6)
{ System.out.println(m);
}
m=m+2;
}
6. What type of loop is this?
for(int i=1; i<=10; i++)
{ }
Ans. null loop
7. Find the error and correct it:
int i=1 i Output
while(i<=5) -1
-2 -2 -4 -6 -8 -10
{ i=+2; }
-3
Ans. int i=1;
-4
while(i<=5)
-5
{ i+=2; }
-6
8. Predict the Output of the following Program: -7
int i; -8
for(i = -1;i>-10;--i) -9
{ -10
-11>-10 x
i=i-1;
230 Touchpad Computer Applications-IX

