Java Loop:

  • In some situation you need to execute a block of code several number of times in such a case loops are used.
  • A java loop statement permits us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages.
  • Looping in programming languages is a characteristic which helps the execution of a set of instructions/functions again and again while a few situation evaluates to true.
  • Java provides three methods for executing the loops.
  • while all the approaches offer similar simple capability, they range of their syntax and condition checking time.

Java supports following loops:

  1. while Loop
  2. do…while Loop
  3. for Loop
  4. for each loop
  5. Java Continue Statement
  6. Java Break Statement

 

1)  The while Loop:

  • A while loop is a control shape that allows you to repeat a assignment for sure number of times.
  • while executing, if the condition is actual, then the actions inside the loop might be done.
  • This could retain as long as the condition is true.
  • While loop begins with the checking of situation. If it evaluated to actual, then the java loop body statements are executed in any other case first statement following the loop is achieved.
  • For that reason it is also referred to as access control loop

Syntax:

Example:

Output:

 

2)  The do…while Loop:

  • A do…while loop is much like a while loop, besides that a do…while loop is assured to execute as a minimum one time.
  • The situation appears on the end of the loop, so the statements within the java loop execute as soon as before the condition is examined.
  • If the condition is proper, the flow of control jumps returned as much as do, and the statements in the loop execute once more.
  • This can hold so long as the condition is actual.

Syntax:

Example:

Output:

 

3)  The for Loop:

  • for loop is a repetition control structure that execute a particular number of times.
  • for loop is useful when you recognize how in many instances a task is to be repeated.

Syntax:

Example:

Output:

 

4)  The for each Loop:

  • it is particularly used to traverse array or collection factors.
  • The gain of for-every java loop is that it eliminates the opportunity of bugs and makes the code extra readable.

Syntax:

Example:

 

5)  Java Continue Statement:

  • The Java continue statement is used to continue loop until statement is true.
  • It continues the current float of this system and skips the remaining code at special condition.
  • In case of internal loop, it continues only internal loop.

Syntax:

Example:

Output:

 

6)  Java Break Statement:

  • The Java break is used to break loop or switch statement until statement is true or false.
  • It breaks the current go with the flow of this system at exact condition.
  • In case of internal loop, it breaks only internal loop.

Syntax:

Example:

Output: