
In Java, there are three kinds of loops which are – the for loop, the while loop, and the do-while loop. All these three loop constructs of Java executes a set of repeated statements as long as a specified condition remains true.
This particular condition is generally known as loop control. For all three loop statements, a true condition is the one that returns a boolean true value and the false condition is the one that returns the boolean false value.
Every loop has its elements or variables that govern its execution. Generally, a loop has four elements that have different purposes which are:
1. Initialization Expression(s)
Before entering into a loop, we must initialize its control variable. The initialization of the control variable takes place under initialization expression.
2. Test Expression
The test expression is an expression whose truth (boolean) value decides whether the loop body will be executed or not.
3 Update Expression(s)
The update expression(s) changes the values of the loop variables.
4. The Body of the Loop
The statements which execute repeatedly (as long as the test expression is non zero) form the body of the loop.
Types of Loops in Java
1. The for Loop
The for loop in Java is an entry controlled loop that allows a user to execute a block of a statement(s) repeatedly with a fixed number of times on the basis of the test expression or test-condition.
The syntax :
for(initialization;condition;i++) { statement inside loop }
Example:
public class ForLoop{ public static void main(String[] args){ int n=2; for(i=1;i<=n;i++){ System.out.println(" java is fun"); } } } OUTPUT java is fun java is fun
2. The while Loop
The next loop available in Java is the while loop. The while loop is an entry-controlled loop.
The syntax :
while(test_expression) { loop_body; }
In a while loop, the loop-body may contain a single, compound or an empty statement. The loop repeats while the test expression or condition evaluates to true. When the expression becomes false, the program control passes to the line just after the end of the loop-body code.
Example:
public class WhileLoop{ public static void main(String[]args){ long i=0, fact=1,num=5; i=num; while(num!=0){ fact=fact*num; --num; } System.out.println("The factorial of "+i+" is "+fact); } } // OUTPUT : The factorial of 5:120
3. The do-while Loop
Unlike the for and while loops, the do-while loop is an exit-controlled loop which means a do-while loop evaluates its test-expression or test-condition at the bottom of the loop after executing the statements in the loop-body.
do- always executes once whatever happens in test conditions it will print the statement once.
while loop always executes at least once.
The syntax :
do { statement(s); }while(test_expression);
Example:
public class DoWhileLoop{
public static void main(String[] args){
char ch='A';
do{
System.out.println(ch + " ");
ch++;
}while(ch<='Z');
}
}
//OUTPUT
A B C D E F G H I J K L M N O P Q R S T5 U V W X Y Z
Java for-each Loop
In Java, the for-each loop is used to iterate through elements of arrays and collections (like ArrayList). It is also known as the enhanced for loop.
syntax :
for(datType item:array) { ....... }
public class EnhanceLoop{ public static void main(String[] args){ int[] numbers={3,6,9,6}; for(int number:numbers){ System.out.println(number); } } } OUTPUT 3,6,9,6