Loops are used to repeat one statement or set statements more than one
time. Most real programs contain some construct that loops within the
program, performing repetitive actions on a stream of data or a region
of memory. There are several ways to loop in C.
For Loop
For loop is a counter loop. The for loop allows automatic initialization of instrumentation of a counter variable. The general form is
for (initialization; condition; increment/decrement)
{
statements block
}
If the statement block is only one statement, the braces are not necessary. Although the for allows a number of variations, generally the initialization is used to set a counter variable to its starting value. The condition is generally a relational statement that checks the counter variable against a termination value, and the increment increments (or decrements) the counter value. The loop repeats until the condition becomes false.
Example
Main(){
int i;
for(i = 0; i < count; i++)
{
printf(“%d\n”,i);
}
}
While Loop
The while loop repeats a statement until the test at the top proves false.
The while loop has the general form:
while(condition)
{
statement block
}
The while tests its condition at the top of the loops. Therefore, if the condition is false to begin with, the loop will not execute at all. The condition may be any expression. An example of a while follows. It reads characters until end-of-file is encountered.
Example
main(){
int t = 0;
while(t<=10) { printf(“%d\n”,t); t=t+1; } }
do-while loop
This is very similar to the while loop except that the test occurs at the end of the loop body. This guarantees that the loop is executed at least once before continuing. Such a setup is frequently used where data is to be read. The test then verifies the data, and loops back to read again if it was unacceptable.
void main(void){
int val;
do
{ printf("Enter 1 to continue and 0 to exit :");
scanf("%d\n", &val);
} while (val!= 1 && val!= 0)
}
For Loop
For loop is a counter loop. The for loop allows automatic initialization of instrumentation of a counter variable. The general form is
for (initialization; condition; increment/decrement)
{
statements block
}
If the statement block is only one statement, the braces are not necessary. Although the for allows a number of variations, generally the initialization is used to set a counter variable to its starting value. The condition is generally a relational statement that checks the counter variable against a termination value, and the increment increments (or decrements) the counter value. The loop repeats until the condition becomes false.
Example
Main(){
int i;
for(i = 0; i < count; i++)
{
printf(“%d\n”,i);
}
}
While Loop
The while loop repeats a statement until the test at the top proves false.
The while loop has the general form:
while(condition)
{
statement block
}
The while tests its condition at the top of the loops. Therefore, if the condition is false to begin with, the loop will not execute at all. The condition may be any expression. An example of a while follows. It reads characters until end-of-file is encountered.
Example
main(){
int t = 0;
while(t<=10) { printf(“%d\n”,t); t=t+1; } }
do-while loop
This is very similar to the while loop except that the test occurs at the end of the loop body. This guarantees that the loop is executed at least once before continuing. Such a setup is frequently used where data is to be read. The test then verifies the data, and loops back to read again if it was unacceptable.
void main(void){
int val;
do
{ printf("Enter 1 to continue and 0 to exit :");
scanf("%d\n", &val);
} while (val!= 1 && val!= 0)
}
No comments:
Post a Comment