Java Looping or Iteration Statements
Looping or Iteration Statements in Java
Loops in Java
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 :
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 statement instead of the general for statement whenever possible.
Java for loop Example
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 :
Java Enhanced for loop Example
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 :
Java while loop Example
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 :
Java do while loop Example