JAVA.
Looping is an important topic, Which helps us to repeat a particular statement or a block of statement or an operation. so what is loop? what are the types of loop? how does it works? to know read the following carefully and completely.
DECISION MAKING AND LOOPING.
When a sequence of statement is needed to be executed repetitively until some conditions for terminated the loop is satisfied.
Loops are required when a set of statement is must be executed repeatedly.
A looping structure is also known as iterative structure. This looping structure are classified into 2 groups.
Entry-control loop: The loop in which condition is checked first and then the body of the statement is executed.
eg: while loop, for loop.
Exit-control loop: The loop in which the body of the loop is executed first and the the condition is checked.
eg: do-while loop.
let us know about the types of loop now.
There are 4 basic loops.
- while loop.
- do-while loop.
- for loop.
- nested for loop.
while loop.
This is a entry-control loop.
The condition of the loop is checked first, when the condition is true the body of the loop is executed, this process is continuous until the condition becomes false.
If the condition is false, the control is moved out of the loop to the next statement.
syntax:
while(condition)
{
......
...... /* body of the loop */
}
flow chart:
do-while loop:
This is an exit control loop.
The body of the loop is executed first using do keyword, and then the condition is checked.
If then the condition is true again the body of the loop is executed. and again the condition is checked.
When the condition is false the control is moved to the next statement.
The body of the loop is executed until the condition is true.
Syntax:
do
{
......
...... /* body of the loop */
} while(condition)
flow chart:
for() loop.
This is an entry control loop.
The condition is checked first and then the body of the loop is executed.
condition part is divided into 3 part.
- initial value.
- condition.
- increment/decrement.
First, the value is initiated, it is executed only once.
Next the conditions checked, if the condition is true the body of the loop is executed, and the control is moved to increment or decrement.
Increment/decrement is used either to increase the value by 1 or to decrease the value by 1. after increment/decrement again the condition is checked.
If the condition becomes false then control is moved out of the loop to the next statement.
syntax:
for(initial;condition;increment/decrement)
{
......
...... /* body of the loop */
}
flow chart:
Nested loop:
When a loop is enclosed in another loop then it is said to be nested loop.THe inner loop is executed for every value of the outer loop.
The general form of nested loop:
for(initial;condition;increment/decrement)
{
for(initial;condition;increment/decrement)
{
......
...... /* body of the loop */
}
}
Check out: Decision making and branching.



Comments
Post a Comment