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.
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 your Java OCA exam, make sure you remember the below points regarding for statement.
for(int i = 0; i<10; i++){
.....
}
System.out.println(i); //fails to compile
//legal
int i;
for(i = 0; i<10;i++){
.....
}
System.out.println(i);
//below code is legal
for(int i=0,j=1,k=3;i<j && j<k; i++,j++,k++){
......
}
//illegal- fails to compile
for(int i=0, String val="Hey";...........)
{
//loop body
}
for(;;)
System.out.println("Hello");
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);
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.
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++;
}
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.
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);
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.