Loops in JAVA

Loops in JAVA

In this tutorial you will learn: 1) Why do we need loops in JAVA? 2) For loop in JAVA 3) While loop in JAVA 4) Do while loop in JAVA 5) Continue and Break Statement in JAVA Why do we need loops? Loops allows you to execute a statement or block of statements repeatedly. For loop is used when the number of iterations are already known. The for loops keep executing until the specific condition is met and when the certain condition is met the execution terminates. For loops Unlike C programming there are 2 type of for loops in JAVA which differ in increment method. Three parts of control of for loop - Initialization, Test, Iteration ( Increment/decrement) Execution starts from the initialization statement that is executed only once. Test condition is evaluated after each statement. If test condition is true, body of for loop gets executed. Iteration expression is evaluated before the test condition is evaluated again. Syntax of for loop type1: This is the ordinary loop just like used in C programming.
  1. for(initial value; condition to be satisfied; increment/decrement)
  2. {
  3. //statements to be executed
  4. }
Example of for loop type1: This loop will start from 0 and keep on executing to j equal to 9. J has to be declared as int. The output of this loop will be numbers from 0 to 9.
  1. for(j=0;j<10;j++)
  2. {
  3. System.out.printIn(j);
  4. }
Syntax for for loop 2: This loop is used for the 1D array, it automatically finds the size of array and keep on executing the statements. Iterations are equal to the size of the array.
  1. public static void listEntries(String[] entries)
  2. //all methods called directly from main must be static
  3. { //enhanced for-loop
  4. for(String entry: entries)
  5. System.out.println(entry);
  6. }
Hence it will print out all the entries of the array. While loop in JAVA While loop executes as long as the test expression between parentheses is true. The statements of the loop may not execute at all in a while construct. Make sure the statements within the body of the while loop eventually result in making the test condition as false
  1. while(condition to be tested)
  2. {
  3. //here we write the statements which are executed until the condition is false;
  4. }
Example:
  1. While(i<10)
  2. {
  3. System.out.printIn(i);
  4. }
Do while loop in JAVA Similar to while loop except that loop block is executed at least once, even if the test condition is false from the start.This is because the test condition is executed at the end of the loop block. Syntax is given as:
  1. do{
  2. //statement to be executed
  3. }
  4. while(condition to be tested);
Continue and Break Statement in JAVA Continue is used if you want to skip all or part of body of the loop for a loop iteration. Continue stop executing the loop and takes you to the next iteration of the loop in which it finds itself. Note that if is not a loop but a decision. Continue statement can appear anywhere in the body of the loop. You can have more than one continue in the body of a loop. Labeled break Statement in case of nested loops, it enables you to stop executing the inner loop and jump to the statement following the end of any enclosing statement block or loop that is identified by the label in the labeled statement It happens regardless of how many levels there are. Used to break out of a deeply nested set of loops in an easy manner. The continue and break statements enables us easy reading and debugging.

Add new comment