Loop Notes

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Chapter 8

ITERATIVE CONSTRUCTS IN JAVA

II. Answer the following questions:

1. What do you understand by iterative process? How can it be resolved by using loop?

Ans. Iterative process is a process in which a set of instructions are performed repeatedly till
the condition is satisfied. The programming constructs used for this purpose are known as
loops. Once the control enters the loop, it executes the statements within the loop repeatedly
till the condition becomes false. When the condition becomes false, the control skips and
moves to execute the rest of the program.

2. Explain for loop with an example.

Ans. A „for‟ loop usually has a loop variable called the control variable. A „for‟ loop has three
parts. They are as follows:

For example, consider the for loop shown below:

for(int p=1; p<4; p++)


{
System.out.print(“Test message”);
System.out.print(p);
System.out.println();
}

Working of the loop:


 The above loop is executed with an initial value p=1 and „Test message1‟is printed.
 Then the body of the loop is executed again when p=2 and „Test message2‟ is printed.
 Later the loop is executed once again when p=3 and „Test message3‟ is printed.
 Finally, when p=4, the condition becomes false and the control breaks out of the loop. [That is,
the body of the loop is not executed for p=4.]

3. Name the different types of loop statements.

The different type of loop statements used in Java are :

 for loop
 while loop
 do while loop

4. What are the parameters needed to create a for loop?

Ans. 1. An initial value for the loop variable.

2. A condition which results in a boolean value.

3. An update expression to modify the loop variable after each iteration.


5. Define the following with their constructs:

a) Entry controlled loop

In this looping structure, a condition is checked at the beginning of the loop module. If the
given condition is true then the control will enter the loop for execution. The process is
repeated under the condition is false. Since the testing of the condition takes place at the entry
point of the loop, it is termed as „Entry Controlled Loop‟. Example : for loop, while loop.

Construct of while loop

while(<condition>)

{
//statements in the body of the loop
}

Construct of for loop

for(initial value; test condition; update)

{ }

b) Exit controlled loop

In this structure, the condition is tested at the end of the looping construct. If the condition is
true, then the control is sent back to the beginning of the loop for the next iteration. As soon as
the condition is false, the control exits the loop. Since the condition is tested at the exit point of
the loop, it is said to be an „Exit Controlled Loop‟. In this type of loop, even if the condition is
false for the first time, the control will enter the loop at least once. Example: do-while loop.

Construct of do-while loop

do

{
//statements in the body of the loop
}

while(condition);

6. Write down the general format of:

a) for loop b) do-while c) while loop

Ans.

Looping Construct General Format


for loop for(initial value; test condition; update) { }
do while do { } while (<condition>);

while loop while (<condition>) { }


7. What is the purpose of using
a) break statement
b) continue statement in a program

break
 The break statement is used for unusual termination of a block.
 This means that as soon as the break statement is executed, the control will come out of the
block by ignoring the rest of the statements of the block and the remaining iterations of the
loop.
 Sometimes the desired result may be obtained even before the loop has finished all the
iterations. In such a situation, we can use break to terminate the loop.
 The break statement is also used in a switch-case block to terminate a case and to avoid „fall
through‟.

Example for (i=1; i<=10; i++)


{
if(i= =5)
break;
System.out.println( );
}

The above snippet prints the numbers from 1 to 4. When i=5 the break statement is executed
and hence the control comes out of the loop.

continue
When the continue statement is executed, it forces the control to skip the rest of the
statements in the current iteration and continue with the next iteration of the loop.

Example: for(i=1; i<=10; i++)


{
if(i= =5)
continue;
System.out.println(i);
}

The above snippet prints all the numbers from 1 to 10 except 5 because of the continue
statement.

8. What do you understand by inter-conversion of loops?

Ans. While writing a program, the programmer always has the flexibility to convert one type of
loop to another type of loop based on the programmer‟s convenience and requirement.

9. What are the different ways to inter-convert the loops? Name them.

