Java Theory
Types of Loops
Loops are used to executed a set of statements repeatedly until a particlar condition is satisfied. There are many types of loops available in java to perform a specific task repeatedly.
- For Loop
- While Loop
- Do While Loop
For Loop
For Loop
It execute a sequence of statements multiple times. A for loop has three arguments, namely, Initialization, test-condition, and increment/decrement variable.
Syntax |
---|
for(initialization statement; test condition; increment/decrement statement) { //statement to be executed repeatedly } |
While Loop
While Loop
In while loop, initialization condition is evaluated first and if it returns true then the statements inside while loop execute. When condition returns false, it comes out of the loop. It tests the increment / decrement condition before executing the loop body.
Syntax |
---|
initialization statement; while (test-condition) { //statements to be executed repeatedly increment/decrement statement } |
Do While Loop
Do-While Loop
Do While loop is considered to be a conditional statement completely similar to a normal While loop. The only difference is that in do while loop except that it tests the condition at the end of the loop body.
Syntax |
---|
initialization statement; do { //statements to be executed repeatedly increment/decrement statement } while (test-condition) |
Note: In do while loop the body will execute at least once irrespective of test condition.