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.

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.
  2. It is possible to not declare and simply initialize the variable in a loop.
  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.
    • 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.
  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.

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.

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.

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.

 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 *