0% found this document useful (0 votes)
136 views5 pages

5 Looping

The document contains examples of various loop constructs in Java like while, for, do-while loops. It discusses the differences between entry controlled and exit controlled loops. It also contains code snippets and questions to analyze the number of iterations and output of loops.

Uploaded by

binduann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views5 pages

5 Looping

The document contains examples of various loop constructs in Java like while, for, do-while loops. It discusses the differences between entry controlled and exit controlled loops. It also contains code snippets and questions to analyze the number of iterations and output of loops.

Uploaded by

binduann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Loopings

State one similarity and one difference between while and for loop. 2009
Ans.
A while loop contains only a condition while a for loop contains initialization, condition and
iteration.
State the difference between entry-controlled loop and exit controlled loop.
Ans.
In an entry-controlled loop, the loop condition is checked before executing the body of the loop.
While loop and for loop are the entry-controlled loops in Java.
In exit-controlled loops, the loop condition is checked after executing the body of the loop. do-
while loop is the exit-controlled loop in Java.

What is an infinite loop? Write an infinite loop statement. 2014


Ans:
Infinite loop is an endless loop whose number of iterations are not fixed.eg: for(;;)    
System.out.println("java");

Analyze the following program segment and determine how many times the body of loop will be
executed (show the working). 2009
x = 5; y = 50;
while(x<=y)
{
y=y/x;
System.out.println(y);
}
Iteration 1 : 5 <= 50 - true y = y / x = 50 / 5 = 10 10 will be printed
Iteration 2 : 5 <= 10 - true y = y / x = 10 / 5 = 2 2 will be printed
Iteration 3 : 5 <= 2 - false
The loop will be executed two times.

Convert the following segment into an equivalent do loop. 2009


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

What will be the output of the following code?


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=14Note that there is a semicolon at the end of the loop. So, it is an empty loop and does’t
affect the values of m and n.

Analyse the following program segment and determine how many times the loop will be
executed and what will be the output of the program segment. 2010
int k=1, i=2;
while (++i<6)
k*=i;
System.out.println(k);
Following are the iterations of the loop

3 < 6 —- k = 1 * 3 = 3
4 < 6 —- k = 3 * 4 = 12
5 < 6 —- k = 12 * 5 = 60
6 < 6 —- false
The loop will run three times and output is 60

Analyse the following program segment and determine how many times the loop will be
executed and what will be the output of the program segment ? 2011
int p = 200;
while(true)
{
    if (p<100)
        break;
    p=p-20;
}
System.out.println(p);

Ans.
p values change as follows : 200, 180, 160, 140, 120, 100, 80. So, the loop executes six times
and value of p is 80.

What is the final value of ctr after the iteration process given below, executes? 2013
int ctr=0;
for(int i=1;i&lt;=5;i++)
for(int j=1;j&lt;=5;j+=2)
++ctr;
Ans.
Outer loop runs five times. For each iteration of the outer loop, the inner loop runs 3 times. So,
the statement ++ctr executes 5*3=15 times and so the value of ctr will be 15.
Find the errors in the given program segment and re-write the statements correctly to
assign values to an integer array. 2014
int a = new int(5);
for(int i=0;i<=5;i++)
a[i]=i;
Ans
int a[]=new int[5]; OR int []a=new int[5];
(for(int i=0;i<5;i++) OR for(int i=0;i<=4;i++)

Study the method and answer the given questions. 2014


public 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(number);
}}}
(i)How many times does the loop execute?
(ii)What is the range of possible values stored in the variable number?
Ans
(h) (i) 6 times
(ii) 0 to 9 OR {1,2,3,4,5,6,7,8,9}

Write the output of the following program code : 2015


char ch;
int x=97;
do
{
ch=(char)x;
System.out.print(ch+" ");
if(x%10==0)
break;
++x;
} while(x<=100);

Ans
abcd

Convert the following while loop to the corresponding for loop: 2016
int m = 5, n = 10;
while (n>=1)
{
System.out.println(m*n);
n-- ;
}
Ans
for ( int m = 5 , n = 10; n > = 1; n-- )
{
System.out.println ( m * n ) ;
}

Analyze the given program segment and answer the following questions: 2016
(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;
}
Ans
m=5
10
Loop is executed 3 times
Give the output of the following program segment and also mention the number of
times the loop is executed: 2017

int a,b;
for (a = 6, b = 4; a <= 24; a = a + 6)
{
if (a%b ==0)
break;
}
System.out.println(a);

Ans
Output: 12,
Loop is executed two times.

Convert following do-while loop into for loop. 2017


int i = 1;
int d=5;
do
{
d=d*2;
System.out.println(d);
i++ ;
} while ( i<=5);

Ans
for (int i =1 ,d=5; i<=5 ; i++)
{
d= d*2;
System.out.println(d);
}

Analyze the given program segment and answer the following questions: 2017
for(int i=3;i<=4;i++ )
{
for(int j=2;j<i;j++ )
{
System.out.print("" );
}
System.out.println("WIN" );
}
(i)
How many times does the inner loop execute?
(ii)
Write the output of the program segment.
ans
(i)
(i)
3
(ii)
WIN
WIN

Give the output of the following program segment and also mention how many times
the loop is executed: 2018
int i;
for ( i = 5 ; i > 10; i ++ )
System.out.println( i );
System.out.println( i * 4 );
Ans
Output: 20
Loop is not executed even once

You might also like