Saturday 4 June 2016

Loop Control in java Program

In this blog post,we will learn about various loop types supported by java.Before discussing loop method first understand the concept of loop and what's need in java program.


Loop :
"According to english language loop is a structure, series, or process,the end of which is connected to the beginning.In java programming loop statement allows us to execute a statement or group of statements multiple times.There may be a many situation in programming when you need to execute a block of code several number of times.In general, statements are executed sequentially: The first statement in a function is executed first,followed by the second,and so on."


Fig:Loop Control


















What's the need of loop in the programming

  1. Reduced code complexity.
  2. Reduce time
  3. Perform same task in multitime
  4. Changing flow of control in a program 




Java programming language provides the following types of loop to handle looping requirements.

1-while loop
2-do...while loop
3-for loop
In above loop type the most important loop type is for loop.

1-While loop
A while loop statement in java repeatedly executes a statement as long as a given condition is true.

Syntax:
The syntax of a while loop is:

while(Boolean expression)
{
   //Statements or block of statements
}

Here, statement(s) may be a single statement or a block of statements.The condition may be any expression,and true is any

non zero value.if the boolean_expression result is true, then the actions inside the loop will be executed.This will

continue as long as the expression result is true.When the condition becomes false, program control passes to the next statement outside the while loop.
The below is the sample demo program based on while loop.

public class DemoWhile {

   public static void main(String args[]) {
      int x =7;

      while( x <10 ) {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }
}

O/P

value of x : 7
value of x : 8
value of x : 9


2-do while loop

A do...while loop is similar to a while loop, except that the Boolean expression appears at the end of the loop,so the

statements in the loop execute once before the Boolean is tested.If the Boolean expression is true, the control jumps back

up to do statement, and the statements in the loop execute again. This process repeats until the Boolean expression is false.


The syntax structure of a do...while loop is:

do
{
   //Statements
}while(Boolean expression);

Example:
public class DemoDoWhile {

   public static void main(String args[]){
      int x = 7;

      do{
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }while( x < 10 );
   }
}
This would produce the following result:

value of x : 7
value of x : 8
value of x : 9


3-For loop
A for loop is updated or modified version of while and do ..while loop .Programmers are mostly using this loop when task is to be repeated.The syntax structure of a for loop is given below:


for(Variable initialization; Boolean_expression; Update Variable)
{
   //Statements
}


The above for loop sytax structure shows three fields...initialization ,Boolean expression,update statement.Every fields end with semi colon(;) except last field.

1-The initialization variable is executed first, and only once.This variable field allows you to declare and initialize any loop control variables.

2-The second field in for loop is Boolean expression executed.If it is true,the body of the loop is executed.If it is false, the body of the loop will not be executed and control jumps to the next statement.After the body of the for loop gets executed, the control jumps back up to the update statement. This statement allows you to update any loop control variables.

3-The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats (body of loop, then update step, then Boolean expression). After the Boolean expression is false,the for loop terminates.

Demo Example:

public class DemoForLoop {

   public static void main(String args[]) {

      for(int x = 1; x < 10; x = x+1) {
         System.out.print("value of x : " + x );
         System.out.print("\n");
      }
   }
}

O/P Result:

value of x : 1
value of x : 2
value of x : 3
value of x : 4
value of x : 5
value of x : 6
value of x : 7
value of x : 8
value of x : 9




1 comment:

  1. perfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete