Iterative Construct

Section A

2006

Question 2
(a) Explain the term for loop with an example. [2]

The for loop is a fixed number iteration statement. It is used to execute a set of statements repeatedly. The following loop starts from 1 and prints up to 10. When the value of i becomes 11 the loop terminates.

Example:
for(int i=1;i<=10;i++)
{
System.out.println(i);
}

(d) State one similarity and one difference between while and do while loop. [2]

Similarity: The while and do while loops are non-fixed number iteration statements.
Difference: The while is an entry controlled loop, that means before entering to the body of the loop a test expression is done and if it is true then only the body executes. But the do while is an exit controlled loop that means the test expression is done only after executing the loop body once.

Question 3
(c) Explain the meaning of break and continue statements. [3]

The break is a statement that enables the execution to jump from a loop or body of switch that causes termination of loop or switch. In the following example the loop terminates when i becomes 5. The output will be 1 2 3 4
for(i=1;;i++)
{
  if(i==5)
    break;
  System.out.print(i+“ ”);
}
			
The continue is a statement that enables to skip the statements after the continue statement and to jump to the next updation. In the following example it prints odd numbers from 1 to 9. When the condition become true it skips the print statement and jumps to next iteration (updation).
for(i=1; i<=10; i++)
{
  if(i%2==0)
    continue;
  System.out.println(i);
}
			

2008

Question 2
(a) What is meant by an infinite loop? Give an example. [2]

A loop that never ends is an infinite loop. If there is no test expression or the test expression is always true then the loop will be infinite loop. E.g.:
for(int i=1;i>0;)
System.out.println(i);

Question 3
(b) Explain the function of each of the following with an example:
(i) break; (ii) continue;
See answer in the year 2005. [4]

See answer in the year 2005.

(b) Convert the following segment into equivalent for loop
{
int i; i=0;
while (i<=20)
System.out.print( i+" ");
i++;
} [2]

{
int i;
for(i=0;i<=20;)
System.out.print(i+" ");
i++;
}
Note to students: In question there is no { } for the while loop body. So the i++ is outside of the loop.
In answer also it should be outside of for loop.

2009

Question 1
(b) State one similarity and one difference between while and for loop. [2]

Similarity:
The while and for loops are entry controlled loops i.e. the test condition is done before entering to the loop. If the condition is true then only the body of loop executes.
Difference:
1) The for is fixed number execution loop; the while loop is more convenient when the number of execution is not known in prior.

Question 2
(b) 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);
} [2]

Answer:
Loop body executes two times
Output is:  
10
2
Working
Before loop x=5, y=50
Is x<=y?   y=y/x    print y
5<=50 yes  10=50/5  10
5<=10 yes  2=10/5   2
5<=2  No. Loop ends
			

(g) Convert the following segment into an equivalent do loop. [3]
int x,c;
for(x=10,c=20;c>=10;c=c-2)
x++;

int x,c;
x=10; c=20;
do
{
x++;
c=c-2;
}
while(c>=10);

2010

Question 1
(c) Name two jump statements and their use. [2]

See the answer in 2005.

Question 3
(b) (i) What will be the output of the following code? [3]
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);

m=3
n=14

(c) Analyze the following program segment and determine how many times the loop will be executed and what will be the output of the program segment. [2]
int k=1,i=2;
while(++i<6)
k*=i;
System.out.println(k);

Loop body executes three times.
Output: 60

2011

Question 1
(c) State the difference between entry controlled and exit controlled loops. [2]

Entry controlled loops checks the condition before entering to the loop. The for and while are entry controlled loops. If the condition is true then the loop body executes, otherwise loop stops.
Exit controlled loop execute at least once. It checks the condition before exiting from the loop. The do while is the exit controlled loop. If the condition is true then the loop body executes, otherwise loop stops.

(a) Analyze the segment and find how many times the loop will be executed and what will be the output?
int p=200;
while(true)
{
if(p<100)
break;
p=p-20;
}
System.out.println(p); [2]

The loop executes 7 times.
Output: 80

2012

Question 1
(i) Rewrite the following program segment using while instead of for.
int f=1,i;
for(i=1;i<=5;i++)
{
f*=i;
System.out.println(f);
} [2]

int f=1,i;
i=1;
while(i<=5)
{
f*=i;
System.out.println(f);
i++;
}

2013

Question 2
(d) How many times will the following loop execute? What value will be returned?
int x=2, y=50;
do {
++x;
y-=x++;
} while(x<=10);
return y; [3]

5 times
15 returns

(h) What is the final value of ctr when the iteration process given below, executes?
int ctr=0;
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j+=2)
++ctr; [2]

The final value of ctr : 15

2014

Question 2
(d) What is an infinite loop? Write an infinite loop statement. [2]

The loop that never ends is an infinite loop. If the loop condition is always true then it will be an infinite loop.
E.g.: for(int i=1;i<=10;)
System.out.println(i);
There is no updation, so the i (1) is aways less than 10.

Question 3
(h) Study the method and answer the given questions.
public static void sampleMethod()
{ for(int i=0;i<3;i++)
{ for(int j=0;j<2;j++)
{ int number=(int)(Math.random()*10);
System.out.println(""); }}}
(i) How many times the loop execute?
(ii) What is the range of possible values stored in the variable number? [2]

(i) 6 times.
(ii) 0 to 9

2015

Question 3
(h) Write the output of the folllowing program code:
char ch;
int x=97;
do
{
ch=(char)x;
System.out.println(ch+" ");
if(x%10==0)
break;
++x;
} while(x<=100); [2]

a
b
c
d

2016

Question 3
(f) Convert the following while loop to the corresponding for loop:
int m=5,n=10;
while(n>=1)
{
System.out.println(m*n);
n--;
} [2]

for(intm=5,n=10;n>=1;n--)
{
System.out.println(m*n);
}

(h) 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;
} [2]

(i) 5
10
(ii) 3 times

2018

Question 2
(b) State the difference between while and do while loops. [2]

The while is an entry controlled loop that means the condition is checks before entering to the loop body. If the condition is true only the loop body executes other wise loop terminates.
The do while is an exit controlled loop that means the condition is checks only before exiting the loop body. If the condition is true then the loop body executes other wise loop terminates. The loop body executes atleast once.

Question 3
(a) State the difference between while and do while loops. [2]

The while is an entry controlled loop that means the condition is checks before entering to the loop body. If the condition is true only the loop body executes other wise loop terminates.
The do while is an exit controlled loop that means the condition is checks only before exiting the loop body. If the condition is true then the loop body executes other wise loop terminates. The loop body executes atleast once.

2019

Question 3
(d) Give output of the following and mention how may times the loop will execute.
int i;
for(i=5;i>=1;i--)
{
if(i%2==1)
continue;
System.out.print(i+" ");
} [2]

Output: 4
2
Loop executes 5 times

(g) 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); [2]

20
Loop body does not execute.

2020

Question 2
(e) What is an infinite loop? Give an example. [2]

An infinite loop is one that never ends its execution.
E.g.: for(i=1;i>5;i++) - it never ends because the condition is always true.

Question 3
(e) Give the output of the following program segment and mention how many times the loop will execute:
int k;
for(k=5 ; k<=20 ; k+=7)
if(k%6==0)
continue;
System.out.println(k); [2]

19
Two times.