· 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
What is Code Obfuscation?Developers spend hundreds of hours developing code, only to find that their code has been pirated.In addition , patent laws do not make it easy to excersize intellectual propert...- Chief Risk Officers Lessening Financial Accounting CorruptionChief Risk Officer are valuable to corporations because they bring expertise to the company about how to reduce, manage, and evaluate risks within the company.
- Democracy and Civil Society in TurkmenistanDemocracy has become a widespread form of government. This research paper examines democracy and civil society in Turkmenistan.
10 Best New Roller Coasters for 2008With theme parks re-opening for a new season, it's time to preview the ten most anticipated roller coasters for 2008.
9 Career Lessons from the New York YankeesAside from "knowing when to fold 'em," let's take a look at some lessons your career could learn from the New York Yankees and baseball in general.
- JavaScript Loop Statements
- PHP Loop Statements
- ActivePerl Loop Statements
- Writing a Powerful Personal Statement for Your College Application
- Self-Loop: In Movies and in Life
- The Effect of Feedback Loops on Climate Change
- Exception Handling in C Plus Plus (C++)
- 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.
