Wednesday

Loop in c languag



Looping

Loops provide a way to repeat commands and control how many times they are repeated. C provides a number of looping way.

while loop


The most basic loop in C is the while loop.A while statement is like a repeating if statement. Like an If statement, if the test condition is true: the statments get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true the statements get executed again.This cycle repeats until the test condition evaluates to false.
Basic syntax of while loop is as follows:


while ( expression )
{
   Single statement
   or
   Block of statements;
}   
for loop
for loop is similar to while, it's just written differently. for statements are often used to proccess lists such a range of numbers:
Basic syntax of for loop is as follows:

for( expression1; expression2; expression3)
{
   Single statement
   or
   Block of statements;
}
   
In the above syntax:
·    expression1 - Initialisese variables.
·    expression2 - Condtional expression, as long as this condition is true, loop will keep executing.
·    expression3 - expression3 is the modifier which may be simple increment of a variable.
do...while loop
do ... while is just like a while loop except that the test condition is checked at the end of the loop rather than the start. This has the effect that the content of the loop are always executed at least once.
Basic syntax of do...while loop is as follows:


do
{
   Single statement
   or
   Block of statements;
}while(expression);    

Kindly Bookmark this Post using your favorite Bookmarking service:
Technorati Digg This Stumble Stumble Facebook Twitter
YOUR ADSENSE CODE GOES HERE

0 comments:

Post a Comment

Dont Forget for Commets

 

| C programing tutorials © 2009. All Rights Reserved | Template Style by My Blogger Tricks .com | Design by Brian Gardner | Back To Top |