Looping statements

In a programming language we have to repeat set of instruction many time as per programmer’s requirements.

To repeat instructions java provides looping statements. Looping is also known as Iterative statement
To achieve looping java provides following looping statements.
1. For statement
2. While statement
3. Do while statement

Work of above statement are same but the have syntax difference. Let us see all of them one by one

For statement

Syntax:

Example

In for statement first loop variable is initialized then condition is checked if condition is true body of loop is executed after that variable is updated.   

Again condition is cheeked condition satisfied body of loop is executed and variable updated this process is continue until condition is false.

Variable initialization is done only once.

Output

While statement

Syntax

In while statement only condition is checked if condition is true then the body of loop is executed loop variable initialization should be before the while statement. Variable updating is part of body of loop.


Example

Output

do while statement

Syntax

In do loop condition is cheeked at last. First time body of loop is excuted without checking the condition after that if condition specified in while is true then body of loop is executed until condition is false.
Example

Result

If condition is false then also do while statement is executed once.
Example

Result

Here condition in do while is false then also body of loop is executed once.