Java Looping or Iteration Statements
Looping or Iteration Statements in Java
Loops in Java
Java Loops
Looping in Java programming language is a feature which facilitates the execution of a set of instructions or functions repeatedly while some condition evaluates to true. The looping statements (for, while, do-while).
Types of Looping Statements in Java
- Java for Loop
- Java Enhanced for Loop
- Java while Loop
- Java do while Loop
What is loop in Java ?
The Java loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages. Java programming language provides the following types of loop to handle looping requirements.
for loop in Java
The Java for loop repeats the execution of a set of Java statements. A for loop executes a block of code as long as some condition is true. The for loop's initialization, condition, increment or decrement all are in a single statement. The for loop has two forms one is classic for loop and another is enhanced for loop.
Java for Loop Syntax :
for( < initialization > ; < condition > ; < increment > ) { Statement 1; Statement 2; Statement n; }
Tips for Java for Loop
- If you ignore the braces after loop statement the first statement after loop is executed in loop.
- If you put ; (semi colon) to loop without braces no statement is executed.
- If you put ; (semi colon) to loop with braces the body of the loop is executed once.
The for statement also has another form designed for iteration through Collections and Arrays This form is sometimes referred to as the enhanced for statement, and can be used to make your loops more compact and easy to read.
We recommend using enhanced for loop statement instead of the general for loop statement whenever possible.
Java for loop Example
/* Java for loop Example Save with file name ForLoop.java */ public class ForLoop { public static void main(String args[]) { System.out.println("Java for loop Example "); for(int count=1 ; count<=10 ; count++) { // POST INCREMENT System.out.println("Loop Statement : " + count); } System.out.println("Loop 2 output"); // ANOTHER WAY int i = 0; for(;i<10;) { // PRE INCREMENT WITH IN THE BODY OF THE LOOP System.out.println("Loop Statement : " + ++i); } System.out.println("Loop 3 output"); // LOOP WITH NO BODY int j=0; for(;j<10;j++); // ; (SEMICOLON) IGNORE THE BODY OF THE LOOP { System.out.println("loop body ignore"); } System.out.println("j value after loop execution : " + j); } }
Enhanced for loop in Java
The for statement also has another form designed for iteration through Collections and arrays This form is sometimes referred to as the enhanced for statement, and can be used to make your loops more compact and easy to read. Enhanced for loop introduced in Java 5. In the following Java enhanced for loop example you can learn how to use enhanced for loop in Java.
Java Enhanced for Loop Syntax :
for( < T element : Collection Object / array > ) { Statement 1; Statement 2; Statement n; }
Java Enhanced for loop Example
/* Java Enhanced for loop Example Save with file name ForLoop.java */ public class EnhancedForLoop { public static void main(String args[]) { System.out.println("Java Enhanced for loop Example "); int[] numbers = {1,2,3,4,5,6,7,8,9,10}; for (int item : numbers) { System.out.println("Count is: " + item); } } }
while Loop in Java
The while statement continually executes a block of statements while a particular condition is true. The while loop can be thought of as a repeating if statement. The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. The while loop is also called as Entry Control Loop. In the following while loop example you can learn how to use while loop in Java.
Java while Loop Syntax :
while( < condition > ) { Statement 1; Statement 2; Statement n; }
Java while loop Example
/* Java while loop Example Save with file name WhileLoop.java */ public class WhileLoop { public static void main(String args[]) { System.out.println("Java while loop Example "); int count = 1; while(count<=10) { // POST INCREMENT System.out.println("Loop Statement : " + count); count++; } } }
do while Loop in Java
The statements within the do while loop are executed at least once. It checks the condition at bottom of loop, it is a bottom tested loop. The do while loop is also called as Exit Control Loop.
Java do while Loop Syntax :
do { Statement 1; Statement 2; Statement n; } while(<condition>);
Java do while loop Example
/* Java do while loop Example Save with file name DoWhileLoop.java */ public class DoWhileLoop { public static void main(String args[]) { System.out.println("Java do while loop Example "); int count = 1; do { // POST INCREMENT System.out.println("Loop Statement : " + count); count++; } while(count<=10); } }