The Best Technical Blog Ever

twitterrss

Loop Control in java

If we need to execute a block of code several number of times, java allows looping mechanisms for that situation. Loop allows a block of statements to be executed repetitively. Each execution of loop is called an iteration. It is controlled by a boolean expression. There are three kind of loops in Java. They are,

  1. while Loop
  2. do...while Loop
  3. for Loop
  4. enhanced for loop / foreach - used for arrays

In while loops and for loops, boolean expression is evaluated before each iteration, but in do-while loops, it is evaluated after each iteration. When the boolean expression becomes false the loop is terminated.


The while Loop:

The while loop allows you to repeat a task to a certain number of times. It executes statement(s) until a condition holds true.


Syntax:
while(Boolean_expression)
{
   //Body of the loop
}

If the condition is true, then the body of the loop will be executed. This will continue until condition becomes false. If the condition is false, then the loop body will be skipped and the first statement after the while loop will be executed. If body of loop contain only single statement you can optionally use braces ({..}). It is recommended to use braces always to make your program easily readable and understandable.


Example:
while


The do...while Loop:

The do while loop is guaranteed to execute at least one time. It is similar to while loop.


Syntax:
do
{
   	//body of the loop
}while(Boolean_expression);
				

Here, condition appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested. If the condition is true, then the body of the loop will be executed again. This will continue until condition becomes false. The do-while loop is end with semicolon.


Example:
dowhile


The for Loop:

It is very useful, when you know how many times a task is to be repeated. For loop is used to repeat the iteration for a specific number of times.


Syntax:
for(initialization; Boolean_expression; increment/decrement)
{
   	//body of the loop
}

  • Here, The initialization step allows you to declare and initialize any loop control variables.
  • If the evaluated Boolean expression is true, the body of the loop is executed.
  • Each iteration increments/decrements the loop control variable. This process repeats until the condition becomes false. If the condition is false, the body of the loop is terminated and flow of control jumps to the next statement followed by the for loop.

Example:
for


Foreach/ Enhanced for loop in Java:

This is another version of for loop. here also, we use for keyword. Foreach loop is used, when we want to iterate on the elements inside an array.


Syntax:
for(declaration : expression)
{
   	//body of the loop
}

  • Declaration part has the newly declared index variable, which is of a type compatible with the elements of the array you are accessing. The value of this variable is same as the current array element. It will be available within the for block.
  • Expression is an array variable that evaluates the array you need to loop through.

Example:
foreach


Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status