Tuesday 24 January 2017

Control Statements in Java ( Iteration Statements )

                                               
Control Statements in Java ( Iteration Statements )


In java , the iteration statements are for , while and do-while. These statements are used to create Loops. A Loop repeatedly executes the same set of instructions until a termination condition is met.

While

The while loop is the fundamental looping statement in java.It repeats a statement or block while  its controlling statement is true.

Its general form is:

    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 the condition becomes false , control passes to the next line of code immediately following the loop.

program:

// To demonstrate while loop

  class WhileDemo
{
     public static void main ( String [] args )
   {
          int n=10;                                                             output:
                                                                                       tick 10
          while ( n>0 )                                                        tick 9
        {                                                                             tick 8
               System.out.println( "tick" + n) ;                     tick 7
               n--;                                                                  tick 6
         }                                                                            tick 5
       }                                                                              tick 4
 }                                                                                    tick 3   
                                                                                       tick 2
                                                                                       tick 1

do-While

The do-while loop always executes its body at least once , because its conditional expression is at the bottom of the loop. It's 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 the expression is true , then the loop will  repeat , otherwise the loop will terminate.

Program:

    // To demonstrate the do-while loop
     class DoWhiledemo
     {
           public static void main ( string [] args )
          {
               int n=10 ;
              do
            {
                     System.out.println (" tick " + n);
                    n--;
            } while ( n>0 ) ;
          }
      }

For

For loop is a powerful and versatile conduct. The general form of for loop is:

                     for ( initialization ; condition ; iteration )
            { 
                 //body of loop
             }

The for loop works as follow : when the loop first starts , the initializaion portion of the loop is executed. This is the expression that sets the value of the loop control variable  , which act as a counter that controls the loop.

The initialization expression is needed only once. Next , condition is evaluated . It is boolean expression and tests the loop control variable against a target value. If it is true , then the body of the loop is executed. If it is false , then the lop terminates.

Next, the Iteration portion of the loop is executed.This is usually an expression that  increments or decrements the loop control variable. This process repeats until the controlling expression is false. 

Program:

  // To demonstrate the for loop
     class  ForDemo
 {
       public static void main ( String []  args )
     {
            int n;

                for ( n=10 ; n>0 ; n--)
          System.out.println ( "tick " + n);
     }


We can also declare the control loop variables inside the for loop. So the previous program for loop expression can be written as  for ( int n= 10 ; n>0 ; n--). we DO NOT NEED TO DECLARE n in the starting of program.

Using COMMA ( , )

Sometimes we want to include more than 1 statement in the intialization and iteration portions of the for loop.

Program:

  // To demonstrate the use of comma
    class ComaDemo
{
                public static void main ( String [] args )
          {
                 int a ,b ;

       for ( a=1 , b=4 ; a<b ; a++ , b-- )              Output:
   {                                                                    a=1
         System.out.println ( "a=" + a);               b=4
        System.out.println ( "b=" + b );               a=2
    }                                                                   b=3
  }
}

Also we can have nested for loops , same as if statements.


                                                                            By: Knowledge Bits



No comments:

Post a Comment

Video of the Day

Contact us

Name

Email *

Message *