A computer is a versatile device because it has got the ability to perform a set of the instructions repeatedly. This will involve some recurring portions of a program either a defined number of times or until a particular condition is satisfied. This recurring operation is completed through a loop control instruction.
There are three different methods which allows you to repeat a part of the program.
- Using for statement
- Using a while statement
- Using a do-while statement
The while Loop
The while loop is suited for the cases where repetitive tasks are required. It is a control flow statemnt that lets the codes to be executed on recurring basis on the given Boolean condition. This loop consists of block of the code and a constion or expression. The condition is then evaluated and if the condition is true the code will execute till the condition becomes false.
The for Loop
The for Loop is the most popular instruction. This loop lets you to specify three things about a loop in a single line.
- Setting a loop counter to an initial value.
- Testing the counter of loop in order to determine whether the value has reached number of repitions desired.
- Incresing the loop counter value every time the program segment within the loop is executed.
For statement’s genral form is as under: