Iterative Constructs
for , while, do..while
Multiple Choice Questions
ICSE | IX
CodeLogical
Predict the Output of the following Programs
Question 1
class dkl
{
public static void main(String args[])
{
int i;
for(i = -1;i<10;i++)
{
[Link](++i);
}
}
}
Output
0
2
4
6
8
10
Explanation
This table shows the changes in the value of i as the for loop iterates:
i Remarks
-1 Initial value
0 1st Iteration — i is -1, ++i makes it 0
2 2nd Iteration — i becomes 1, ++i makes it 2
4 3rd Iteration — i becomes 3, ++i makes it 4
6 4th Iteration — i becomes 5, ++i makes it 6
8 5th Iteration — i becomes 7, ++i makes it 8
10 6th Iteration — i becomes 9, ++i makes it 10
1/9
11 Once i becomes 11, condition is false and loop stops iterating.
Question 2
class dk2
{
public static void main(String args[])
{
int i=2,k=1;
while (++i<6)
k *= i;
[Link](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, the condition is false and the loop stops
iterating.
Notice that [Link](k); is not inside the while loop. As there are no curly braces so
only the statement k *= i; is inside the loop. The statement [Link](k); is outside the
while loop, it is executed once and prints a value of k which is 60 to the console.
Question 3
class dk3
{
public static void main(String args[])
{
int m=2,n=15;
2/9
for(int i=1;i<=5;i++)
{
m++;--n;
[Link]("m="+m);
[Link]("n="+n);
}
}
}
Output
m=3
n=14
m=4
n=13
m=5
n=12
m=6
n=11
m=7
n=10
Explanation
This table shows the change in values of m, n and i as the for loop iterates:
m n i Remarks
2 15 — Initial values
3 14 1 1st Iteration
4 13 2 2nd Iteration
5 12 3 3rd Iteration
6 11 4 4th Iteration
7 10 5 5th Iteration
7 10 6 Once i becomes 6, condition is false and loop stops iterating.
Question 4
Determine how many times the body of the loop will be executed.
class dk4
{
3/9
public static void main(String args[])
{
int x=5,y=50;
while(x<=y)
{
y=y/x;
[Link](y);
}
}
}
Output
10
2
The loop will execute 2 times
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 the condition of the while loop becomes false and it
stops executing.
4/9
Rewrite the following Programs
Question 1
Using do while:
class Test
{
public static void main(String args[])
{
int x,c;
for(x=10,c=20;c>=10;c=c-2)
{
x++;
[Link](x);
}
}
}
Solution
class Test
{
public static void main(String args[])
{
int x=10, c=20;
do {
x++;
[Link](x);
c=c-2;
} while (c>=10);
}
}
Question 2
Using do while:
class Pattern
{
public static void main(String args[])
{
5/9
int i,j;
for(i=5;i>=1;i--)
{
[Link](i);
}
[Link]();
}
}
Solution
class Pattern
{
public static void main(String args[])
{
int i=5,j;
do {
[Link](i);
i--;
} while (i>=1);
[Link]();
}
}
Question 3
Using do while:
class Number
{
public static void main(String args[])
{
int i,n=191,c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
c=c+1;
}
if(c==2)
[Link]("Prime");
else
[Link]("Not Prime");
}
}
6/9
Solution
class Number
{
public static void main(String args[])
{
int i=1,n=191,c=0;
do {
if(n%i==0)
c=c+1;
i++;
} while (i<=n);
if(c==2)
[Link]("Prime");
else
[Link]("Not Prime");
}
}
Question 4
Using while loop:
import [Link].*;
class Sample
{
public static void main(String args[]) throws IOException
{
int n,r;
InputStreamReader read = new InputStreamReader([Link]);
BufferedReader in = new BufferedReader(read);
[Link]("Enter a number");
n=[Link]([Link]());
do
{
r=n%10;
n=n/10;
[Link](r);
}
while(n!=0);
}
}
7/9
Solution
import [Link].*;
class Sample
{
public static void main(String args[]) throws IOException
{
int n,r;
InputStreamReader read = new InputStreamReader([Link]);
BufferedReader in = new BufferedReader(read);
[Link]("Enter a number");
n=[Link]([Link]());
while(n!=0)
{
r=n%10;
n=n/10;
[Link](r);
}
}
}
8/9