Java's Iteration Statements | Controls Statement | Java Programming | Java For Beginner | while.....(do....while)....for loop.

Java’s iteration statements are for, while, and do-while. These statements create what we
commonly call loops. As you probably know, a loop repeatedly executes the same set of
instructions until a termination condition is met.

While Loop

The while loop is Java’s most fundamental loop statement. It repeats a statement or block
while its controlling expression is true.


 Here is its general form:



while(condition)
{
// body of loop
}



The condition can be any Boolean expression. The body of the loop will be executed as long
as the conditional expression is true. When condition becomes false, control passes to the
next line of code immediately following the loop. The curly braces are unnecessary if only

a single statement is being repeated.


Example 


class While
{
   public static void main(String args[])
   {
      int n = 10;
      while(n > 0)
      {
          System.out.println("Number " + n);
          n--;
      }
   }
}



Output

Number 10
Number 9
Number 8
Number 7
Number 6
Number 5
Number 4
Number 3
Number 2
Number 1


Do-while Loop

As you just saw, if the conditional expression controlling a while loop is initially false,
then the body of the loop will not be executed at all. However, sometimes it is desirable
to execute the body of a loop at least once, even if the conditional expression is false to
begin with.

Fortunately, Java supplies a loop that does just that: the do-while. The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop.

Its general form is


do
{
// body of loop
} while (condition);



Each iteration of the do-while loop first executes the body of the loop and then evaluates
the conditional expression. If this expression is true, the loop will repeat. Otherwise, the loop terminates. As with all of Java’s loops, condition must be a Boolean expression.


Example


class DoWhileLoopExample
{
   public static void main(String args[])
   {
      int i=1;
      do
      {
         System.out.println(i);
         i++;
      }
      while(i<=5);
   }
}



Output

1
2
3
4
5


For Loop

The For Loop is entry - controlled loop that provides a more concise loop control structure .


The General form of for loop is:-


for (initialization ; test-condition;increment)
{
   body of the loop;
}



The execution of for statement is as follow:-


  1. Initialization of the control variables is done first, using assignment statements such as i=1 and count =0.The variable i and count are known as loop-control variables.
  2. The value of the control variable is tested using the test condition. The test condition is a relational expression , such as i<10 that determines when the loop will exit. If the condition is true, the body of the loop is executed; otherwise the loop is terminated and the execution continues with the statement that immediately follows the loop.
  3. When the body of the loop is executed, the control is transferred back to the for statement after evaluating the last statement in the loop. Now, the control variable is incremented using an assignment statement such as i= i+1 and the new value of the control variable is again tested to see whether it satisfies the loop condition . If the loop condition is satisfied , the body of the loop is again executed.

Example 


class fortest
{
   public static void main(String args[])
   {
      int i;
      for (i=0;i<=6;i++)
      {
         System.out.println(i);
      }
   }
}



Output

0
1
2
3
4
5
6


Nesting of for loop


Nesting of loops, that means one for statement within another for statement, is allowed in java.

Let see through example:

Program


class nested_loop
{
   public static void main(String args[])
   {
      int p,q;
      System.out.println("The Right angle triangle of @ is shown below:\n");

      for(p=1;p<5;p++)
      {
         for(q=1;q<=p;q++)
         {
            System.out.println("@");
         }
         System.out.printl(" ");
      }
   }
}


Output

@
@@
@@@
@@@@
@@@@@



Post a Comment

2 Comments