Looping Statements in Java

Harsh Gupta - Tech Writer
Looping statements in java enables you to execute blocks of statements repeatedly. The JAVA programming language supports three type of loop constructs:

· for loop

· Do-while loop

· while loop

The "for" and "while" loop tests the condition statement before executing the loop body. This implies that for and while loops might not execute the loop body even once, whereas do loops execute the loop body at least once.

for loop

A for loop is an "entry controlled" loop i.e. it will test the condition before entering in the loop body. The statements within the loop will be getting executed at least once if and only if the condition is true otherwise; the following loop statements will be getting executed. The for loop syntax is:

For (; ; )

You can also use an enhanced for loop instead of the regular for loop. Enhanced for loop is an advance feature introduced in java 1.5 that enables you to iterate over the complete array without giving its length or size. The syntax of enhanced for loop is:

for (datatype variable/element : array name)

while loop

A while loop is an "entry controlled" loop. This loop will check the condition before entering in the loop. The statements within the loop will be getting executed at least once if and only if the condition is true otherwise; the following loop statements will be getting executed. The syntax for while loop is:

while ()

Ensure that the loop-control variable is initialized appropriately before the loop body begins execution. You must update the control variable appropriately to prevent an infinite loop.

Do-while loop

The do/while loop is an "exit controlled" loop. This loop will test the condition at the exit of the loop statements. So at least once the statements will be get executed and, after once the condition will be checked. If than the condition is satisfied the loop will continue otherwise, the following statements will be get executed. The syntax for do/while loop is:

do

< statement_or_block >

while (As with the while loops, ensure that the loop-control variable is initialized appropriately, update in the body of the loop, and tested properly.

Use the 'for' loop in the cases where the loop is to be predetermined number of times. Use the while and do loops in cases where this is not determined beforehand.

Published by Harsh Gupta - Tech Writer

I am a part time freelancer and writing is my hobby Some of my websites: http://www.GenericArticles.com http://www.JailBreakingiPhone.com  View profile

  • For loop is an "entry controlled" loop i.e. it will test the condition before entering the loop body
  • A while loop is an "entry controlled" loop.
  • The do/while loop is an "exit controlled" loop.
The do/while loop is an "exit controlled" loop. This loop will test the condition at the exit of the loop statements. So at least once the statements will be get executed and, after once the condition will be checked.

To comment, please sign in to your Yahoo! account, or sign up for a new account.