Thursday 5 March 2015

FOR loop in C

The for loop construct is used to repeat a statement or block of statements a specified number of times. The general form of a for loop is:

for (counter-initialization; condition; increment)
statement(s);

The construct includes the initialization of the counter, the condition and the increment. The main function of the for loop is to repeat the statement(s) while the condition remains true. The condition should check for a specific value of the counter. In addition it provides ways to initialize the counter and increment (or decrement) the counter. Therefore the for loop is designed to perform a repetitive action for a pre-defined number of times. Consider the following example:

/* Program-5.1 */

#include <stdio.h>
int main()
{
int counter;
for(counter=1; counter <= 5; counter++) //loop 5 times
{
printf("This is a loop\n");
}
return 0;
}

Execution of program-5.1 displays:

This is a loop
This is a loop
This is a loop
This is a loop
This is a loop

In the above example , the variable “counter” starts with an initial value of “1”. The second expression inside the parenthesis determines the number of repetit ions of the loop. It reads as; “as long as the counter is less than or equal to 5 repeat the statement(s)”. The third expression is the incrimination of the counter; it is achieved by the ++ operator. You may also decrement the counter depending on the requirement, but you have to use suitable control expression and an initial value. In the first round of execution the “counter” is set to “1”. Then the expression “counter <= 5” is evaluated. Since the current value of the “counter” is “1” expression is evaluated as TRUE.


Therefore the printf function gets executed. Then the “counter” is incremented by the ++ operator and now its new value becomes “2”. At this point the first round of the loop is completed. Then in the second round the expression is evaluated again. Since the “counter” is “2” the expression will be TRUE. Therefore the printf function gets executed for the second time. Then the “counter” is incremented once more and its new value becomes “3”. This process continues for another 2 rounds. After a total of five rounds the “counter” becomes “6”. When the expression is evaluated at the beginning of the sixth round the “counter” is greater than 5 therefore expression becomes FALSE. Then the loop will terminate and the control is given to rest of the instructions which are outside the loop.

Example 5.1 – Write a program to calculate the sum of all the even numbers up to 100.

First even number is 0 and the last even number is 100. By adding 2 to an even number the next even number can be found. Therefore the counter should be incremented by 2 in each round. Program-5.2 is an implementation of the above requirement.

/* Program-5.2 */
#include <stdio.h>
int main()
{
int counter, sum;
sum = 0;
for(counter=0; counter <= 100; (counter += 2)) //increment by 2
{
sum += counter;
}
printf("Total : %d", sum);
return 0;
}

Example 5.2 – Write a program to compute the sum of all integers between any given two numbers. In this program both inputs should be given from the keyboard. Therefore at the time of development both initial value and the final value are not known.

/* Program-5.3 */

#include <stdio.h>
int main()
{
int num1, num2, sum;
sum=0;
printf("Enter first number: ");
scanf("%d", &num1); //read num1
printf("Enter second number: ");
scanf("%d", &num2); //read num2
for(; num1 <= num2; num1++)
{
sum += num1; //sum = sum+num1
}
printf("Total : %d", sum);
return 0;
}

No comments:

Post a Comment