Iterative Construct in java class IX Srinivas
Ch8: Iterative Constructs in Java
Loop:
A group of statements executed repeatedly until a specified condition is met.
The loops are broadly categorized in two ways:
i) Entry controlled loop and (ii) Exit controlled loop.
i) Entry controlled loop: In entry-controlled loop it checks the condition first. If the
condition is true the control enters into the body of the loop and execute the statements.
If the condition is false the control goes outside the body of the loop and execute the
statements.
Example for entry-controlled loops is: for loop and while loop.
This loop is further categorized into two types:
(i) Definite loop or Fixed iterative loop ii) Indefinite loop or Unfixed Iterative loop.
(i) Definite loop or Fixed iterative loop:
In definite loop the number of iterations is fixed before writing a program.
Ex: for loop.
(ii) Indefinite loop or Unfixed Iterative loop:
While writing a program the programmer does not know how many times the loop
will iterate. In this loop the number of iterations is not fixed.
Ex: while loop, do-while loop(exit controlled loop).
ii) Exit controlled loop: In exit-controlled loop , The condition is checked at the end of the loop.
In this loop if the condition is false also the body of the loop statements will be executed at
least once.
Example for exit-controlled loop is: do … while loop
2) Syntax and example of for loop.
A) Syntax of for loop:
for(initial value; test condition; increment/decrement)
{
Statement-1;
Statement-2;
………..
Statement-n;
}
Example:
for(i=1; i<=10; i++)
System.out.print(i+” “);
In the above example one statement is enclosed with in the brackets. They will execute by
changing the value of control variable I by one, unless it will exceed 10.
Output: 1 2 3 4 5 6 7 8 9 10
3) Modifications with for loop : Syntax and example
A) Generally for loop consists of three parameters. We can modify the for-loop parameters in
the following ways:
Iterative Construct in java class IX Srinivas
(i) Omitting a parameter:
A for loop can be used by omitting one or more parameters as shown in code snippet.
for(; i<=10; i++)
System.out.println(i);
In the above loop start with garbage initial value and will operate the block unless the
given condition is false.
(ii) including more parameters:
In for loops we can add one or more expressions in the parameter list.
int s=0;
for(a=1;x=1; a<=10; x++,a++)
{
System.out.println(a);
s=s+x;
}
In the above loop initially the variables ‘a’ and ‘x’ are initialized with 1.
The loop block will execute by displaying the value of x and updating ‘s’ with the
value of ‘x’. this loop will execute until the condition a<=10 is false.
(iii) increment more than one(step loop):
In for loops we can increment or decrement more than 1 by using the key variable.
for(i=5; i<=50; i=i+5)
{
System.out.println(i);
}
In the above code the value of ‘i’ ranges from 5 to 50 with an update of key variable I
is 5.
4) Indefinite loop or Unfixed Iterative loop:
A) Before writing a program the user does not how many times the loop will repeated.
This type of structure is called Indefinite loop or unfixed iterative loop.
Ex: while loop and do… while loop.
5) Syntax and example of while loop
A) Syntax:
while(condition)
{
Statement-1;
Statement-2;
…….
Statement n;
Increment/decrement;
}
Example: int r=10; t=1; double p=1000.0, si=0.0;
while(p<=10000.0)
{
si=(p*r*t)/100.0;
p=p+si;
System.out.println(p);
}
Iterative Construct in java class IX Srinivas
6) Define Exit controlled loop? Give example.
A) Exit controlled loop checks the condition after executing its body. If the condition is true,
loop will perform the next iteration. Otherwise program control will move out of the loop.
The loop executes at least once even if the condition is false.
Example: do…while loop.
7) Syntax and example of do…while loop
A) Syntax:
do
{
Statement-1;
Statement-2;
…….
Statement n;
Increment/decrement;
}while(condition);
Example: int r=10; t=1; double p=1000.0, si=0.0;
do
{
si=(p*r*t)/100.0;
p=p+si;
System.out.println(p);
}while(p<=10000.0);
8) Differentiate between selection statement and Iterative statement.
A) Selection statement Iterative statement
1)A single statement or a set of statements 1) A single statement or a set of statements
are either executed of skipped depending executed repeatedly till a condition is true
on the test condition. or false.
2) Execution of selection set of statements 2) Execution of selected set of statements
is maximum once. can be more than once.
3) if, if else, switch case are the examples 3) for, while and do…while are the
of selection statements. examples of iterative statements.
9) Differentiate between entry controlled loop and exit controlled loop.
A) Entry controlled loop Exit controlled loop.
1) In entry-controlled loop it checks the 1) Exit controlled loop checks the
condition first. If the condition is true the condition after executing its body. If the
control enters into the body of the loop condition is true, loop will perform the
and execute the statements. next iteration.
2) If the condition is false loop will not 2) If the condition is false loop will
execute at least one time.. execute at least one time.
3) ex: for and while loops 3) Ex: do.. while loop.
10) State one difference and one similarity between for and while loop.
A) Similarity: Both for and while loops are Entry controlled loops. Means that both loops are
check the condition first, if the condition is true then enter into the body of the loop.
Difference: For loop is a definite loop means number of iterations are fixed before writing
a program. Where as in while loop , it is an indefinite loop, means number of iterations are
not fixed before writing the loop.
Iterative Construct in java class IX Srinivas
11) State one difference and one similarity between while loop and do…while loop.
A) Similarity: Both while and do-while are suitable in situations where numbers of
iterations is not known.
Difference: while is an entry controlled loop whereas do-while is an exit-controlled loop.
12) Differentiate between while loop and do while loop.
A) while loop do… while
1) It is an entry-controlled loop. It checks 1) It is an exit controlled loop. It checks
the condition first. If the condition is true the condition after executing its body. If
the control enters into the body of the loop the condition is true, loop will perform the
and execute the statements. next iteration.
2) If the condition is false loop will not 2) If the condition is false also the loop
execute at least one time. will execute at least one time.
13) Differentiate between for loop and while loop.
A) for loop while loop
1) For loop is a definite loop means 1) In while loop, it is an indefinite loop,
number of iterations are fixed before means number of iterations are not fixed
writing a program. before writing the loop
2) ex: for(int i=1;i<=5; i+=){} 2) int n; while(n>0){ d=n%10; s=s+d;
n=n/10;}
14) What is finite loop? Define two types of finite loop.
A) In any loop the statements are run for a fixed number of times then the iterative construct
is called finite loop.
Finite loop can be categorized into two types.
(i) Continuous loop (ii) Step loop.
15) What is continuous loop? Give an example.
A) In this looping structure the control variable is increased by or decreased by 1 after each
iteration.
The loop is terminated when the given condition is false.
for(int i=1;i<=5; i++)
{
// body of the loop
}
16) What is step loop? Give an example.
A) In this looping structure the control variable is increased by or decreased by a given value.
The loop is terminated when the given condition is false.
for(int i=1;i<=5; i=i+2)
{
// body of the loop
}
17) What is null loop or delay loop? Give an example.
A) A loop does not include any statement to be repeated is called delay loop or null loop.
In null loop the the loop consists no statements.
for(int i=1;i<=5; i=i++)
{
// no statements
}
Iterative Construct in java class IX Srinivas
Note:
Null loop: A semicolon is placed after closing a bracket in a for loop indicates that the
loop does not execute any statement or the body of the loop is not available.
Ex: int x;
for(x=1; x<=3; x++);
System.out.println(x);
Output: 4.
18) What is infinite loop? Give an example.
A) A loop never ends its execution means that the loop will execute infinite times is called
infinite loop or endless loop.
Ex 1: for(i=1; i>=1;i++) ex:2: int n=1;
{ while(n>0)
System.out.println(i); { System.out.println(n);
} n=n+1;
}
19) What are Jumping statements? Name different types of Jumping statements.
A) Jumping statements are used to unconditional transfer of a program control from one
point to elsewhere in the program.
Jumping statements are of three types.
1) break
2) continue
3) return
20) What is the use of break statement? Give an example.
A) break is a key word. The break statement terminates the current loop or switch statement.
The execution then continues from the statement immediately following the
current loop or switch statement.
Ex: for(int i=1; i<=10; i++)
{
if(i==5)
break;
System.out.println(i);
}
In the above code, The loop will execute first four values printed, but when the value i
reaches 5 , break statement is executed and control jumps out of the loop. Remaining 5
execution not done.
Output: 1
2
3
4
21) What is the use of continue statement? Give an example.
A) continue is a key word. continue statement is used to unconditionally jump to the next
iteration of the loop, skipping the remaining statements of the current iteration.
Ex: for(int i=1; i<=10; i++)
{
Iterative Construct in java class IX Srinivas
if(i==5)
continue;
System.out.print(i+”\t “);
}
In the above code, The loop will execute first four values printed, but when the value i
reaches 5 , continue statement is executed and control skips the current iteration and
execute the next iteration until the condition is false.
Output: 1 2 3 4 6 7 8 9 10
22) Differentiate between break and continue.
A) break continue
1) The break statement terminates the 1) continue statement is used to
current loop or switch statement. unconditionally jump to the next iteration
of the loop, skipping the remaining
statements of the current iteration.
2) It is used in switch case and loops. 2) It is only used in loops.
3) Ex: for(int i=1; i<=10; i++) 3) for(int i=1; i<=10; i++)
{ {
if(i==5) if(i==5)
break; continue;
System.out.print(i+” “); System.out.print(i+” ”);
} }
Output: 1 2 3 4 Output: 1 2 3 4 6 7 8 9 10
23) What will be the output of the following code?
int m=2,n=15;
for(int i=1; i<5; i++)
m++; --n;
System.out.println(”m= “+m);
System.out.println(”n= “+n);
A) m=6, n=14
24) 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.
int p=200;
while(true)
{
if(p<100)
break;
p=p-20;
}
System.out.println(p);
A) (i) The program will run for 6 times.
(ii) The out of the code is : 80
25) What do you understand by interconversion of loops?
A) We can convert the repetitive logic written using one type of loop into any of the other two
types. For example for to while loop and while to do while loop. This is termed as inter-
conversion of loops.
Iterative Construct in java class IX Srinivas
26) Rewrite the following program segment using while instead of for statement.
int f=1,i;
for(i=1; i<=5; i++)
{
f *=i;
}
System.out.println(f);
A) int f=1,i;
while( i<=5)
{
f *=i;
i++;
}
System.out.println(f) ;
27) Write the output of the following program code.
char ch; int x=97;
do{
ch=(char)x;
System.out.println(ch+” “);
if(x%10==0)
break;
++x;
}
while(x<=100)
A) The out of the above code:
a
b
c
d
28) Rewrite the following code snippet by using while loop and give the output.
int p=0;
for(i=1; i<=5; i++)
{
System.out.print(i+” “);
p=p+1;
}
System.out.println(p);
A) code snippet by using while loop
int p=0;
while(i<=5)
{
System.out.print(i+” “);
p=p+1;
i=i+1;
}
System.out.println(p);
The output: 1 2 3 4 5
Iterative Construct in java class IX Srinivas
29) Rewrite the following code snippet by }
using for loop. }}
int i=100; Output:
while(i>0) m=3
{ n=14
System.out.print(i); m=4
i=i-2; n=13
} m=5
A) for(i=100; i>0; i=i-2) n=12
{ m=6
System.out.print(i); n=11
} m=7
30) Predict the output of the following n=10
program segments: d) class dk4{
a) class dk1{ public static void main(String args[])
public static void main(String args[]) {
{ int x=5,y=50;
int i; while(x<=y)
for(i=-1; i<10;i++) {
{ y=y/x;
System.out.println(++i); System.out.println(y);
} }
}} }}
A) 0 A) Output: 10
2 2
4 31) Rewrite the following programs:
6 i) Using do while
8 class test{
10 public static void main(String args[])
b) class dk2{ {
public static void main(String args[]) int x,c;
{ for(x=10,c=20; c>=10;c=c-2)
int i=2,k=1; {
while(++i<6) x++;
k*=i; System.out.println(x);
System.out.println(k); }
} }}
} A) class test{
A) output: 60 public static void main(String args[])
c) class dk3{ {
public static void main(String args[]) int x=10,c=20;
{ do {
int m=2,n=15; x++;
for(i=1; i<=5;i++) System.out.println(x);
{ c=c-2;
m++; --n; }while(c>=10);
System.out.println(m=”+m); }}
System.out.println(n=”+n);
Iterative Construct in java class IX Srinivas
ii) Using do..while i++;
class pattern{ }
public static void main(String args[]) if(c==2)
{ System.out.println(“Prime” );
int i,j; else
for(i=5; i>=1; i--) System.out.println(“Not Prime” );
{ }}
System.out.print(i); iv) Using while loop:
} import java.io.*;
System.out.println( ); class sample{
}} public static void main(String args[])
A) class pattern{ throws IOException
public static void main(String args[]) {
{ int n,r;
int i,j; InputStreamReader read=new
do InputStreamReader(System.in);
{ BufferedReader in= new
System.out.print(i); BufferedReader(read);
i--; System.out.println(“Enter a number” );
} while(i>=1); n=Integer.parseInt(in.readLine());
System.out.println( ); do{
}} r=n%10;
iii) using do..while n=n.10;
class Number{ System.out.println(r );
public static void main(String args[]) } while(n!=0);
{ }
int i,n=191,c=0; }
for(i=1; i<=n; i++) A) import java.io.*;
{ class sample{
if(n%i==0) public static void main(String args[])
{ throws IOException
c=c+1; {
} int n,r;
if(c==2) InputStreamReader read=new
System.out.println(“Prime” ); InputStreamReader(System.in);
else BufferedReader in= new
System.out.println(“Not Prime” ); BufferedReader(read);
}} System.out.println(“Enter a number” );
Ans) class Number{ n=Integer.parseInt(in.readLine());
public static void main(String args[]) while(n!=0){
{ r=n%10;
int i,n=191,c=0; n=n.10;
do System.out.println(r );
{ }
if(n%i==0) }
c=c+1; }