Saturday, 10 August 2013

Looping in C

Welcome to Wind Trainers :: Trainers, C, Tutorials, Programming, Java, VB.net, C++, ANSI, Learn C, Learn Programming
Forum | Contact

C Tutorials


| Loops |

C programming language provides following types of loop to handle looping requirements.

Loop Type Description
for loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
do while loop Like a while statement, except that it tests the condition at the end of the loop body

| Control Statements |


Control Statement Description
break Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
continue Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
goto Transfers control to the labeled statement. Though it is not advised to use goto statement in your program.

| Infinite Loop |



A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty.

#include <stdio.h>
int main ()
{
for( ; ; )
{
printf("This loop will run forever.\n");
}
return 0;
}

When the conditional expression is absent, it is assumed to be true.

No comments:

Post a Comment