Ads

Iterative Constructs with Nested Loop

A construct in which a block of statements gets executed repeatedly unless the required task has been completed. Such repetitive construct is known as an iterative construct or a Loop.

Iterative means repeated execution of a set of statements. This can be achieved by using a loop.

Based on the flow of control, the looping constructs can be categorized into two types. They are:

  • Entry Controlled Loop
  • Exit Controlled Loop


Entry Controlled Loop

A looping construct in which the condition is checked in the beginning. If the condition is true, the control is allowed to enter into the loop otherwise the entry will be denied. There are two types of looping construct under this category:

for Loop

The for loop is used when number of iterations is fixed and known. It is a fixed or known iterative
looping construct.
Components of for loop structure are:
  1. control variable with initial value
  2. test condition
  3. iteration (increment / decrement step value)
syntax:
for(initialization; test condition; iteration)
{
// loop body
}

e.g.,
for(a=1; a<=10; a++)
{
System.out.prinlnt(a);
s=s+a;
}

while Loop

Sometimes, it may happen that a set of statements are to be executed repeatedly without knowing the number of iterations, while loop is used to solve the purpose. In this looping construct, a block of statements gets executed repeatedly unless the given condition is false. while loop is unfixed or unknown iterative loop.
syntax:
initialization;
while(test condition)
{
//statements
iteration;
}

e.g.,
int p=1000, r=10;
while(p<=10000)
{
si = (p * r) / 1000.0;
p = p + si;
}


Exit Controlled Loop

This is another type of looping construct in which condition is checked at the exit point (end point) of the loop. The control will repeat for the next iteration or exit from the loop depending on the condition is true or false receptively. Here, the body of the loop will execute at least once.

do-while Loop

The do-while loop also works in the same way as while loop. It is also unfixed or unknown iterative loop. Only difference is the position at which the condition will be checked. In do-while loop the condition is checked at the end of the loop.
syntax
initialization;
do
{
//statement
iteration;
}while(test condition);

e.g.,
int p=1000, r=10;
do
{
si = (p * r) / 1000.0;
p = p + si;
}while(p<=10000);


Use of 'break' statement in loop

Sometimes, it may happen that the loop has not been completed but the requirement has been fulfilled. The user then would like to terminate the loop without its completion. In this situation, break statement is used to solve the purpose. Hence, the break statement is used in a loop to send the control out of the loop, if certain condition is true.
eg.,
for(i=1; i<=10; i++)
{
      if(i == 5)
           break;
      s = s + i;
}

In the above example, the loop will add the numbers from 1 to 4. As soon as the value of 'i' becomes 5, the break statement will be executed. As a result control will exit from the loop.


Use of 'continue' statement in loop

The continue statement is opposite of the break statement. When continue statement is invoked, the control goes back to check the condition of the loop by ignoring rest statements of the loop.

eg.,
for(i=1; i<=10; i++)
{
      if(i == 5)
           continue;
      s = s + i;
}

In the above example, the loop will run from 1 to 10 times, but as soon as, the value of 'i' becomes 5, the continue statement will be executed. As a result, the control will be forced to move back for the next iteration by ignoring the last statement of the loops.

Infinite loop

A loop that repeats for infinite number of iterations is known as infinite loop. It is also called endless loop. Here, the test condition always return true value.
eg.,
for(;;)
//statements

Nested loop

When you use a loop within another loop is said to be a nested loop. In the nested loop, you will notice that the inner loop repeats a number of times for each repetition of the outer loop.
e.g.,
int i, j;
for(i=1; i<=5; i++)      // outer loop
{
      for(j=1; j<=i; j++)     // inner loop
      {
            System.out.print(j);
       }
       System.out.println();
}

OUTPUT
1
12
123
1234
12345
SHARE

Author

Hi, Its me Hafeez. A webdesigner, blogspot developer and UI/UX Designer. I am a certified Themeforest top Author and Front-End Developer. I'am business speaker, marketer, Blogger and Javascript Programmer.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 Comments:

Post a Comment