Loops :
The versatility of computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of a program either a specified number of times or until particular condition is being satisfied. this repetitive operation is done through a loop control structure. There are 3 methods by which we can repeat a part of program. They are:
- Using a for statement
- Using a while statement
- Using a do - while statement
for(initialization; condition; decrement or increment iterator)
{
statements;
}
While loop:
It is often the case in programming that you want to do something a fixed number of times. perhaps you want to calculate gross salaries of 10 different persons or you want to convert temperature from centigrate or farenhite for 15 different cities. The while loop is ideally suited for such cases.
while(condition)
{
statements;
}
The while loop in C is also a top tested loop, i.e., the condition is tested first, and only if the condition is true, the statements in the body of the loop are executed. The syntax is:
<initialization>
while (<testcondition>)
{
statements...
...
increment/decrement
};
Do - while loop :
do
{
statements;
}while(condition);
No comments:
Post a Comment