for Loop
For is the most popular looping control.The for allows us to specify three things about a loop in a single line:
(1) setting a loop counter to an initial value.
(2) testing the loop counter to determine whether its value has reached the number of repititions desired.
(3) incresing the value of loop counter each time the program segment within the loop has been executed.
The general form of for loop is as shown below:
for(initialise counter; test counter; increment counter)
{
do this;
and this;
and this;
}
A simple program which demonstrates the use of for loop:
/* print 1 to 10 numbers */
main()
{
int num = 1;
for(num=0; num <= 10; num++)
{
printf(“%d”,num);
}
}
If this program is compared with the one written using , it can be seen that the three steps – initialisation, testing and incrementation required for the loop construct have now been incorporated in the for statement.
Let us now examine how the for statement gets executed:
(1) When the for statement is executed for the first time, the value num variable is set to an initial value 1.
(2) Now thw condition (num <= 10) is tested.Since the num is 1 the condition is satisfied and the body of the loop is executed for the time.
(3) Upon reaching the closing braces of for, computer sends the condition back to the for statment, where the value of num gets incremented by 1.
(4) Again the test is performed to check whether the new value of num exceeds 10.n(5) If the value of num is still within the range 1 to 10, the statements within braces of for are executed again, if not then for loop gets terminated.
Loop
It is often the case in programming that you want to do something a fixed number of times.The loop ideally suited for these cases.
The general form of is as shown below:
initialise loop counter;
(test loop counter using a condition)
{
do this;
and this;
increment loop counter;
}
A simple program which demonstrates the use of loop:
/* print 1 to 10 numbers */
main()
{
int num = 1;
(10 <= num)
{
printf(“%d”,num);
num++;
}
}
The program executes all statements after the 3 times.Every time num variable get increment by one, and program will print 1 to 10 numbers.
Note the following points about loop…
(1) The statements within the loop would keep on getting executed till the condition being tested remains true.When the condition become false, the control passes to the first statement that follows the body of the loop.
(2) The condition being tested may use relational and logical operator.
(3) The statement within the loop may be a single line or a block of statements.
do- Loop
The do- loop looks like this:
do
{
do this;
and this;
and this;
and this;
}whiel(this condition is true);
There is a minor difference between the working of and do- loops.This difference is the place where the condition is tested.The tests the condition before executing any of the statements within the loop.As against this, the do- tests the condition after having executed the statements within the loop.
This means that do- would execute its statements at least once, even if the condition fails for the first time itself.The , on the other hand will not execute its statements if the condition fails for the first time.This difference is brought about more clearly by the following program.
main()
{
(1 > 4)
{
printf(“hello world”);
}
}
Here, since the condition fails for the first time itself the printf() will not get executed at all.
Let’s now write the same program using do- loop.
main()
{
do
{
printf(“hello world”);
} (1 > 4);
}
In this program the printf() would be executed once, since first the body of the loop is executed and then the condition is tested.