Wednesday, 16 August 2017

Java Loop Control

Repeats a statement or collection of statements while a given condition is true. It tests the condition before executing the loop body. Execute a series of statements multiple times and reduce the code that manages the loop variable.

    Loop is very general control flow statement in programming languages such as java. We are going to describe the basics of “java loop”. In this post, we will study various ways to use loop in day-to-day programming behavior.

There are four types of loops:

1. For loop
2. For each loop
3. While loop
4. Do..While loop

       1.   For loop

    A for loop is a repetition control structure that permit you to proficiently write a loop that needs to execute a specific number of times. The syntax for for loop is :

Syntax
for(initilization; conditional expression; increment expression)

                        {
                           //repetition code here
                        }


Example
public class for_loop_example
{
     public static void main(String args[])
       {
         for(int iNum=1;iNum<=10;iNum++)
          {
              System.out.println(iNum);
           }
       }
}
Output:
1
2
3
4
5
6
7
8
9
10

2. For each loop

     The for-each loop is used to access each consecutive value in a collection of values. Arrays and Collections. It's generally used to iterate over an array or a Collections class (eg, ArrayList). Iterable<E>. It can also iterate over everything that implements the Iterable<E> interface (must define iterator() method).
                                               
Syntax
for(declaration : expression)
                {
                            //Code Here
                }


Example :
class ForEachExample
     public static void main(String args[])
    { 
         int arr[]={11,12,13,17}; 

         for(int iNum:arr)
        { 
             System.out.println(iNum); 
         } 
     }  
Output:
11
12
13
17


       3.   While loop

    While loop is also a control structure like for loop which permit you to repeat a task for number of times. The only difference between a while loop and for loop is that the for loop repeats the task for a exact number of times but the while loop repeats the task for an unknown number of times. The syntax fro while loop is :

Syntax
while(Boolean_Expression Test)
{
                //Statements
}

Code
public class while_example
{
    public static void main(String args[])
       {
              int iNum=1;
              for(iNum<=10)
              {
                   System.out.println(iNum);
                   iNum++;
              }
       }
}
Output:
1
2
3
4
5
6
7
8
9
10


4. do… while loop

    A do while loop is exactly similar to while loop and the just difference between two is that the do while loop executes the statement at least one time. As it start with do keyword and the boolean expression come out at the end of the loop. the syntax for do while loop is :

Syntax

do
{
                //Statements
}
while(Boolean_Expression Test);

Code
public class do_while_example
{
  public static void main(String args[])
  {
    int iNum = 15;

      do
          {
         System.out.println("Value of iNum : " + iNum );
         iNum++;
          }
           while( x < 15 );
   }
}
Output:
Value of iNum : 50


So,in this post we learned about loop control in java like For loop  For each loop  While loop  Do..While loop.


Next session will be base on decision making in java.

0 comments:

Post a Comment