Ans. The various inter-conversion of loops that can be done are given below:

 for loop to while loop


 for loop to do while loop
 while loop to for loop
 while loop to do while loop
 do while loop to while loop
 do while loop to for loop
10. Define the following:

a) Finite loop b) Delay loop

c) Infinite loop d) Null loop

Ans.

a) Finite loop : When the statements run for a fixed number of times then the iteration is called
as a „infinite loop‟. It can be further categorised as continuous loop and step loop.

Example: int k=1;


while(k<=5)
{
System.out.println(k);
k++;
}

b) Delay loop or Null loop : A loop that does not include any statement to be repeated, is
known as Null loop. Basically, a null loop is used to create a delay in the execution. This can
be done to pause the control (or create a time delay) as needed by the user during
programming. The body of the delay loop does not contain any meaningful operation to
perform.

Example:

for(int j=5 ; j<=500 ;j++)


{ }

c) Infinite loop: A loop which is executed endlessly is known as „Infinite Loop‟. This type of loop
never comes to an end. Such „Infinite Loops‟ are created when there are missing parameters.
It may be a missing condition or it may be a condition which never becomes false.

Example 1:

for (m=1; m<=10; )

{ }

In the above loop, since the update expression is missing, the condition always evaluates to
true and hence results in an „Infinite Loop‟.

Example 2:

while(true)
{ }

In the above loop, the condition is always true. If there is no break statement in the body of the
loop, this loop becomes an infinite loop.

Example 3:

for (int j=5; j<=10; j- -)


{ }
In the above loop, due to an incorrect logic the loop is executed endlessly. In this case,
suppose if the update expression was j++ instead of
j- -, the loop would have terminated after five iterations.

d) Null loop – repeated – refer to Question 10 b (Delay loop or Null loop)

11. Distinguish between:

a) for and while loop

For loop While loop


 The for loop is a finite loop which can be The while loop can be used when the
used when the number of iterations is number of iterations is fixed as well as
fixed. unfixed.

 The initialization of the loop variable and  The initialization and the update
the update expression can be done in the expression cannot be given in the while
„for statement‟ itself. statement. The initialization must be done
before the control enters the loop module
and the update expression must be given
in the body of the loop.

b) while and do-while loop

While loop Do-while loop


It is known as an entry-controlled loop. It is known as the exit-control loop
It checks the condition first and then It checks the condition after executing the
starts executing the body of the loop. body of the loop.
 If the condition is false, the statements Even if the condition is false the
within the body of the loop are not statements in the body of the loop are
executed. executed at least once

12. State one difference and one similarity between while and do-while loop?

Difference:

 A while loop is an entry-controlled loop in which the condition is checked before the control
starts executing the body of the loop. If the condition is false, the statements within the body of
the loop are not executed.
 A do-while loop is an exit-controlled loop in which the condition is checked after executing the
body of the loop. So, even if the condition is false the statements in the body of the loop are
executed at least once.

Similarity:

 Both while and do-while are suitable in situations where numbers of iterations is not known.
13. State one similarity and one difference between while and for loop.

Similarity : Both the „for‟ loop as well as the „while „ loop are entry controlled or entry restricted
loops because the condition is checked before entering the loop. Only when the condition is
true, the loop is executed. Suppose if the condition is false, the control is transferred to the
statement after the looping construct.

Difference :
 The for loop is a finite loop which can be used when the number of iterations are fixed.
 The while loop can be used in both cases i.e. whether the number of iterations are
known or unknown (fixed or unfixed iterations).

14. Give two differences between Step loop and Continuous loop.

Step loop Continuous loop


The loop variable/ control variable is The loop variable/ control variable is
incremented or decremented by a given incremented or decremented only by 1,
value, other than 1, after each iteration. after each iteration.

eg. for(int m=1; m<=10;m=m+2) eg. for(int m=1; m<=10;m++)


{ } { }

________________________________

You might also like