Bright Java Tutorial

is brought to you by DoITBright LLC

Java

For, While, and Do While

There will be a time when we need to execute one or more line of codes up to our desired number of times. To accomplish this, like most programming languages, Java provides a concept of a loop. The loop concept can be achieved using either the for statement, while statement, or do-while statement. Let us demostrate this using the example below.

  package com.example.core;

  /**
   * This is to demonstrate on how to use loops. 
   * Loops are there to do repetitive sequence of work.
   * 
   * @author Rolan Liwanag
   *
   */
  public class Loop {

    public static void main(String[] args) {
      //Looping using a for loop
      for(int i=0; i<3; i++) {
        //i++ means i = i + 1;
        System.out.println("Inside the for loop...");
        System.out.println("The value of i is " + i);
      }
      System.out.println("The loop ended.");
      /*
       * As you may have noticed, the lines of code inside 
       * the for loop got executed 3 times. Here is how it  
       * worked. The variable i initially has a value of 0. 
       * Then, it got evaluated. Is 0 < 3. The statement is 
       * true so it went inside the for loop block of 
       * code. Then, i was increased by 1. Then, 
       * i got evaluated again. Is 1 < 3. The statement is 
       * true and for loop block of code got executed again. 
       * The variable i was increased by 1. Variable i is 
       * equal to 2. Now, is 2 < 3? As it still 
       * satisfies the condition, the lines of codes inside
       * the for loop still gets executed. Then, i is 
       * increased by 1. Variable i is now equal to 3. 
       * Is 3 < 3? Because it failed to satisfy the 
       * condition, the loop now terminates and executes 
       * the lines of code, if there are any,
       * outside the for loop.
       */
      //Looping using a while loop.
      int j = 10;
      while(j > 8) {
        System.out.println("Inside the while loop...");
        System.out.println("The value of j is " + j);
        j--;
      }
      /*
       * Same concept. This time, the variable j is 
       * declared outside the while loop. Then, it gets 
       * evaluated by the while condition as 10 > 8. 
       * Since condition is satisfied, the lines of code
       * inside the block of the while loop gets executed.
       * The j-- means j = j - 1. Now, is 9 > 8. 
       * The statement is still true. So, the lines of 
       * code inside the while loop still gets executed. 
       * Is 8 > 8. The statement is false, the while loop 
       * exits.
       */
      //Looping using do while loop
      int k = 1;
      do {
        System.out.println("Inside the do while loop...");
        System.out.println("The value of k is " + k);
        k = k + 1;
      } while(k < 2);
      /*
       * Like the while loop, we declared the variable k 
       * outside the do while statement. And unlike the 
       * for and while loops, do while loop immediately 
       * executes the lines of codes inside the do while 
       * block. The variable k is increased by 1.
       * Then, it gets evaluated. Is 2 < 2? The statement 
       * is false. So, loop terminates. The do while loop 
       * always gets executed at least once since the 
       * evaluation of the condition happens at the end.
       */
      //looping through arrays
      //We will discuss arrays in more detail later on.
      int[] intArr = {1,2};
      for(int l : intArr) {
		System.out.println("The value of l is " + l);
      }
      /*
       * The array contains 1 and 2. The for loop used 
       * the statement int l : intArr to place one value
       * at a time during the execution of the loop.
       * This means, the variable l is set everytime the 
       * loop executes. The loop will execute based on 
       * the number of array elements.
       */
      //looping an array the traditional way is...
      for(int index=0; index < intArr.length; index++) {
        System.out.println("The value of intArr at " 
          + "index " + index + " is " + intArr[index]);
      }
      /*
       * At index 0, it will retrieve the first value 
       * inside the array which is 1. At index 1, it
       * will retrieve the second value inside the array 
       * which is 2.
       */
      //Breaking out of the loop when we desire it
      for(int n=0;n < 2;n++) {
        if(n==1) {
          //this will force the loop to terminate
          break;
        }
        System.out.println("The value of n is " + n);
      }
      /*
       * Initially, n is equal to 0. Since 0<2, it goes 
       * inside the for loop block.  It now goes into the 
       * if condition. Since n is not equal to one, it 
       * wont go inside the if block. Then, it executes 
       * the print console command. n is increased by 1.
       * Since n is less than 2, it gets inside the for 
       * loop block. Then if statement gets evaluated.
       * Since n is equal to 1, the break command gets 
       * executed. Loop now terminates.
       */
    }
  }
                           



Back    Next