Loop Notes
Loop Notes
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.
Ans. A „for‟ loop usually has a loop variable called the control variable. A „for‟ loop has three
parts. They are as follows:
for loop
while loop
do while 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.
while(<condition>)
{
//statements in the body of the 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.
do
{
//statements in the body of the loop
}
while(condition);
Ans.
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‟.
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.
The above snippet prints all the numbers from 1 to 10 except 5 because of the continue
statement.
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:
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.
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:
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:
{ }
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:
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.
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.
________________________________