Loops

Loops are a type of control structure that allows you to repeat a certain number of statements a certain number of times. Here are the loop types in Java.

The while Loop:

The syntax of a while loop is:

while (boolean_expression){
  //Statements
}

During executing, if the boolean_expression result is true, then the statements inside the loop will be executed. This will continue as long as the boolean expression result is true. When the boolean expression is false, the loop statements will be skipped and the first statement after the while loop will be executed.

Example:

public class WhileLoopDemo {

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

      while( x < 20 ) {
         System.out.println("value of x : " + x );
         x++;
      }
   }
}

Check the result.

What happens if you do not add x++; statement in the above code?

The do...while Loop:

A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

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

do{
  //Statements
} while (boolean_expression);

Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the boolean expression is tested. If the boolean expression is true, the flow of control jumps back up to do, and the statements in the loop execute again. This process repeats until the boolean expression turns to false.

Example:

public class DoWhileLoopDemo {

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

      do{
         System.out.println("value of x : " + x );
         x++;
      }while( x < 20 );
   }
}

Check the results.

What is the difference in the execution between the while and do-while in the above example?

The for Loop:

for loops is similar to while and do while except the for construct not only takes in conditional expression but also an initialization block and update block.

The syntax of a for loop is:

for(initialization; boolean_expression; update){
  //Statements
}

Here is the flow of control in a for loop:

  • The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables. If you do not need an initialization block, you can skip it but should put in a semicolon anyways.
  • Next, the boolean expression is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement past the for loop.
  • After the body of the for loop executes, the flow of control jumps back up to the update statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the Boolean expression.
  • After completing the update statement, the boolean expression is evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then update step, then boolean expression). Once the boolean expression is false, the for loop terminates.

Example:

public class ForLoopDemo {

   public static void main(String args[]) {

      for(int x = 10; x < 20; x++) {
         System.out.println("value of x : " + x );
      }
   }
}

Check the results

Break and Continue statements

The break keyword in loops:

The break keyword is used to stop the entire loop. The break keyword must be used inside any loop or a switch statement. The break keyword will stop the execution of the loop and start executing the next line of code after the loop block. In case of nested loops, break statement will stop the execution of the innermost loop where the break is present. Outer loops are unaffected.

Syntax: The syntax of a break is a single statement inside any loop:

break; Example:

public class BreakExample {

   public static void main(String args[]) {

     for(int x = 10; x < 20; x++) {
         if( x == 15 ) {
             break;
         }
         System.out.println("value of x : " + x );
     }

   }
}

Check the result. What do you notice? Why did the display stop at number 15?

The continue keyword in loops:

The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop. In a for loop, the continue keyword causes flow of control to immediately jump to the update statement. In a while loop or do/while loop, flow of control immediately jumps to the Boolean expression. Syntax: The syntax of a continue is a single statement inside any loop:

continue; Example:

public class ContinueExample {

   public static void main(String args[]) {

     for(int x = 10; x < 20; x++) {
         if( x == 15 ) {
             continue;
         }
         System.out.println("value of x : " + x );
     }

   }
}

Check the results. What do you notice? Why is number 15 missing in the print statement?

results matching ""

    No results matching ""