Core Java

OCA Java 8 Preparation : Looping Statements

Looping statements provide a means to achieve an iteration over a set of elements or to perform some operation repeatedly without adding redundant lines of code.Java provides a variety of looping or iteration statements.

For your Java OCA exam, you need to be comfortable with the use of Java looping statements and we have some interesting stuff to cover.

for Statement :

It is one of the most basic loops which makes use of a counter to keep track of its iteration.

//syntax of basic for loop
for(initialization;condition;update_expression){
   //body of the loop
}

//Sample usage
for(int i=0;i<10;i++)
    System.out.println(i);
for(int i=10;i>0;i--)
    System.out.println(i);

The update_expression is always executed after completion of each iteration.

for Loop in Java

For your Java OCA exam, make sure you remember the below points regarding for statement.

  1. Any variable which is declared in the initialization block will not be accessible outside of that loop. Beware of falling for such traps when solving your question.
    for(int i = 0; i<10; i++){
     .....
    }
    System.out.println(i); //fails to compile
  2. It is possible to not declare and simply initialize the variable in a loop.
    //legal
    int i;
    for(i = 0; i<10;i++){
     .....
    }
    System.out.println(i);
  3. It is possible to declare and initialize multiple variables in the initialization section.
    • When of the same type, we can provide a comma-separated list of values.
      //below code is legal
      for(int i=0,j=1,k=3;i<j && j<k; i++,j++,k++){
      ......
      }
      
    • If the declared variables are of different types, a comma-separated list of values in initialization block will fail to compile. For such instances, we must declare them outside the loop.
      //illegal- fails to compile
      for(int i=0, String val="Hey";...........)
      {
       //loop body
      }
  4. The update expression can also be a comma separated list of values.
  5. All the sections in the for( ;; )  statement are optional. We can leave any of them and even all of them empty.However, the two semicolons are mandatory.
  6. When the condition statement is kept empty, it will always be assumed to be evaluated to true.
  7. The empty for statement – for(;;) is also known as crab and is used to have an infinite loop.
    for(;;)
      System.out.println("Hello");

The enhanced-for statement :

The enhanced-for statement, sometimes also referred as extended-for or a for-each statement. It is used in situations when we need to traverse over a list of elements. It can operate only in the forward direction and it provides no means to determine the current index of the list while traversing the loop.

Unlike basic for statement, it also doesn’t allow iteration over multiple arrays in one-go.

//Syntax of extended-for
//list here means any collection of objects including Arrays
for(Type element : listOfElements){
  //loop body
}

//Sample usage
int[] elements = {1,2,3};
for(int i : elements)
   System.out.println(i);

while statement :

In while loop, the condition is evaluated before the execution of the body of the loop. As long as the condition holds true, the loop body is repeatedly executed.

While Loop in Java

Below is the syntax and its sample usage.

//Syntax
//condition must evaluate to either boolean or Boolean type
while(condition){
  // body of the loop
}

//sample usage
int i = 0;
while(i<10){
   System.out.println(i);
   i++;
}

do-while statement :

In a do-while statement, the loop condition is evaluated after the execution of loop body. The do-while statement therefore ensures that the loop body is executed at least once.Do-while Loop in Java

It executes the loop body repeatedly until the condition evaluates to false.

//Syntax of do-while - condition must be a boolean or Boolean type
do{
//loop body
}while(condition);

//Sample usage - prints false as an output
boolean flag = false;
do{
System.out.println(flag);
}while(flag);

 Conclusion :

In this tutorial, we covered the different types of looping statements available in Java. We now know what a crab is. With the points presented in this article on your fingertips, I am pretty sure you’ll be able to answer any question based out on this topic in your OCA.

If you wish to learn about the transfer statements used within looping statements, refer this article.

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *