In this section, we will discuss about Looping Statements in java 7. This is one type of control flow statement.
Looping Statements :
This kind of statements are used where you need to execute block of statements repeatedly. Following are three types of Looping statements -
1. For Loop :
For loop provides a way to repeat block of code under particular condition. It is single line statement. It contains three terms-initialization, condition and increment/decrement.
for(initialization; Boolean termination; increment/decrement)
initialization : In initialization, we assign some initial value to the loop variable and it is executed only once during the execution of for loop.
termination/condition : This is the point where your loop stop. We write some condition here.
increment/decrement : If condition is true then your variable either increment or decrement as you specify.
2. Do-while Loop :
It is a control structure which is similar to while loop except that a do-while loop is guaranteed to execute at least one time.
Here expression is declared at the end of the loop, so that the statements in the loop will get execute once before the expression is tested. If the condition is true, the flow of control jumps back to do, and the statements in the loop execute again. It repeats until the condition is false.
3. While Loop :
This type of control structure repeat the task certain number of times. It is more easier to understand in comparison to for loops.
When executing, if the expression result is true then statement inside the loop will get executed and it will continue as long as the expression result is true.
Posted on: December 22, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles