Tuesday 12 February 2013

The for Loop in C Language Programming


The for loop is far superior and more powerful than the other loop constructs provided by C language. The construct provides a compact way of specifying the statements that control repetition of the steps within the loop.

In the for construct, the loop control statements are not written within the loop; instead, they are written at the top.

This makes the program more readable and easily understandable.

The following function uses a for loop to accept and display 10 strings entered from the keyboard:

#include <stdio.h>
main()
{
char str1[50];
int times;
for(times=0;times<10;times=times+1)
{
puts("Enter string");
gets(str1);
fflush(stdin);
puts(str1);
}
}

If there are multiple initialisations and/or multiple reinitialisations, they are separated by commas in the for loop control statement. If there are multiple conditions for loop control statement. If there are multiple conditions for evaluation in the for loop, they are compounded by logical operators.

0 comments:

Post a Comment

Powered by Blogger.